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

80 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-19 20:29 +0000

1import os 1abcghidef

2import subprocess 1abcghidef

3import sys 1abcghidef

4from pathlib import Path 1abcghidef

5from unittest import mock 1abcghidef

6 

7import shellingham 1abcghidef

8from typer.testing import CliRunner 1abcghidef

9 

10from docs_src.typer_app import tutorial001 as mod 1abcghidef

11 

12from ..utils import requires_completion_permission 1abcghidef

13 

14runner = CliRunner() 1abcghidef

15app = mod.app 1abcghidef

16 

17 

18@requires_completion_permission 1abcghidef

19def test_completion_install_no_shell(): 1abcghidef

20 result = subprocess.run( 1abcghidef

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 1abcghidef

30 

31 

32@requires_completion_permission 1abcghidef

33def test_completion_install_bash(): 1abcghidef

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

35 text = "" 1abcghidef

36 if bash_completion_path.is_file(): 1abcghidef

37 text = bash_completion_path.read_text() 1abcdef

38 result = subprocess.run( 1abcghidef

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() 1abcghidef

56 bash_completion_path.write_text(text) 1abcghidef

57 install_source = Path(".bash_completions/tutorial001.py.sh") 1abcghidef

58 assert str(install_source) not in text 1abcghidef

59 assert str(install_source) in new_text 1abcghidef

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

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

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

63 assert install_source_path.is_file() 1abcghidef

64 install_content = install_source_path.read_text() 1abcghidef

65 install_source_path.unlink() 1abcghidef

66 assert ( 1abcghidef

67 "complete -o default -F _tutorial001py_completion tutorial001.py" 

68 in install_content 

69 ) 

70 

71 

72@requires_completion_permission 1abcghidef

73def test_completion_install_zsh(): 1abcghidef

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

75 text = "" 1abcghidef

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

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

78 if completion_path.is_file(): 1abcghidef

79 text = completion_path.read_text() 1abcghidef

80 result = subprocess.run( 1abcghidef

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() 1abcghidef

98 completion_path.write_text(text) 1abcghidef

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

100 assert zfunc_fragment in new_text 1abcghidef

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

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

103 install_source_path = Path.home() / ".zfunc/_tutorial001.py" 1abcghidef

104 assert install_source_path.is_file() 1abcghidef

105 install_content = install_source_path.read_text() 1abcghidef

106 install_source_path.unlink() 1abcghidef

107 assert "compdef _tutorial001py_completion tutorial001.py" in install_content 1abcghidef

108 

109 

110@requires_completion_permission 1abcghidef

111def test_completion_install_fish(): 1abcghidef

112 script_path = Path(mod.__file__) 1abcghidef

113 completion_path: Path = ( 1abcghidef

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

115 ) 

116 result = subprocess.run( 1abcghidef

117 [ 

118 sys.executable, 

119 "-m", 

120 "coverage", 

121 "run", 

122 mod.__file__, 

123 "--install-completion", 

124 "fish", 

125 ], 

126 capture_output=True, 

127 encoding="utf-8", 

128 env={ 

129 **os.environ, 

130 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

131 }, 

132 ) 

133 new_text = completion_path.read_text() 1abcghidef

134 completion_path.unlink() 1abcghidef

135 assert "complete --command tutorial001.py" in new_text 1abcghidef

136 assert "completion installed in" in result.stdout 1abcghidef

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

138 

139 

140@requires_completion_permission 1abcghidef

141def test_completion_install_powershell(): 1abcghidef

142 completion_path: Path = ( 1abcghidef

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

144 ) 

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

146 text = "" 1abcghidef

147 if completion_path.is_file(): # pragma: no cover 1abcghidef

148 text = completion_path.read_text() 

149 

150 with mock.patch.object( 1abcghidef

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

152 ): 

153 with mock.patch.object( 1abcghidef

154 subprocess, 

155 "run", 

156 return_value=subprocess.CompletedProcess( 

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

158 ), 

159 ): 

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

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

162 parent: Path = completion_path.parent 1abcghidef

163 parent.mkdir(parents=True, exist_ok=True) 1abcghidef

164 completion_path.write_text(install_script) 1abcghidef

165 new_text = completion_path.read_text() 1abcghidef

166 completion_path.write_text(text) 1abcghidef

167 assert install_script not in text 1abcghidef

168 assert install_script in new_text 1abcghidef

169 assert "completion installed in" in result.stdout 1abcghidef

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