Coverage for tests\test_packagelister.py: 100%

1 import pytest

2 from pathier import Pathier, Pathish

3

4 root = Pathier(__file__).parent

5

6 from packagelister import packagelister

7

8# List of packages imported by packagelister.py

9 builtins = ["ast", "importlib", "sys", "dataclasses"]

10 third_partys = ["pathier", "printbuddies", "typing_extensions"]

11 imports = builtins + third_partys

12 num_packages = len(imports)

13 test_path = root.parent / "src" / "packagelister" / "packagelister.py"

14

15

16 def test__is_builtin():

17 assert packagelister.is_builtin("sys")

18 assert not packagelister.is_builtin("pathier")

19

20

21 def test__Package():

22 package = packagelister.Package.from_name("pathier")

23 assert package.name == "pathier"

24 assert package.distribution_name == "pathier"

25 assert package.version

26 assert not package.builtin

27 assert (

28 package.format_requirement("==")

29 == f"{package.distribution_name}=={package.version}"

30 )

31

32

33 def test__get_package_names_from_source():

34 file = test_path

35 package_names = packagelister.get_package_names_from_source(file.read_text())

36 assert len(package_names) == num_packages

37 for package in imports:

38 assert package in package_names

39

40

41 def test__packagelister_scan_file():

42 file = test_path

43 scanned_file = packagelister.scan_file(file)

44 assert len(scanned_file.packages) == num_packages

45 for package in scanned_file.packages.names:

46 assert package in imports

47 for package in scanned_file.packages.builtin.names:

48 assert package in builtins

49 for package in scanned_file.packages.third_party.names:

50 assert package in third_partys

51

52

53 def test__packagelister_scan_dir():

54 path = root.parent

55 project = packagelister.scan_dir(path)

56 files = path.rglob("*.py")

57 assert all(file.path in files for file in project.files)

58 assert len(project.requirements) == len(third_partys + ["pytest"])

59 print(project.get_formatted_requirements("=="))

60 assert project.get_formatted_requirements() == sorted(third_partys + ["pytest"])

61 print(project.get_files_by_package())