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

80 statements  

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

1import os 1abcfghde

2import subprocess 1abcfghde

3import sys 1abcfghde

4from pathlib import Path 1abcfghde

5from unittest import mock 1abcfghde

6 

7import shellingham 1abcfghde

8from typer.testing import CliRunner 1abcfghde

9 

10from docs_src.typer_app import tutorial001_py39 as mod 1abcfghde

11 

12from ..utils import requires_completion_permission 1abcfghde

13 

14runner = CliRunner() 1abcfghde

15app = mod.app 1abcfghde

16 

17 

18@requires_completion_permission 1abcfghde

19def test_completion_install_no_shell(): 1abcfghde

20 result = subprocess.run( 1abcfghde

21 [sys.executable, "-m", "coverage", "run", mod.__file__, "--install-completion"], 

22 capture_output=True, 

23 encoding="utf-8", 

24 env={ 

25 **os.environ, 

26 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

27 }, 

28 ) 

29 assert "Option '--install-completion' requires an argument" in result.stderr 1abcfghde

30 

31 

32@requires_completion_permission 1abcfghde

33def test_completion_install_bash(): 1abcfghde

34 bash_completion_path: Path = Path.home() / ".bashrc" 1abcfghde

35 text = "" 1abcfghde

36 if bash_completion_path.is_file(): 1abcfghde

37 text = bash_completion_path.read_text() 1abcde

38 result = subprocess.run( 1abcfghde

39 [ 

40 sys.executable, 

41 "-m", 

42 "coverage", 

43 "run", 

44 mod.__file__, 

45 "--install-completion", 

46 "bash", 

47 ], 

48 capture_output=True, 

49 encoding="utf-8", 

50 env={ 

51 **os.environ, 

52 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

53 }, 

54 ) 

55 new_text = bash_completion_path.read_text() 1abcfghde

56 bash_completion_path.write_text(text) 1abcfghde

57 install_source = Path(".bash_completions/tutorial001_py39.py.sh") 1abcfghde

58 assert str(install_source) not in text 1abcfghde

59 assert str(install_source) in new_text 1abcfghde

60 assert "completion installed in" in result.stdout 1abcfghde

61 assert "Completion will take effect once you restart the terminal" in result.stdout 1abcfghde

62 install_source_path = Path.home() / install_source 1abcfghde

63 assert install_source_path.is_file() 1abcfghde

64 install_content = install_source_path.read_text() 1abcfghde

65 install_source_path.unlink() 1abcfghde

66 assert ( 1abcfghde

67 "complete -o default -F _tutorial001_py39py_completion tutorial001_py39.py" 

68 in install_content 

69 ) 

70 

71 

72@requires_completion_permission 1abcfghde

73def test_completion_install_zsh(): 1abcfghde

74 completion_path: Path = Path.home() / ".zshrc" 1abcfghde

75 text = "" 1abcfghde

76 if not completion_path.is_file(): # pragma: no cover 1abcfghde

77 completion_path.write_text('echo "custom .zshrc"') 1abcfghde

78 if completion_path.is_file(): 1abcfghde

79 text = completion_path.read_text() 1abcfghde

80 result = subprocess.run( 1abcfghde

81 [ 

82 sys.executable, 

83 "-m", 

84 "coverage", 

85 "run", 

86 mod.__file__, 

87 "--install-completion", 

88 "zsh", 

89 ], 

90 capture_output=True, 

91 encoding="utf-8", 

92 env={ 

93 **os.environ, 

94 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

95 }, 

96 ) 

97 new_text = completion_path.read_text() 1abcfghde

98 completion_path.write_text(text) 1abcfghde

99 zfunc_fragment = "fpath+=~/.zfunc" 1abcfghde

100 assert zfunc_fragment in new_text 1abcfghde

101 assert "completion installed in" in result.stdout 1abcfghde

102 assert "Completion will take effect once you restart the terminal" in result.stdout 1abcfghde

103 install_source_path = Path.home() / ".zfunc/_tutorial001_py39.py" 1abcfghde

104 assert install_source_path.is_file() 1abcfghde

105 install_content = install_source_path.read_text() 1abcfghde

106 install_source_path.unlink() 1abcfghde

107 assert ( 1abcfghde

108 "compdef _tutorial001_py39py_completion tutorial001_py39.py" in install_content 

109 ) 

110 

111 

112@requires_completion_permission 1abcfghde

113def test_completion_install_fish(): 1abcfghde

114 script_path = Path(mod.__file__) 1abcfghde

115 completion_path: Path = ( 1abcfghde

116 Path.home() / f".config/fish/completions/{script_path.name}.fish" 

117 ) 

118 result = subprocess.run( 1abcfghde

119 [ 

120 sys.executable, 

121 "-m", 

122 "coverage", 

123 "run", 

124 mod.__file__, 

125 "--install-completion", 

126 "fish", 

127 ], 

128 capture_output=True, 

129 encoding="utf-8", 

130 env={ 

131 **os.environ, 

132 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

133 }, 

134 ) 

135 new_text = completion_path.read_text() 1abcfghde

136 completion_path.unlink() 1abcfghde

137 assert "complete --command tutorial001_py39.py" in new_text 1abcfghde

138 assert "completion installed in" in result.stdout 1abcfghde

139 assert "Completion will take effect once you restart the terminal" in result.stdout 1abcfghde

140 

141 

142@requires_completion_permission 1abcfghde

143def test_completion_install_powershell(): 1abcfghde

144 completion_path: Path = ( 1abcfghde

145 Path.home() / ".config/powershell/Microsoft.PowerShell_profile.ps1" 

146 ) 

147 completion_path_bytes = f"{completion_path}\n".encode("windows-1252") 1abcfghde

148 text = "" 1abcfghde

149 if completion_path.is_file(): # pragma: no cover 1abcfghde

150 text = completion_path.read_text() 

151 

152 with mock.patch.object( 1abcfghde

153 shellingham, "detect_shell", return_value=("pwsh", "/usr/bin/pwsh") 

154 ): 

155 with mock.patch.object( 1abcfghde

156 subprocess, 

157 "run", 

158 return_value=subprocess.CompletedProcess( 

159 ["pwsh"], returncode=0, stdout=completion_path_bytes 

160 ), 

161 ): 

162 result = runner.invoke(app, ["--install-completion"]) 1abcfghde

163 install_script = "Register-ArgumentCompleter -Native -CommandName mocked-typer-testing-app -ScriptBlock $scriptblock" 1abcfghde

164 parent: Path = completion_path.parent 1abcfghde

165 parent.mkdir(parents=True, exist_ok=True) 1abcfghde

166 completion_path.write_text(install_script) 1abcfghde

167 new_text = completion_path.read_text() 1abcfghde

168 completion_path.write_text(text) 1abcfghde

169 assert install_script not in text 1abcfghde

170 assert install_script in new_text 1abcfghde

171 assert "completion installed in" in result.stdout 1abcfghde

172 assert "Completion will take effect once you restart the terminal" in result.stdout 1abcfghde