test_find_py_modules.py 2.3 KB

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