def test_from_local_tree_exclude_multilevel_name(local_tree_fs): assert Directory.from_local_tree( Path('/var/data'), exclude=['bar/__pycache__']) == Directory( path=None, entries={ "data": Directory( path='data/', entries={ "foo.py": File(('data', 'foo.py'), None, None), "foo.pyc": File(('data', 'foo.pyc'), None, None), "bar": Directory( path='data/bar/', entries={ "__init__.py": File( ('data', 'bar', '__init__.py'), None, None, ), }, ), }, ), }, )
def test_apply_config_toplevel_pkgtree_mismatch_warning(capsys, mocker): pkgtree = Directory( path=None, entries={ "foo.py": File(('foo.py'), None, None), "bar": Directory(path='bar/'), }, ) cfg = mocker.Mock( **{ "get_selected_checks.return_value": mocker.sentinel.SELECTED, "get_package_tree.return_value": pkgtree, }, ) cfg.toplevel = ["bar.py", "foo"] checker = WheelChecker() checker.apply_config(cfg) assert attr.asdict(checker, recurse=False) == { "selected": mocker.sentinel.SELECTED, "toplevel": ["bar.py", "foo"], "pkgtree": pkgtree, } captured = capsys.readouterr() assert captured.out == '' assert captured.err == ( 'Warning: --toplevel value does not match top level of --package/' '--src-dir file tree\n')
def test_trees_data_platlib(): """ Test the ``purelib_tree`` and ``platlib_tree`` attributes of a purelib wheel containing an empty purelib and a nonempty platlib """ whlcon = WheelContents.from_wheel(WHEEL_DIR/'MPC2860-0.3-py3-none-any.whl') assert whlcon.dist_info_dir == 'MPC2860-0.3.dist-info' assert whlcon.data_dir == 'MPC2860-0.3.data' assert whlcon.root_is_purelib is True assert whlcon.purelib_tree == Directory() assert whlcon.platlib_tree == Directory( path='MPC2860-0.3.data/platlib/', entries={ "_motion_2860.pyd": File( ('MPC2860-0.3.data', 'platlib', '_motion_2860.pyd'), 23040, 'sha256=kCCzhKz-ZujN2OI0gaeB1W1WHUaC_PdifaIwy4AG6uA', ), "MPC2860": Directory( path='MPC2860-0.3.data/platlib/MPC2860/', entries={ "MPC2860.dll": File( ('MPC2860-0.3.data', 'platlib', 'MPC2860', 'MPC2860.dll'), 548974, 'sha256=kOatNp1OqMU-bSzDgNtl9fg-cbQROp2pfnC_dAkD7zw', ), "MPC2860.lib": File( ('MPC2860-0.3.data', 'platlib', 'MPC2860', 'MPC2860.lib'), 29200, 'sha256=mW1wenQt2kDLZLZFNShxzTxI-lsFUvroi7nlOC-WnbA', ), "MPC2860CFG.txt": File( ('MPC2860-0.3.data', 'platlib', 'MPC2860', 'MPC2860CFG.txt'), 56, 'sha256=HVDtq-6twa1_nOtCBmuyrGB24EsnJ8PmoRYdqqBqFDI', ), "__init__.py": File( ('MPC2860-0.3.data', 'platlib', 'MPC2860', '__init__.py'), 27, 'sha256=PYXKN_qL7Q0aak8GkOsleT-8hAezfzcbRR3gxCSB7n8', ), "_motion_2860.pyd": File( ('MPC2860-0.3.data', 'platlib', 'MPC2860', '_motion_2860.pyd'), 23040, 'sha256=kCCzhKz-ZujN2OI0gaeB1W1WHUaC_PdifaIwy4AG6uA', ), "motion_2860.i": File( ('MPC2860-0.3.data', 'platlib', 'MPC2860', 'motion_2860.i'), 98, 'sha256=cwWUHTvpo_ZmeSg7pDcD0qUM4LJnuOH2gZS6Zz4f0p8', ), "motion_2860.py": File( ('MPC2860-0.3.data', 'platlib', 'MPC2860', 'motion_2860.py'), 4283, 'sha256=gn_45lgJZ1MQqvaBkjSnBJSbZUKmWGMy-1G8cln5-78', ), }, ), }, )
def test_validate_tree_dir_rows(purelib): whlcon = WheelContents( dist_info_dir='foo-1.0.dist-info', data_dir='foo-1.0.data', root_is_purelib=purelib, ) whlcon.add_record_rows([ [ 'foo-1.0.dist-info/METADATA', 'sha256=NVefY26xjCmYCQCnZaKUTNc5WaqZHDKxVde8l72cVOk', '950', ], ['empty/', '', ''], ]) whlcon.validate_tree() assert whlcon.filetree == Directory( path=None, entries={ "empty": Directory('empty/'), "foo-1.0.dist-info": Directory( path="foo-1.0.dist-info/", entries={ "METADATA": File( ('foo-1.0.dist-info', 'METADATA'), 950, 'sha256=NVefY26xjCmYCQCnZaKUTNc5WaqZHDKxVde8l72cVOk', ), }, ), }, ) assert whlcon.by_signature == { (950, 'sha256=NVefY26xjCmYCQCnZaKUTNc5WaqZHDKxVde8l72cVOk'): [whlcon.filetree["foo-1.0.dist-info"]["METADATA"]], }
def test_get_package_tree_package_path(mocker, package_omit, exclude): path = Path('foobar') cfg = Configuration(package_paths=[path], package_omit=package_omit) tree = Directory( path=None, entries={ "foobar": Directory( path="foobar/", entries={ "__init__.py": File( ('foobar', '__init__.py'), None, None, ), "foo.py": File( ('foobar', 'foo.py'), None, None, ), "bar.py": File( ('foobar', 'bar.py'), None, None, ), }, ), }, ) fltmock = mocker.patch.object(Directory, 'from_local_tree', return_value=tree) assert cfg.get_package_tree() == tree fltmock.assert_called_once_with(path, exclude=exclude)
def test_from_local_tree_directory_excluded(local_tree_fs): assert Directory.from_local_tree( Path('/var/data/bar'), exclude=['bar']) == Directory( path=None, entries={ "bar": Directory( path='bar/', entries={ "__init__.py": File( ('bar', '__init__.py'), None, None, ), "__pycache__": Directory( path='bar/__pycache__/', entries={ "__init__.cpython-36.pyc": File( ('bar', '__pycache__', '__init__.cpython-36.pyc'), None, None, ), }, ), }, ), }, )
def test_trees_data_purelib(): """ Test the ``purelib_tree`` and ``platlib_tree`` attributes of a platlib wheel containing a nonempty purelib and an empty platlib """ whlcon = WheelContents.from_wheel( WHEEL_DIR / 'mxnet_coreml_converter-0.1.0a7-cp27-cp27m-macosx_10_7_x86_64.whl') assert whlcon.dist_info_dir == 'mxnet_coreml_converter-0.1.0a7.dist-info' assert whlcon.data_dir == 'mxnet_coreml_converter-0.1.0a7.data' assert whlcon.root_is_purelib is False assert whlcon.purelib_tree == Directory( path='mxnet_coreml_converter-0.1.0a7.data/purelib/', entries={ "converter": Directory( path='mxnet_coreml_converter-0.1.0a7.data/purelib/converter/', entries={ "__init__.py": File( ('mxnet_coreml_converter-0.1.0a7.data', 'purelib', 'converter', '__init__.py'), 786, 'sha256=EWsEJdqMbVZOBLYEvcci5VgvFEul-nubLnmcMn5MJlI', ), "_add_pooling.py": File( ('mxnet_coreml_converter-0.1.0a7.data', 'purelib', 'converter', '_add_pooling.py'), 5499, 'sha256=2wyhzbR60jm1uoMqgb50o7QHb7k5S3qJx3Xh17musnE', ), "_layers.py": File( ('mxnet_coreml_converter-0.1.0a7.data', 'purelib', 'converter', '_layers.py'), 16133, 'sha256=5w5ddYvF9mJv0YFKKcg4AqQPae4dZDYwn2iNyihukhU', ), "_mxnet_converter.py": File( ('mxnet_coreml_converter-0.1.0a7.data', 'purelib', 'converter', '_mxnet_converter.py'), 8850, 'sha256=kPLQxZv_KpuwRO1_gaa8vxhSMoDgNDEVJ-ZVLK02L2E', ), "utils.py": File( ('mxnet_coreml_converter-0.1.0a7.data', 'purelib', 'converter', 'utils.py'), 2447, 'sha256=oa9WWamfXwfNVMcjD0MERh_GeiwFDP9n4gEockPDr7Y', ), }, ), }, ) assert whlcon.platlib_tree == Directory()
def test_add_entry_1level_file(): d = Directory() assert not bool(d) assert d.entries == {} assert d.files == {} assert "foo.py" not in d f = File.from_record_row(['foo.py', '', '']) d.add_entry(f) assert bool(d) assert d.entries == {"foo.py": f} assert d.files == {"foo.py": f} assert d.entries["foo.py"] is f assert d.files["foo.py"] is f assert d["foo.py"] is f assert "foo.py" in d
def test_add_entry_descendant(): foo = Directory("foo/") assert not bool(foo) assert foo.entries == {} assert foo.files == {} assert "bar.py" not in foo f = File.from_record_row(['foo/bar.py', '', '']) foo.add_entry(f) assert bool(foo) assert foo.entries == {"bar.py": f} assert foo.files == {"bar.py": f} assert foo.entries["bar.py"] is f assert foo.files["bar.py"] is f assert foo["bar.py"] is f assert "bar.py" in foo
def test_add_entry_nonempty_dir(): d = Directory() foo = Directory('foo/') foo.add_entry(File.from_record_row(['foo/bar.py', '', ''])) with pytest.raises(ValueError) as excinfo: d.add_entry(foo) assert str(excinfo.value) \ == 'Cannot add nonempty directory to directory tree'
def test_add_entry_2level_file(): d = Directory() assert not bool(d) assert d.entries == {} assert "foo" not in d f = File.from_record_row(['foo/bar.py', '', '']) d.add_entry(f) assert bool(d) assert d.entries == { "foo": Directory(path='foo/', entries={"bar.py": f}), } assert d.entries["foo"].entries["bar.py"] is f assert d["foo"]["bar.py"] is f assert "foo" in d assert "bar.py" in d["foo"]
def test_add_entry_1level_dir(): d = Directory() assert not bool(d) assert d.entries == {} assert d.subdirectories == {} assert "foo" not in d sd = Directory('foo/') d.add_entry(sd) assert bool(d) assert d.entries == {"foo": sd} assert d.subdirectories == {"foo": sd} assert d.entries["foo"] is sd assert d.subdirectories["foo"] is sd assert d["foo"] is sd assert "foo" in d assert sd.entries == {}
def test_apply_config_calls(mocker): pkgtree = Directory( path=None, entries={"TOPLEVEL": Directory(path='TOPLEVEL/')}, ) cfg = mocker.Mock( **{ "get_selected_checks.return_value": mocker.sentinel.SELECTED, "get_package_tree.return_value": pkgtree, }, ) cfg.toplevel = ["TOPLEVEL"] checker = WheelChecker() checker.apply_config(cfg) assert attr.asdict(checker, recurse=False) == { "selected": mocker.sentinel.SELECTED, "toplevel": ["TOPLEVEL"], "pkgtree": pkgtree, }
def test_from_local_tree_no_include_root(): assert Directory.from_local_tree( PROJECT_TREE, include_root=False) == Directory( path=None, entries={ "foo.py": File(('foo.py', ), None, None), "foo.pyc": File(('foo.pyc', ), None, None), "bar": Directory( path='bar/', entries={ "__init__.py": File( ('bar', '__init__.py'), None, None, ), "__pycache__": Directory( path='bar/__pycache__/', entries={ "__init__.cpython-36.pyc": File( ('bar', '__pycache__', '__init__.cpython-36.pyc'), None, None, ), }, ), }, ), "src": Directory( path='src/', entries={ ".placeholder": File(('src', '.placeholder'), None, None), }, ), }, )
def test_add_entry_known_dir(): d = Directory() f = File.from_record_row(['foo/bar.py', '', '']) d.add_entry(f) sd = Directory("foo/") d.add_entry(sd) assert d.entries == { "foo": Directory(path='foo/', entries={"bar.py": f}), } assert d.entries["foo"].entries["bar.py"] is f assert d["foo"]["bar.py"] is f
def test_trees_platlib(): """ Test the ``purelib_tree`` and ``platlib_tree`` attributes of a platlib wheel containing an empty purelib and a nonempty platlib """ whlcon = WheelContents.from_wheel(WHEEL_DIR / 'bcrypt-3.1.7-cp38-cp38-win_amd64.whl') assert whlcon.dist_info_dir == 'bcrypt-3.1.7.dist-info' assert whlcon.data_dir == 'bcrypt-3.1.7.data' assert whlcon.root_is_purelib is False assert whlcon.purelib_tree == Directory( path='bcrypt-3.1.7.data/purelib/', entries={}, ) assert whlcon.platlib_tree == Directory( path=None, entries={ "bcrypt": Directory( path="bcrypt/", entries={ "__about__.py": File( ('bcrypt', '__about__.py'), 1296, 'sha256=jDmr9vNGIReR9gvahrcAIvOGOFq6Zmo_afmjN1Beb5g', ), "__init__.py": File( ('bcrypt', '__init__.py'), 5497, 'sha256=sIAkb9VLIbn4fHq_8xO79ndQ1bZ5O8rDO8OSHa5-RYs', ), "_bcrypt.cp38-win_amd64.pyd": File( ('bcrypt', '_bcrypt.cp38-win_amd64.pyd'), 30208, 'sha256=ALdvcGdNK8O_Ux3njNosbQc_9_5W3zzvb1ShFQCPdDU', ), }, ), }, )
def test_from_local_tree(local_tree_fs): assert Directory.from_local_tree(Path('/var/data')) == Directory( path=None, entries={ "data": Directory( path='data/', entries={ "foo.py": File(('data', 'foo.py'), None, None), "foo.pyc": File(('data', 'foo.pyc'), None, None), "bar": Directory( path='data/bar/', entries={ "__init__.py": File( ('data', 'bar', '__init__.py'), None, None, ), "__pycache__": Directory( path='data/bar/__pycache__/', entries={ "__init__.cpython-36.pyc": File( ('data', 'bar', '__pycache__', '__init__.cpython-36.pyc'), None, None, ), }, ), }, ), }, ), }, )
def test_from_local_tree_exclude_multilevel_name(): assert Directory.from_local_tree( PROJECT_TREE, exclude=['bar/__pycache__']) == Directory( path=None, entries={ "project-tree": Directory( path='project-tree/', entries={ "foo.py": File(('project-tree', 'foo.py'), None, None), "foo.pyc": File(('project-tree', 'foo.pyc'), None, None), "bar": Directory( path='project-tree/bar/', entries={ "__init__.py": File( ('project-tree', 'bar', '__init__.py'), None, None, ), }, ), "src": Directory( path='project-tree/src/', entries={ ".placeholder": File( ('project-tree', 'src', '.placeholder'), None, None, ), }, ), }, ), }, )
def test_get_package_tree_package_path_and_src_dir(fs): fs.create_file('/usr/src/project/src/foo.py') fs.create_file('/usr/src/project/bar/__init__.py') fs.create_file('/usr/src/project/bar/quux.py') fs.create_file('/usr/src/project/bar/glarch.py') fs.cwd = '/usr/src/project' cfg = Configuration(package_paths=[Path('bar')], src_dirs=[Path('src')]) assert cfg.get_package_tree() == Directory( path=None, entries={ "foo.py": File(('foo.py', ), None, None), "bar": Directory( path='bar/', entries={ "__init__.py": File(('bar', '__init__.py'), None, None), "quux.py": File(('bar', 'quux.py'), None, None), "glarch.py": File(('bar', 'glarch.py'), None, None), }, ), }, )
def test_get_package_tree_package_path_and_src_dir(monkeypatch, tmp_path): create_file(tmp_path / 'src/foo.py') create_file(tmp_path / 'bar/__init__.py') create_file(tmp_path / 'bar/quux.py') create_file(tmp_path / 'bar/glarch.py') monkeypatch.chdir(tmp_path) cfg = Configuration(package_paths=[Path('bar')], src_dirs=[Path('src')]) assert cfg.get_package_tree() == Directory( path=None, entries={ "foo.py": File(('foo.py', ), None, None), "bar": Directory( path='bar/', entries={ "__init__.py": File(('bar', '__init__.py'), None, None), "quux.py": File(('bar', 'quux.py'), None, None), "glarch.py": File(('bar', 'glarch.py'), None, None), }, ), }, )
def test_from_local_tree_no_include_root(local_tree_fs): assert Directory.from_local_tree( Path('/var/data'), include_root=False) == Directory( path=None, entries={ "foo.py": File(('foo.py', ), None, None), "foo.pyc": File(('foo.pyc', ), None, None), "bar": Directory( path='bar/', entries={ "__init__.py": File( ('bar', '__init__.py'), None, None, ), "__pycache__": Directory( path='bar/__pycache__/', entries={ "__init__.cpython-36.pyc": File( ('bar', '__pycache__', '__init__.cpython-36.pyc'), None, None, ), }, ), }, ), }, )
def test_add_entry_in_known_dir(): d = Directory() f = File.from_record_row(['foo/bar.py', '', '']) d.add_entry(f) f2 = File.from_record_row(['foo/glarch.py', '', '']) d.add_entry(f2) assert d.entries == { "foo": Directory(path='foo/', entries={ "bar.py": f, "glarch.py": f2 }), } assert d.entries["foo"].entries["glarch.py"] is f2 assert d["foo"]["glarch.py"] is f2 assert list(d["foo"].entries.keys()) == ["bar.py", "glarch.py"]
def test_from_local_tree(): assert Directory.from_local_tree(PROJECT_TREE) == Directory( path=None, entries={ "project-tree": Directory( path='project-tree/', entries={ "foo.py": File(('project-tree', 'foo.py'), None, None), "foo.pyc": File(('project-tree', 'foo.pyc'), None, None), "bar": Directory( path='project-tree/bar/', entries={ "__init__.py": File( ('project-tree', 'bar', '__init__.py'), None, None, ), "__pycache__": Directory( path='project-tree/bar/__pycache__/', entries={ "__init__.cpython-36.pyc": File( ('project-tree', 'bar', '__pycache__', '__init__.cpython-36.pyc'), None, None, ), }, ), }, ), "src": Directory( path='project-tree/src/', entries={ ".placeholder": File( ('project-tree', 'src', '.placeholder'), None, None, ), }, ), }, ), }, )
def test_add_entry_two_1level_files(): d = Directory() assert not bool(d) assert d.entries == {} assert d.files == {} assert "bar.py" not in d f = File.from_record_row(['foo.py', '', '']) d.add_entry(f) f2 = File.from_record_row(['bar.py', '', '']) d.add_entry(f2) assert bool(d) assert d.entries == {"foo.py": f, "bar.py": f2} assert d.files == {"foo.py": f, "bar.py": f2} assert d.entries["bar.py"] is f2 assert d.files["bar.py"] is f2 assert d["bar.py"] is f2 assert "bar.py" in d assert list(d.entries.keys()) == ["foo.py", "bar.py"]
def test_from_local_tree_exclude_glob(): assert Directory.from_local_tree( PROJECT_TREE, exclude=['*.pyc', '.*']) == Directory( path=None, entries={ "project-tree": Directory( path='project-tree/', entries={ "foo.py": File(('project-tree', 'foo.py'), None, None), "bar": Directory( path='project-tree/bar/', entries={ "__init__.py": File( ('project-tree', 'bar', '__init__.py'), None, None, ), "__pycache__": Directory( path='project-tree/bar/__pycache__/', entries={}, ), }, ), "src": Directory( path='project-tree/src/', entries={}, ), }, ), }, )
def test_default_path(): d = Directory() assert d.path is None assert d.parts == ()
def test_from_local_tree_file_excluded(local_tree_fs): assert Directory.from_local_tree( Path('/var/data/foo.pyc'), exclude=['*.pyc']) == Directory( path=None, entries={"foo.pyc": File(('foo.pyc', ), None, None)}, )
def test_from_local_tree_nonexistent(local_tree_fs): with pytest.raises(FileNotFoundError) as excinfo: Directory.from_local_tree(Path('DNE')) assert excinfo.value.filename == 'DNE'
def test_from_local_tree_file(local_tree_fs): assert Directory.from_local_tree(Path('/var/data/foo.py')) == Directory( path=None, entries={"foo.py": File(('foo.py', ), None, None)}, )
def test_parts(path, expected): assert Directory(path).parts == expected