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

49 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-26 21:46 +0000

1import os 1abcdefg

2import subprocess 1abcdefg

3import sys 1abcdefg

4from pathlib import Path 1abcdefg

5 

6from docs_src.typer_app import tutorial001_py310 as mod 1abcdefg

7 

8from ..utils import needs_bash, needs_linux, requires_completion_permission 1abcdefg

9 

10 

11@needs_bash 1abcdefg

12@needs_linux 1abcdefg

13def test_show_completion(): 1abcdefg

14 result = subprocess.run( 1abc

15 [ 

16 "bash", 

17 "-c", 

18 f"'{sys.executable}' -m coverage run '{mod.__file__}' --show-completion", 

19 ], 

20 capture_output=True, 

21 encoding="utf-8", 

22 env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"}, 

23 ) 

24 assert "_TUTORIAL001_PY310.PY_COMPLETE=complete_bash" in result.stdout 1abc

25 

26 

27@needs_bash 1abcdefg

28@needs_linux 1abcdefg

29@requires_completion_permission 1abcdefg

30def test_install_completion(): 1abcdefg

31 bash_completion_path: Path = Path.home() / ".bashrc" 1abc

32 text = "" 1abc

33 if bash_completion_path.is_file(): # pragma: no cover 1abc

34 text = bash_completion_path.read_text() 1abc

35 result = subprocess.run( 1abc

36 [ 

37 "bash", 

38 "-c", 

39 f"'{sys.executable}' -m coverage run '{mod.__file__}' --install-completion", 

40 ], 

41 capture_output=True, 

42 encoding="utf-8", 

43 env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"}, 

44 ) 

45 new_text = bash_completion_path.read_text() 1abc

46 bash_completion_path.write_text(text) 1abc

47 assert "source" in new_text 1abc

48 assert str(Path(".bash_completions/tutorial001_py310.py.sh")) in new_text 1abc

49 assert "completion installed in" in result.stdout 1abc

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

51 

52 

53def test_completion_invalid_instruction(): 1abcdefg

54 result = subprocess.run( 1abcdefg

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

56 capture_output=True, 

57 encoding="utf-8", 

58 env={ 

59 **os.environ, 

60 "_TUTORIAL001_PY310.PY_COMPLETE": "sourcebash", 

61 }, 

62 ) 

63 assert result.returncode != 0 1abcdefg

64 assert "Invalid completion instruction." in result.stderr 1abcdefg

65 

66 

67def test_completion_source_bash(): 1abcdefg

68 result = subprocess.run( 1abcdefg

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

70 capture_output=True, 

71 encoding="utf-8", 

72 env={ 

73 **os.environ, 

74 "_TUTORIAL001_PY310.PY_COMPLETE": "source_bash", 

75 }, 

76 ) 

77 assert ( 1abcdefg

78 "complete -o default -F _tutorial001_py310py_completion tutorial001_py310.py" 

79 in result.stdout 

80 ) 

81 

82 

83def test_completion_source_invalid_shell(): 1abcdefg

84 result = subprocess.run( 1abcdefg

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

86 capture_output=True, 

87 encoding="utf-8", 

88 env={ 

89 **os.environ, 

90 "_TUTORIAL001_PY310.PY_COMPLETE": "source_xxx", 

91 }, 

92 ) 

93 assert "Shell xxx not supported." in result.stderr 1abcdefg

94 

95 

96def test_completion_source_invalid_instruction(): 1abcdefg

97 result = subprocess.run( 1abcdefg

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

99 capture_output=True, 

100 encoding="utf-8", 

101 env={ 

102 **os.environ, 

103 "_TUTORIAL001_PY310.PY_COMPLETE": "explode_bash", 

104 }, 

105 ) 

106 assert 'Completion instruction "explode" not supported.' in result.stderr 1abcdefg

107 

108 

109def test_completion_source_zsh(): 1abcdefg

110 result = subprocess.run( 1abcdefg

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

112 capture_output=True, 

113 encoding="utf-8", 

114 env={ 

115 **os.environ, 

116 "_TUTORIAL001_PY310.PY_COMPLETE": "source_zsh", 

117 }, 

118 ) 

119 assert ( 1abcdefg

120 "compdef _tutorial001_py310py_completion tutorial001_py310.py" in result.stdout 

121 ) 

122 

123 

124def test_completion_source_fish(): 1abcdefg

125 result = subprocess.run( 1abcdefg

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

127 capture_output=True, 

128 encoding="utf-8", 

129 env={ 

130 **os.environ, 

131 "_TUTORIAL001_PY310.PY_COMPLETE": "source_fish", 

132 }, 

133 ) 

134 assert "complete --command tutorial001_py310.py --no-files" in result.stdout 1abcdefg

135 

136 

137def test_completion_source_powershell(): 1abcdefg

138 result = subprocess.run( 1abcdefg

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

140 capture_output=True, 

141 encoding="utf-8", 

142 env={ 

143 **os.environ, 

144 "_TUTORIAL001_PY310.PY_COMPLETE": "source_powershell", 

145 }, 

146 ) 

147 assert ( 1abcdefg

148 "Register-ArgumentCompleter -Native -CommandName tutorial001_py310.py -ScriptBlock $scriptblock" 

149 in result.stdout 

150 ) 

151 

152 

153def test_completion_source_pwsh(): 1abcdefg

154 result = subprocess.run( 1abcdefg

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

156 capture_output=True, 

157 encoding="utf-8", 

158 env={ 

159 **os.environ, 

160 "_TUTORIAL001_PY310.PY_COMPLETE": "source_pwsh", 

161 }, 

162 ) 

163 assert ( 1abcdefg

164 "Register-ArgumentCompleter -Native -CommandName tutorial001_py310.py -ScriptBlock $scriptblock" 

165 in result.stdout 

166 )