blender/tests/python/bl_pyapi_bpy_path.py
Campbell Barton 93145bafae Core: apply auto-execution overrides when opening blend-files
Command line overrides now define the default "Trusted Source"
for drop and the file-selector.

This is done so the default matches the behavior of opening
files in contexts where the option to trust source isn't shown.

"Recover Last Session" run without setting "use_scripts"
(from Python for e.g.) now disables auto-execution
unless a command line override is passed in,
as the path being recovered isn't known until the file is read.

This includes a refactor & tests.

Details:

- `bpy.path.is_autoexec(..., skip_overrides=False)`:
  Can be used to check if the current environment would trust a file.
- Expose: `bpy.app.autoexec_override`,
  set when `--enable-autoexec` & `--disable-autoexec` are passed in.
- `BKE_autoexec_default_trust_source`:
  new function to more conveniently access the default trust for a file.
- Show when "Auto Run Python Scripts" preference is overridden.
- Add tests for the file selector, dropping & the command line.

Ref !163894
2026-09-17 11:17:29 +10:00

83 lines
3.8 KiB
Python

# SPDX-FileCopyrightText: 2015-2022 Blender Authors
#
# SPDX-License-Identifier: Apache-2.0
# ./blender.bin --background --python tests/python/bl_pyapi_bpy_path.py -- --verbose
import unittest
class TestBpyPath(unittest.TestCase):
def test_ensure_ext(self):
from bpy.path import ensure_ext
# Should work with both strings and bytes.
self.assertEqual(ensure_ext('demo', '.blend'), 'demo.blend')
self.assertEqual(ensure_ext(b'demo', b'.blend'), b'demo.blend')
# Test different cases.
self.assertEqual(ensure_ext('demo.blend', '.blend'), 'demo.blend')
self.assertEqual(ensure_ext('demo.BLEND', '.blend'), 'demo.BLEND')
self.assertEqual(ensure_ext('demo.blend', '.BLEND'), 'demo.blend')
# Test empty extensions, compound extensions etc.
self.assertEqual(ensure_ext('demo', 'blend'), 'demoblend')
self.assertEqual(ensure_ext('demo', ''), 'demo')
self.assertEqual(ensure_ext('demo', '.json.gz'), 'demo.json.gz')
self.assertEqual(ensure_ext('demo.json.gz', '.json.gz'), 'demo.json.gz')
self.assertEqual(ensure_ext('demo.json', '.json.gz'), 'demo.json.json.gz')
self.assertEqual(ensure_ext('', ''), '')
self.assertEqual(ensure_ext('', '.blend'), '.blend')
# Test case-sensitive behavior.
self.assertEqual(ensure_ext('demo', '.blend', case_sensitive=True), 'demo.blend')
self.assertEqual(ensure_ext('demo.BLEND', '.blend', case_sensitive=True), 'demo.BLEND.blend')
self.assertEqual(ensure_ext('demo', 'Blend', case_sensitive=True), 'demoBlend')
self.assertEqual(ensure_ext('demoBlend', 'blend', case_sensitive=True), 'demoBlendblend')
self.assertEqual(ensure_ext('demo', '', case_sensitive=True), 'demo')
def test_is_autoexec(self):
import bpy
from bpy.path import is_autoexec
prefs = bpy.context.preferences
path_cmp = prefs.autoexec_paths.new()
self.addCleanup(prefs.autoexec_paths.remove, path_cmp)
path_cmp.path = "/untrusted/"
self.assertFalse(is_autoexec("/untrusted/", skip_overrides=True))
self.assertFalse(is_autoexec(b"/untrusted/", skip_overrides=True))
self.assertTrue(is_autoexec("/trusted/", skip_overrides=True))
self.assertFalse(is_autoexec("/untrusted/demo.blend", strip_filename=True, skip_overrides=True))
self.assertTrue(is_autoexec("/trusted/demo.blend", strip_filename=True, skip_overrides=True))
path_cmp.use_glob = True
path_cmp.path = "*/download*"
self.assertFalse(is_autoexec("/home/user/downloads/", skip_overrides=True))
self.assertTrue(is_autoexec("/home/user/projects/", skip_overrides=True))
# The default for "Trusted Source" also checks the preference.
path_cmp.use_glob = False
path_cmp.path = "/untrusted/"
prefs_filepaths = prefs.filepaths
self.addCleanup(
setattr, prefs_filepaths, "use_scripts_auto_execute", prefs_filepaths.use_scripts_auto_execute)
prefs_filepaths.use_scripts_auto_execute = True
self.assertTrue(is_autoexec("/trusted/", skip_overrides=False))
self.assertFalse(is_autoexec("/untrusted/", skip_overrides=False))
# Nothing is trusted by default, only the excluded paths are still checked.
prefs_filepaths.use_scripts_auto_execute = False
self.assertFalse(is_autoexec("/trusted/", skip_overrides=False))
self.assertFalse(is_autoexec("/untrusted/", skip_overrides=False))
self.assertTrue(is_autoexec("/trusted/", skip_overrides=True))
self.assertFalse(is_autoexec("/untrusted/", skip_overrides=True))
if __name__ == '__main__':
import sys
sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])
unittest.main()