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

76 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 18:26 +0000

1import os 1habfgcde

2import subprocess 1habfgcde

3import sys 1habfgcde

4from pathlib import Path 1habfgcde

5from unittest import mock 1habfgcde

6 

7import shellingham 1habfgcde

8import typer 1habfgcde

9from typer.testing import CliRunner 1habfgcde

10 

11from docs_src.commands.index import tutorial001 as mod 1habfgcde

12 

13runner = CliRunner() 1habfgcde

14app = typer.Typer() 1habfgcde

15app.command()(mod.main) 1habfgcde

16 

17 

18def test_completion_install_no_shell(): 1habfgcde

19 result = subprocess.run( 1habfgcde

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

21 capture_output=True, 

22 encoding="utf-8", 

23 env={ 

24 **os.environ, 

25 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

26 }, 

27 ) 

28 assert "Option '--install-completion' requires an argument" in result.stderr 1habfgcde

29 

30 

31def test_completion_install_bash(): 1habfgcde

32 bash_completion_path: Path = Path.home() / ".bashrc" 1habfgcde

33 text = "" 1habfgcde

34 if bash_completion_path.is_file(): 1habfgcde

35 text = bash_completion_path.read_text() 1habcde

36 result = subprocess.run( 1habfgcde

37 [ 

38 sys.executable, 

39 "-m", 

40 "coverage", 

41 "run", 

42 mod.__file__, 

43 "--install-completion", 

44 "bash", 

45 ], 

46 capture_output=True, 

47 encoding="utf-8", 

48 env={ 

49 **os.environ, 

50 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

51 }, 

52 ) 

53 new_text = bash_completion_path.read_text() 1habfgcde

54 bash_completion_path.write_text(text) 1habfgcde

55 install_source = Path(".bash_completions/tutorial001.py.sh") 1habfgcde

56 assert str(install_source) not in text 1habfgcde

57 assert str(install_source) in new_text 1habfgcde

58 assert "completion installed in" in result.stdout 1habfgcde

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

60 install_source_path = Path.home() / install_source 1habfgcde

61 assert install_source_path.is_file() 1habfgcde

62 install_content = install_source_path.read_text() 1habfgcde

63 install_source_path.unlink() 1habfgcde

64 assert ( 1habfgcde

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

66 in install_content 

67 ) 

68 

69 

70def test_completion_install_zsh(): 1habfgcde

71 completion_path: Path = Path.home() / ".zshrc" 1habfgcde

72 text = "" 1habfgcde

73 if not completion_path.is_file(): # pragma: no cover 1habfgcde

74 completion_path.write_text('echo "custom .zshrc"') 1habfgcde

75 if completion_path.is_file(): 1habfgcde

76 text = completion_path.read_text() 1habfgcde

77 result = subprocess.run( 1habfgcde

78 [ 

79 sys.executable, 

80 "-m", 

81 "coverage", 

82 "run", 

83 mod.__file__, 

84 "--install-completion", 

85 "zsh", 

86 ], 

87 capture_output=True, 

88 encoding="utf-8", 

89 env={ 

90 **os.environ, 

91 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

92 }, 

93 ) 

94 new_text = completion_path.read_text() 1habfgcde

95 completion_path.write_text(text) 1habfgcde

96 zfunc_fragment = "fpath+=~/.zfunc" 1habfgcde

97 assert zfunc_fragment in new_text 1habfgcde

98 assert "completion installed in" in result.stdout 1habfgcde

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

100 install_source_path = Path.home() / ".zfunc/_tutorial001.py" 1habfgcde

101 assert install_source_path.is_file() 1habfgcde

102 install_content = install_source_path.read_text() 1habfgcde

103 install_source_path.unlink() 1habfgcde

104 assert "compdef _tutorial001py_completion tutorial001.py" in install_content 1habfgcde

105 

106 

107def test_completion_install_fish(): 1habfgcde

108 script_path = Path(mod.__file__) 1habfgcde

109 completion_path: Path = ( 1abfgcde

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

111 ) 

112 result = subprocess.run( 1habfgcde

113 [ 

114 sys.executable, 

115 "-m", 

116 "coverage", 

117 "run", 

118 mod.__file__, 

119 "--install-completion", 

120 "fish", 

121 ], 

122 capture_output=True, 

123 encoding="utf-8", 

124 env={ 

125 **os.environ, 

126 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

127 }, 

128 ) 

129 new_text = completion_path.read_text() 1habfgcde

130 completion_path.unlink() 1habfgcde

131 assert "complete --command tutorial001.py" in new_text 1habfgcde

132 assert "completion installed in" in result.stdout 1habfgcde

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

134 

135 

136def test_completion_install_powershell(): 1habfgcde

137 completion_path: Path = ( 1abfgcde

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

139 ) 

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

141 text = "" 1habfgcde

142 if completion_path.is_file(): # pragma: no cover 1habfgcde

143 text = completion_path.read_text() 

144 

145 with mock.patch.object( 1habfgcde

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

147 ): 

148 with mock.patch.object( 1habfgcde

149 subprocess, 

150 "run", 

151 return_value=subprocess.CompletedProcess( 

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

153 ), 

154 ): 

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

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

157 parent: Path = completion_path.parent 1habfgcde

158 parent.mkdir(parents=True, exist_ok=True) 1habfgcde

159 completion_path.write_text(install_script) 1habfgcde

160 new_text = completion_path.read_text() 1habfgcde

161 completion_path.write_text(text) 1habfgcde

162 assert install_script not in text 1habfgcde

163 assert install_script in new_text 1habfgcde

164 assert "completion installed in" in result.stdout 1habfgcde

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