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

32 statements  

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

1import os 1abcdefghi

2import subprocess 1abcdefghi

3import sys 1abcdefghi

4from unittest import mock 1abcdefghi

5 

6import typer 1abcdefghi

7import typer.completion 1abcdefghi

8from typer.testing import CliRunner 1abcdefghi

9 

10from docs_src.typer_app import tutorial001 as mod 1abcdefghi

11 

12runner = CliRunner() 1abcdefghi

13app = mod.app 1abcdefghi

14 

15 

16def test_completion_show_no_shell(): 1abcdefghi

17 result = subprocess.run( 1abcdefghi

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

27 

28 

29def test_completion_show_bash(): 1abcdefghi

30 result = subprocess.run( 1abcdefghi

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 ( 1abcdefghi

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

49 in result.stdout 

50 ) 

51 

52 

53def test_completion_source_zsh(): 1abcdefghi

54 result = subprocess.run( 1abcdefghi

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

72 

73 

74def test_completion_source_fish(): 1abcdefghi

75 result = subprocess.run( 1abcdefghi

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

93 

94 

95def test_completion_source_powershell(): 1abcdefghi

96 result = subprocess.run( 1abcdefghi

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 ( 1abcdefghi

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

115 in result.stdout 

116 ) 

117 

118 

119def test_completion_source_pwsh(): 1abcdefghi

120 result = subprocess.run( 1abcdefghi

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 ( 1abcdefghi

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

139 in result.stdout 

140 ) 

141 

142 

143def test_completion_show_invalid_shell(): 1abcdefghi

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

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

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