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

32 statements  

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

1import os 1abcdefghij

2import subprocess 1abcdefghij

3import sys 1abcdefghij

4from unittest import mock 1abcdefghij

5 

6import typer 1abcdefghij

7import typer.completion 1abcdefghij

8from typer.testing import CliRunner 1abcdefghij

9 

10from docs_src.typer_app import tutorial001 as mod 1abcdefghij

11 

12runner = CliRunner() 1abcdefghij

13app = mod.app 1abcdefghij

14 

15 

16def test_completion_show_no_shell(): 1abcdefghij

17 result = subprocess.run( 1abcdefghij

18 [sys.executable, "-m", "coverage", "run", mod.__file__, "--show-completion"], 

19 capture_output=True, 

20 encoding="utf-8", 

21 env={ 

22 **os.environ, 

23 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

24 }, 

25 ) 

26 assert "Option '--show-completion' requires an argument" in result.stderr 1abcdefghij

27 

28 

29def test_completion_show_bash(): 1abcdefghij

30 result = subprocess.run( 1abcdefghij

31 [ 

32 sys.executable, 

33 "-m", 

34 "coverage", 

35 "run", 

36 mod.__file__, 

37 "--show-completion", 

38 "bash", 

39 ], 

40 capture_output=True, 

41 encoding="utf-8", 

42 env={ 

43 **os.environ, 

44 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

45 }, 

46 ) 

47 assert ( 1abcdefghij

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

49 in result.stdout 

50 ) 

51 

52 

53def test_completion_source_zsh(): 1abcdefghij

54 result = subprocess.run( 1abcdefghij

55 [ 

56 sys.executable, 

57 "-m", 

58 "coverage", 

59 "run", 

60 mod.__file__, 

61 "--show-completion", 

62 "zsh", 

63 ], 

64 capture_output=True, 

65 encoding="utf-8", 

66 env={ 

67 **os.environ, 

68 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

69 }, 

70 ) 

71 assert "compdef _tutorial001py_completion tutorial001.py" in result.stdout 1abcdefghij

72 

73 

74def test_completion_source_fish(): 1abcdefghij

75 result = subprocess.run( 1abcdefghij

76 [ 

77 sys.executable, 

78 "-m", 

79 "coverage", 

80 "run", 

81 mod.__file__, 

82 "--show-completion", 

83 "fish", 

84 ], 

85 capture_output=True, 

86 encoding="utf-8", 

87 env={ 

88 **os.environ, 

89 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

90 }, 

91 ) 

92 assert "complete --command tutorial001.py --no-files" in result.stdout 1abcdefghij

93 

94 

95def test_completion_source_powershell(): 1abcdefghij

96 result = subprocess.run( 1abcdefghij

97 [ 

98 sys.executable, 

99 "-m", 

100 "coverage", 

101 "run", 

102 mod.__file__, 

103 "--show-completion", 

104 "powershell", 

105 ], 

106 capture_output=True, 

107 encoding="utf-8", 

108 env={ 

109 **os.environ, 

110 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

111 }, 

112 ) 

113 assert ( 1abcdefghij

114 "Register-ArgumentCompleter -Native -CommandName tutorial001.py -ScriptBlock $scriptblock" 

115 in result.stdout 

116 ) 

117 

118 

119def test_completion_source_pwsh(): 1abcdefghij

120 result = subprocess.run( 1abcdefghij

121 [ 

122 sys.executable, 

123 "-m", 

124 "coverage", 

125 "run", 

126 mod.__file__, 

127 "--show-completion", 

128 "pwsh", 

129 ], 

130 capture_output=True, 

131 encoding="utf-8", 

132 env={ 

133 **os.environ, 

134 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

135 }, 

136 ) 

137 assert ( 1abcdefghij

138 "Register-ArgumentCompleter -Native -CommandName tutorial001.py -ScriptBlock $scriptblock" 

139 in result.stdout 

140 ) 

141 

142 

143def test_completion_show_invalid_shell(): 1abcdefghij

144 with mock.patch.object(typer.completion, "_get_shell_name", return_value="xshell"): 1abcdefghij

145 result = runner.invoke(app, ["--show-completion"]) 1abcdefghij

146 assert "Shell xshell not supported" in result.output 1abcdefghij