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

49 statements  

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

1import os 1abcdefgh

2import subprocess 1abcdefgh

3import sys 1abcdefgh

4from pathlib import Path 1abcdefgh

5 

6from docs_src.typer_app import tutorial001_py39 as mod 1abcdefgh

7 

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

9 

10 

11@needs_bash 1abcdefgh

12@needs_linux 1abcdefgh

13def test_show_completion(): 1abcdefgh

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_PY39.PY_COMPLETE=complete_bash" in result.stdout 1abc

25 

26 

27@needs_bash 1abcdefgh

28@needs_linux 1abcdefgh

29@requires_completion_permission 1abcdefgh

30def test_install_completion(): 1abcdefgh

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_py39.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(): 1abcdefgh

54 result = subprocess.run( 1abcdefgh

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

56 capture_output=True, 

57 encoding="utf-8", 

58 env={ 

59 **os.environ, 

60 "_TUTORIAL001_PY39.PY_COMPLETE": "sourcebash", 

61 }, 

62 ) 

63 assert result.returncode != 0 1abcdefgh

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

65 

66 

67def test_completion_source_bash(): 1abcdefgh

68 result = subprocess.run( 1abcdefgh

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

70 capture_output=True, 

71 encoding="utf-8", 

72 env={ 

73 **os.environ, 

74 "_TUTORIAL001_PY39.PY_COMPLETE": "source_bash", 

75 }, 

76 ) 

77 assert ( 1abcdefgh

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

79 in result.stdout 

80 ) 

81 

82 

83def test_completion_source_invalid_shell(): 1abcdefgh

84 result = subprocess.run( 1abcdefgh

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

86 capture_output=True, 

87 encoding="utf-8", 

88 env={ 

89 **os.environ, 

90 "_TUTORIAL001_PY39.PY_COMPLETE": "source_xxx", 

91 }, 

92 ) 

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

94 

95 

96def test_completion_source_invalid_instruction(): 1abcdefgh

97 result = subprocess.run( 1abcdefgh

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

99 capture_output=True, 

100 encoding="utf-8", 

101 env={ 

102 **os.environ, 

103 "_TUTORIAL001_PY39.PY_COMPLETE": "explode_bash", 

104 }, 

105 ) 

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

107 

108 

109def test_completion_source_zsh(): 1abcdefgh

110 result = subprocess.run( 1abcdefgh

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

112 capture_output=True, 

113 encoding="utf-8", 

114 env={ 

115 **os.environ, 

116 "_TUTORIAL001_PY39.PY_COMPLETE": "source_zsh", 

117 }, 

118 ) 

119 assert "compdef _tutorial001_py39py_completion tutorial001_py39.py" in result.stdout 1abcdefgh

120 

121 

122def test_completion_source_fish(): 1abcdefgh

123 result = subprocess.run( 1abcdefgh

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

125 capture_output=True, 

126 encoding="utf-8", 

127 env={ 

128 **os.environ, 

129 "_TUTORIAL001_PY39.PY_COMPLETE": "source_fish", 

130 }, 

131 ) 

132 assert "complete --command tutorial001_py39.py --no-files" in result.stdout 1abcdefgh

133 

134 

135def test_completion_source_powershell(): 1abcdefgh

136 result = subprocess.run( 1abcdefgh

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

138 capture_output=True, 

139 encoding="utf-8", 

140 env={ 

141 **os.environ, 

142 "_TUTORIAL001_PY39.PY_COMPLETE": "source_powershell", 

143 }, 

144 ) 

145 assert ( 1abcdefgh

146 "Register-ArgumentCompleter -Native -CommandName tutorial001_py39.py -ScriptBlock $scriptblock" 

147 in result.stdout 

148 ) 

149 

150 

151def test_completion_source_pwsh(): 1abcdefgh

152 result = subprocess.run( 1abcdefgh

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

154 capture_output=True, 

155 encoding="utf-8", 

156 env={ 

157 **os.environ, 

158 "_TUTORIAL001_PY39.PY_COMPLETE": "source_pwsh", 

159 }, 

160 ) 

161 assert ( 1abcdefgh

162 "Register-ArgumentCompleter -Native -CommandName tutorial001_py39.py -ScriptBlock $scriptblock" 

163 in result.stdout 

164 )