|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | +import unittest |
| 4 | +import tempfile |
| 5 | +import shutil |
| 6 | + |
| 7 | +import src.namespace_pkgs |
| 8 | + |
| 9 | + |
| 10 | +class TempDir: |
| 11 | + def __init__(self): |
| 12 | + self.dir = tempfile.mkdtemp() |
| 13 | + |
| 14 | + def root(self): |
| 15 | + return self.dir |
| 16 | + |
| 17 | + def add_dir(self, rel_path): |
| 18 | + d = pathlib.Path(self.dir, rel_path) |
| 19 | + d.mkdir(parents=True) |
| 20 | + |
| 21 | + def add_file(self, rel_path, contents=None): |
| 22 | + f = pathlib.Path(self.dir, rel_path) |
| 23 | + f.parent.mkdir(parents=True, exist_ok=True) |
| 24 | + if contents: |
| 25 | + with open(f, "w") as writeable_f: |
| 26 | + writeable_f.write(contents) |
| 27 | + else: |
| 28 | + f.touch() |
| 29 | + |
| 30 | + def remove(self): |
| 31 | + shutil.rmtree(self.dir) |
| 32 | + |
| 33 | + |
| 34 | +class TestPkgResourcesStyleNamespacePackages(unittest.TestCase): |
| 35 | + def test_finds_correct_namespace_packages(self): |
| 36 | + directory = TempDir() |
| 37 | + directory.add_file("google/auth/__init__.py") |
| 38 | + directory.add_file("google/auth/foo.py") |
| 39 | + directory.add_file( |
| 40 | + "google_auth-1.8.2.dist-info/namespace_packages.txt", |
| 41 | + contents="google\n" |
| 42 | + ) |
| 43 | + |
| 44 | + expected = { |
| 45 | + f"{directory.root()}/google", |
| 46 | + } |
| 47 | + actual = src.namespace_pkgs.pkg_resources_style_namespace_packages(directory.root()) |
| 48 | + self.assertEqual(actual, expected) |
| 49 | + |
| 50 | + def test_empty_case(self): |
| 51 | + # Even though this directory contains directories with no __init__.py |
| 52 | + # it has an empty namespace_packages.txt file so no namespace packages |
| 53 | + # should be returned. |
| 54 | + directory = TempDir() |
| 55 | + directory.add_file("foo/bar/biz.py") |
| 56 | + directory.add_file("foo/bee/boo.py") |
| 57 | + directory.add_file("foo/buu/__init__.py") |
| 58 | + directory.add_file("foo/buu/bii.py") |
| 59 | + directory.add_file("foo-1.0.0.dist-info/namespace_packages.txt") |
| 60 | + |
| 61 | + actual = src.namespace_pkgs.pkg_resources_style_namespace_packages(directory.root()) |
| 62 | + self.assertEqual(actual, set()) |
| 63 | + |
| 64 | + def test_missing_namespace_pkgs_record_file(self): |
| 65 | + # Even though this directory contains directories with no __init__.py |
| 66 | + # it has no namespace_packages.txt file, so no namespace packages should |
| 67 | + # be found and returned. |
| 68 | + directory = TempDir() |
| 69 | + directory.add_file("foo/bar/biz.py") |
| 70 | + directory.add_file("foo/bee/boo.py") |
| 71 | + directory.add_file("foo/buu/__init__.py") |
| 72 | + directory.add_file("foo/buu/bii.py") |
| 73 | + directory.add_file("foo-1.0.0.dist-info/METADATA") |
| 74 | + directory.add_file("foo-1.0.0.dist-info/RECORD") |
| 75 | + |
| 76 | + actual = src.namespace_pkgs.pkg_resources_style_namespace_packages(directory.root()) |
| 77 | + self.assertEqual(actual, set()) |
| 78 | + |
| 79 | + |
| 80 | +class TestImplicitNamespacePackages(unittest.TestCase): |
| 81 | + def test_finds_correct_namespace_packages(self): |
| 82 | + directory = TempDir() |
| 83 | + directory.add_file("foo/bar/biz.py") |
| 84 | + directory.add_file("foo/bee/boo.py") |
| 85 | + directory.add_file("foo/buu/__init__.py") |
| 86 | + directory.add_file("foo/buu/bii.py") |
| 87 | + |
| 88 | + expected = { |
| 89 | + f"{directory.root()}/foo", |
| 90 | + f"{directory.root()}/foo/bar", |
| 91 | + f"{directory.root()}/foo/bee", |
| 92 | + } |
| 93 | + actual = src.namespace_pkgs.implicit_namespace_packages(directory.root()) |
| 94 | + self.assertEqual(actual, expected) |
| 95 | + |
| 96 | + def test_ignores_empty_directories(self): |
| 97 | + directory = TempDir() |
| 98 | + directory.add_file("foo/bar/biz.py") |
| 99 | + directory.add_dir("foo/cat") |
| 100 | + |
| 101 | + expected = { |
| 102 | + f"{directory.root()}/foo", |
| 103 | + f"{directory.root()}/foo/bar", |
| 104 | + } |
| 105 | + actual = src.namespace_pkgs.implicit_namespace_packages(directory.root()) |
| 106 | + self.assertEqual(actual, expected) |
| 107 | + |
| 108 | + def test_empty_case(self): |
| 109 | + directory = TempDir() |
| 110 | + directory.add_file("foo/__init__.py") |
| 111 | + directory.add_file("foo/bar/__init__.py") |
| 112 | + directory.add_file("foo/bar/biz.py") |
| 113 | + |
| 114 | + actual = src.namespace_pkgs.implicit_namespace_packages(directory.root()) |
| 115 | + self.assertEqual(actual, set()) |
0 commit comments