Coverage for tests / test_completion / test_completion_complete.py: 100%

53 statements  

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

1import importlib 1abcdefgh

2import os 1abcdefgh

3import subprocess 1abcdefgh

4import sys 1abcdefgh

5from pathlib import Path 1abcdefgh

6from types import ModuleType 1abcdefgh

7 

8import pytest 1abcdefgh

9 

10 

11@pytest.fixture( 1abcdefgh

12 name="mod", 

13 params=[ 

14 pytest.param("tutorial001_py39"), 

15 pytest.param("tutorial001_an_py39"), 

16 ], 

17) 

18def get_mod(request: pytest.FixtureRequest) -> ModuleType: 1abcdefgh

19 module_name = f"docs_src.commands.help.{request.param}" 1abcdefgh

20 mod = importlib.import_module(module_name) 1abcdefgh

21 return mod 1abcdefgh

22 

23 

24def test_completion_complete_subcommand_bash(mod: ModuleType): 1abcdefgh

25 file_name = Path(mod.__file__).name 1abcdefgh

26 result = subprocess.run( 1abcdefgh

27 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

28 capture_output=True, 

29 encoding="utf-8", 

30 env={ 

31 **os.environ, 

32 f"_{file_name.upper()}_COMPLETE": "complete_bash", 

33 "COMP_WORDS": f"{file_name} del", 

34 "COMP_CWORD": "1", 

35 }, 

36 ) 

37 print(result) 1abcdefgh

38 assert "delete\ndelete-all" in result.stdout 1abcdefgh

39 

40 

41def test_completion_complete_subcommand_bash_invalid(mod: ModuleType): 1abcdefgh

42 file_name = Path(mod.__file__).name 1abcdefgh

43 result = subprocess.run( 1abcdefgh

44 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

45 capture_output=True, 

46 encoding="utf-8", 

47 env={ 

48 **os.environ, 

49 f"_{file_name.upper()}_COMPLETE": "complete_bash", 

50 "COMP_WORDS": f"{file_name} del", 

51 "COMP_CWORD": "42", 

52 }, 

53 ) 

54 assert "create\ndelete\ndelete-all\ninit" in result.stdout 1abcdefgh

55 

56 

57def test_completion_complete_subcommand_zsh(mod: ModuleType): 1abcdefgh

58 file_name = Path(mod.__file__).name 1abcdefgh

59 result = subprocess.run( 1abcdefgh

60 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

61 capture_output=True, 

62 encoding="utf-8", 

63 env={ 

64 **os.environ, 

65 f"_{file_name.upper()}_COMPLETE": "complete_zsh", 

66 "_TYPER_COMPLETE_ARGS": f"{file_name} del", 

67 }, 

68 ) 

69 assert ( 1abcdefgh

70 """_arguments '*: :(("delete":"Delete a user with USERNAME."\n""" 

71 """\"delete-all":"Delete ALL users in the database."))'""" 

72 ) in result.stdout 

73 

74 

75def test_completion_complete_subcommand_zsh_files(mod: ModuleType): 1abcdefgh

76 file_name = Path(mod.__file__).name 1abcdefgh

77 result = subprocess.run( 1abcdefgh

78 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

79 capture_output=True, 

80 encoding="utf-8", 

81 env={ 

82 **os.environ, 

83 f"_{file_name.upper()}_COMPLETE": "complete_zsh", 

84 "_TYPER_COMPLETE_ARGS": f"{file_name} delete ", 

85 }, 

86 ) 

87 assert ("_files") in result.stdout 1abcdefgh

88 

89 

90def test_completion_complete_subcommand_fish(mod: ModuleType): 1abcdefgh

91 file_name = Path(mod.__file__).name 1abcdefgh

92 result = subprocess.run( 1abcdefgh

93 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

94 capture_output=True, 

95 encoding="utf-8", 

96 env={ 

97 **os.environ, 

98 f"_{file_name.upper()}_COMPLETE": "complete_fish", 

99 "_TYPER_COMPLETE_ARGS": f"{file_name} del", 

100 "_TYPER_COMPLETE_FISH_ACTION": "get-args", 

101 }, 

102 ) 

103 assert ( 1abcdefgh

104 "delete\tDelete a user with USERNAME.\ndelete-all\tDelete ALL users in the database." 

105 in result.stdout 

106 ) 

107 

108 

109def test_completion_complete_subcommand_fish_should_complete(mod: ModuleType): 1abcdefgh

110 file_name = Path(mod.__file__).name 1abcdefgh

111 result = subprocess.run( 1abcdefgh

112 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

113 capture_output=True, 

114 encoding="utf-8", 

115 env={ 

116 **os.environ, 

117 f"_{file_name.upper()}_COMPLETE": "complete_fish", 

118 "_TYPER_COMPLETE_ARGS": f"{file_name} del", 

119 "_TYPER_COMPLETE_FISH_ACTION": "is-args", 

120 }, 

121 ) 

122 assert result.returncode == 0 1abcdefgh

123 

124 

125def test_completion_complete_subcommand_fish_should_complete_no(mod: ModuleType): 1abcdefgh

126 file_name = Path(mod.__file__).name 1abcdefgh

127 result = subprocess.run( 1abcdefgh

128 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

129 capture_output=True, 

130 encoding="utf-8", 

131 env={ 

132 **os.environ, 

133 f"_{file_name.upper()}_COMPLETE": "complete_fish", 

134 "_TYPER_COMPLETE_ARGS": f"{file_name} delete ", 

135 "_TYPER_COMPLETE_FISH_ACTION": "is-args", 

136 }, 

137 ) 

138 assert result.returncode != 0 1abcdefgh

139 

140 

141def test_completion_complete_subcommand_powershell(mod: ModuleType): 1abcdefgh

142 file_name = Path(mod.__file__).name 1abcdefgh

143 result = subprocess.run( 1abcdefgh

144 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

145 capture_output=True, 

146 encoding="utf-8", 

147 env={ 

148 **os.environ, 

149 f"_{file_name.upper()}_COMPLETE": "complete_powershell", 

150 "_TYPER_COMPLETE_ARGS": f"{file_name} del", 

151 }, 

152 ) 

153 assert ( 1abcdefgh

154 "delete:::Delete a user with USERNAME.\ndelete-all:::Delete ALL users in the database." 

155 ) in result.stdout 

156 

157 

158def test_completion_complete_subcommand_pwsh(mod: ModuleType): 1abcdefgh

159 file_name = Path(mod.__file__).name 1abcdefgh

160 result = subprocess.run( 1abcdefgh

161 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

162 capture_output=True, 

163 encoding="utf-8", 

164 env={ 

165 **os.environ, 

166 f"_{file_name.upper()}_COMPLETE": "complete_pwsh", 

167 "_TYPER_COMPLETE_ARGS": f"{file_name} del", 

168 }, 

169 ) 

170 assert ( 1abcdefgh

171 "delete:::Delete a user with USERNAME.\ndelete-all:::Delete ALL users in the database." 

172 ) in result.stdout 

173 

174 

175def test_completion_complete_subcommand_noshell(mod: ModuleType): 1abcdefgh

176 file_name = Path(mod.__file__).name 1abcdefgh

177 result = subprocess.run( 1abcdefgh

178 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

179 capture_output=True, 

180 encoding="utf-8", 

181 env={ 

182 **os.environ, 

183 f"_{file_name.upper()}_COMPLETE": "complete_noshell", 

184 "_TYPER_COMPLETE_ARGS": f"{file_name} del", 

185 }, 

186 ) 

187 assert ("") in result.stdout 1abcdefgh