• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Tests for automatic discovery of modules"""
2import os
3
4import pytest
5
6from setuptools.discovery import FlatLayoutModuleFinder, ModuleFinder
7
8from .test_find_packages import ensure_files, has_symlink
9
10
11class TestModuleFinder:
12    def find(self, path, *args, **kwargs):
13        return set(ModuleFinder.find(str(path), *args, **kwargs))
14
15    EXAMPLES = {
16        # circumstance: (files, kwargs, expected_modules)
17        "simple_folder": (
18            ["file.py", "other.py"],
19            {},  # kwargs
20            ["file", "other"],
21        ),
22        "exclude": (
23            ["file.py", "other.py"],
24            {"exclude": ["f*"]},
25            ["other"],
26        ),
27        "include": (
28            ["file.py", "fole.py", "other.py"],
29            {"include": ["f*"], "exclude": ["fo*"]},
30            ["file"],
31        ),
32        "invalid-name": (
33            ["my-file.py", "other.file.py"],
34            {},
35            []
36        )
37    }
38
39    @pytest.mark.parametrize("example", EXAMPLES.keys())
40    def test_finder(self, tmp_path, example):
41        files, kwargs, expected_modules = self.EXAMPLES[example]
42        ensure_files(tmp_path, files)
43        assert self.find(tmp_path, **kwargs) == set(expected_modules)
44
45    @pytest.mark.skipif(not has_symlink(), reason='Symlink support required')
46    def test_symlinked_packages_are_included(self, tmp_path):
47        src = "_myfiles/file.py"
48        ensure_files(tmp_path, [src])
49        os.symlink(tmp_path / src, tmp_path / "link.py")
50        assert self.find(tmp_path) == {"link"}
51
52
53class TestFlatLayoutModuleFinder:
54    def find(self, path, *args, **kwargs):
55        return set(FlatLayoutModuleFinder.find(str(path)))
56
57    EXAMPLES = {
58        # circumstance: (files, expected_modules)
59        "hidden-files": (
60            [".module.py"],
61            []
62        ),
63        "private-modules": (
64            ["_module.py"],
65            []
66        ),
67        "common-names": (
68            ["setup.py", "conftest.py", "test.py", "tests.py", "example.py", "mod.py"],
69            ["mod"]
70        ),
71        "tool-specific": (
72            ["tasks.py", "fabfile.py", "noxfile.py", "dodo.py", "manage.py", "mod.py"],
73            ["mod"]
74        )
75    }
76
77    @pytest.mark.parametrize("example", EXAMPLES.keys())
78    def test_unwanted_files_not_included(self, tmp_path, example):
79        files, expected_modules = self.EXAMPLES[example]
80        ensure_files(tmp_path, files)
81        assert self.find(tmp_path) == set(expected_modules)
82