"""Real import-machinery PEP 420 fixtures; no Kit imports or installed writes.""" import importlib import importlib.machinery import importlib.util import sys from pathlib import Path from types import ModuleType import pytest from test_contact_cycle_sim_evaluation_binding import binding, load @pytest.fixture def namespace(tmp_path, monkeypatch): for name in tuple(sys.modules): if name.split('.')[0] in ('isaaclab', 'isaaclab_physx', 'SkillFactory'): monkeypatch.delitem(sys.modules, name) isaac = tmp_path / 'IsaacLab' package = isaac / 'source/isaaclab/isaaclab' directory = package / 'ui' directory.mkdir(parents=True) (package / '__init__.py').write_text('') (directory / 'child.py').write_text('IDENTITY = 1\n') monkeypatch.setattr(binding, 'ISAAC_ROOT', isaac) monkeypatch.setattr(binding, 'ISAAC_PINS', {}) monkeypatch.syspath_prepend(str(package.parent)) # Register deletions before real imports so monkeypatch restores sys.modules. for name in ('isaaclab', 'isaaclab.ui', 'isaaclab.ui.child'): monkeypatch.setitem(sys.modules, name, None) del sys.modules[name] module = importlib.import_module('isaaclab.ui') importlib.import_module('isaaclab.ui.child') return module, directory def test_namespace_binds_real_loader_locations_and_imported_children(namespace, tmp_path): module, directory = namespace assert type(module.__loader__) is importlib.machinery.NamespaceLoader assert module.__file__ is None records = binding._imports(set(), tmp_path) record = records['isaaclab.ui'] assert record == { 'kind': 'pep420-namespace', 'spec_name': 'isaaclab.ui', 'origin': None, 'loader': 'importlib.machinery.NamespaceLoader', 'search_locations': [str(directory)], 'imported_child_files': {'isaaclab.ui.child': records['isaaclab.ui.child']}} assert records['isaaclab.ui.child']['sha256'] == binding.sha256((directory / 'child.py').read_bytes()) (directory / 'unimported.py').write_text('not bound\n') assert binding._imports(set(), tmp_path) == records (directory / 'child.py').write_text('IDENTITY = 2\n') assert binding._imports(set(), tmp_path) != records assert binding.FORMAT.endswith('.v5') # Frozen file-only helper remains strict (including callers in symbol checks). with pytest.raises(ValueError, match='no real __file__'): binding._module_file('isaaclab.ui') @pytest.mark.parametrize('change', [ 'name', 'origin', 'loader', 'spec_loader', 'spec_path', 'list_path', 'foreign_path', 'empty_path', 'file', 'package', 'regular_package', 'missing_dir', 'symlink', 'synthetic', 'forged_spec', 'alias', 'path_alias', 'child_elsewhere', 'parent_path', 'path_name', 'path_finder', ]) def test_namespace_rejects_broken_swapped_and_forged_identity(namespace, tmp_path, monkeypatch, change): module, directory = namespace if change == 'name': module.__spec__.name = 'other' elif change == 'origin': module.__spec__.origin = 'built-in' elif change == 'loader': module.__loader__ = object() elif change == 'spec_loader': module.__spec__.loader = object() elif change == 'spec_path': module.__spec__.submodule_search_locations = [str(directory)] elif change == 'list_path': module.__path__ = module.__spec__.submodule_search_locations = [str(directory)] module.__loader__._path = module.__path__ elif change in ('foreign_path', 'empty_path'): module.__path__._path[:] = [] if change == 'empty_path' else [str(tmp_path)] elif change == 'file': module.__file__ = '' elif change == 'package': module.__package__ = 'other' elif change == 'regular_package': (directory / '__init__.py').write_text('') elif change == 'missing_dir': directory.rename(directory.with_name('moved')) elif change == 'symlink': moved = directory.with_name('moved') directory.rename(moved) directory.symlink_to(moved, target_is_directory=True) elif change in ('synthetic', 'forged_spec'): forged = ModuleType('isaaclab.ui') if change == 'forged_spec': loader = importlib.machinery.NamespaceLoader('isaaclab.ui', [str(directory)], None) forged.__loader__ = loader forged.__spec__ = importlib.machinery.ModuleSpec('isaaclab.ui', loader, is_package=True) forged.__path__ = forged.__spec__.submodule_search_locations monkeypatch.setitem(sys.modules, 'isaaclab.ui', forged) elif change == 'alias': monkeypatch.setitem(sys.modules, 'foreign_alias', module) elif change == 'path_alias': forged = ModuleType('foreign_alias') forged.__path__ = [str(directory)] monkeypatch.setitem(sys.modules, 'foreign_alias', forged) elif change == 'parent_path': sys.modules['isaaclab'].__path__ = [str(tmp_path)] elif change == 'path_name': module.__path__._name = 'other' elif change == 'path_finder': module.__path__._path_finder = lambda *_: None elif change == 'child_elsewhere': path = directory.parent / 'elsewhere.py' path.write_text('') load('isaaclab.ui.child', path, monkeypatch) with pytest.raises((ValueError, OSError)): binding._imports(set(), tmp_path) def test_namespace_unapproved_root_and_fileless_provider_rejected(namespace, tmp_path, monkeypatch): module, _ = namespace monkeypatch.setitem(sys.modules, 'dynamic_provider', ModuleType('dynamic_provider')) with pytest.raises(ValueError, match='spec/loader'): binding._imports({'dynamic_provider'}, tmp_path) # A genuine namespace loader is insufficient without an approved source root. outside = tmp_path / 'foreign_ns' outside.mkdir() spec = importlib.machinery.PathFinder.find_spec('foreign_ns', [str(tmp_path)]) foreign = importlib.util.module_from_spec(spec) monkeypatch.setitem(sys.modules, 'foreign_ns', foreign) with pytest.raises(ValueError, match='no approved source root'): binding._imports({'foreign_ns'}, tmp_path) assert module.__file__ is None def test_import_diagnostics_aggregate_without_hashing_invalid_modules(namespace, tmp_path, monkeypatch): for name in ('isaaclab.bad_one', 'isaaclab.bad_two'): monkeypatch.setitem(sys.modules, name, ModuleType(name)) errors = [] records = binding._imports(set(), tmp_path, diagnostics=errors) assert {row['entry'] for row in errors} == {'isaaclab.bad_one', 'isaaclab.bad_two'} assert not ({'isaaclab.bad_one', 'isaaclab.bad_two'} & records.keys()) assert 'isaaclab.ui' in records def test_actual_installed_isaaclab_ui_read_only(monkeypatch): directory = binding.ISAAC_ROOT / 'source/isaaclab/isaaclab/ui' if not directory.is_dir(): pytest.skip('Pinned installation not present') assert not (directory / '__init__.py').exists() before = {str(p): binding.sha256(p.read_bytes()) for p in directory.rglob('*.py')} # PathFinder and namespace exec_module execute no installed package code. parent_spec = importlib.util.spec_from_file_location('isaaclab', directory.parent / '__init__.py') monkeypatch.setitem(sys.modules, 'isaaclab', importlib.util.module_from_spec(parent_spec)) spec = importlib.machinery.PathFinder.find_spec('isaaclab.ui', [str(directory.parent)]) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) monkeypatch.setitem(sys.modules, 'isaaclab.ui', module) record = binding._namespace_identity('isaaclab.ui', Path.cwd()) assert record['search_locations'] == [str(directory)] assert record['imported_child_files'] == {} # not an entire-tree binding claim assert before == {str(p): binding.sha256(p.read_bytes()) for p in directory.rglob('*.py')}