Coverage for tests / test_docs_cleanup.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-11-18 11:12 +0000

1from pathlib import Path 1abcdefghij

2 

3import pytest 1abcdefghij

4 

5from scripts.docs import remove_unused_docs_src 1abcdefghij

6 

7 

8def create_files(paths: set[str]) -> None: 1abcdefghij

9 for path_str in paths: 1abcdefghij

10 path = Path(path_str) 1abcdefghij

11 path.parent.mkdir(parents=True, exist_ok=True) 1abcdefghij

12 path.write_text("# Example code\n", encoding="utf-8") 1abcdefghij

13 

14 

15def include_file(path: str) -> None: 1abcdefghij

16 doc = Path("docs/tutorial.md") 1abcdefghij

17 doc.parent.mkdir(parents=True, exist_ok=True) 1abcdefghij

18 doc.write_text("{* " + path + " *}\n", encoding="utf-8") 1abcdefghij

19 

20 

21@pytest.mark.parametrize( 1abcdefghij

22 "names", 

23 [ 

24 ("tutorial001", "tutorial001_non_an"), 

25 ("tutorial001", "tutorial001_an"), 

26 ("tutorial001_py310", "tutorial001_non_an_py310"), 

27 ("tutorial001_py310", "tutorial001_an_py310"), 

28 ( 

29 "tutorial001_py310", 

30 "tutorial001_an_py310", 

31 "tutorial001_py314", 

32 "tutorial001_an_py314", 

33 ), 

34 ( 

35 "tutorial001_py311", 

36 "tutorial001_non_an_py311", 

37 "tutorial001_py314", 

38 "tutorial001_non_an_py314", 

39 ), 

40 ("tutorial001_py311", "tutorial001_py314"), 

41 ("tutorial_another", "tutorial_another_non_an"), 

42 ], 

43) 

44@pytest.mark.parametrize("preferred", [0, 1]) 1abcdefghij

45@pytest.mark.parametrize("topic", ["options", "arguments/default"]) 1abcdefghij

46def test_keep_file_variants( 1abcdefghij

47 tmp_path: Path, 

48 monkeypatch: pytest.MonkeyPatch, 

49 names: tuple[str, ...], 

50 preferred: int, 

51 topic: str, 

52) -> None: 

53 monkeypatch.chdir(tmp_path) 1abcdefghij

54 kept = {f"docs_src/{topic}/{name}.py" for name in names} 1abcdefghij

55 unused = { 1abcdefghij

56 f"docs_src/{topic}/tutorial002.py", 

57 f"docs_src/{topic}/tutorial001_extra.py", 

58 } 

59 create_files(kept | unused) 1abcdefghij

60 include_file(f"docs_src/{topic}/{names[preferred]}.py") 1abcdefghij

61 

62 remove_unused_docs_src() 1abcdefghij

63 

64 assert {path.as_posix() for path in Path("docs_src").rglob("*.py")} == kept 1abcdefghij

65 

66 

67@pytest.mark.parametrize( 1abcdefghij

68 ("topic", "names", "unused_name"), 

69 [ 

70 ("one_file_per_command", ("app", "app_non_an"), "app2"), 

71 ("one_file_per_command", ("app", "app_an"), "app2"), 

72 ( 

73 "testing", 

74 ("app02_py310", "app02_an_py310", "app02_py314", "app02_an_py314"), 

75 "app03", 

76 ), 

77 ( 

78 "testing", 

79 ( 

80 "app02_py311", 

81 "app02_non_an_py311", 

82 "app02_py314", 

83 "app02_non_an_py314", 

84 ), 

85 "app03", 

86 ), 

87 ( 

88 "subcommands", 

89 ("tutorial001_py311", "tutorial001_py314"), 

90 "tutorial002", 

91 ), 

92 ( 

93 "subcommands/nested", 

94 ("tutorial001", "tutorial001_non_an"), 

95 "tutorial002", 

96 ), 

97 ], 

98) 

99@pytest.mark.parametrize("preferred", [0, 1]) 1abcdefghij

100@pytest.mark.parametrize("included", ["main.py", "users/items.py", "__init__.py"]) 1abcdefghij

101def test_keep_entire_variant_packages( 1abcdefghij

102 tmp_path: Path, 

103 monkeypatch: pytest.MonkeyPatch, 

104 names: tuple[str, ...], 

105 preferred: int, 

106 included: str, 

107 topic: str, 

108 unused_name: str, 

109) -> None: 

110 monkeypatch.chdir(tmp_path) 1abcdefghij

111 kept = { 1abcdefghij

112 f"docs_src/{topic}/{name}/{module}" 

113 for name in names 

114 for module in ( 

115 "__init__.py", 

116 "main.py", 

117 "config.py", 

118 "users/items.py", 

119 "users/utils.py", 

120 "empty_package/__init__.py", 

121 ) 

122 } 

123 kept.add(f"docs_src/{topic}/{names[1]}/variant_only.py") 1abcdefghij

124 unused = { 1abcdefghij

125 f"docs_src/{topic}/{unused_name}/main.py", 

126 f"docs_src/{topic}/{unused_name}/__init__.py", 

127 f"docs_src/{topic}/{unused_name}/empty_package/__init__.py", 

128 f"docs_src/{topic}/{unused_name}/users/items.py", 

129 } 

130 create_files(kept | unused) 1abcdefghij

131 include_file(f"docs_src/{topic}/{names[preferred]}/{included}") 1abcdefghij

132 

133 remove_unused_docs_src() 1abcdefghij

134 

135 assert {path.as_posix() for path in Path("docs_src").rglob("*.py")} == kept 1abcdefghij

136 assert not Path(f"docs_src/{topic}/{unused_name}").exists() 1abcdefghij