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

33 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-14 00:18 +0000

1import os 1abcdefghi

2import subprocess 1abcdefghi

3import sys 1abcdefghi

4from unittest import mock 1abcdefghi

5 

6import shellingham 1abcdefghi

7import typer 1abcdefghi

8from typer.testing import CliRunner 1abcdefghi

9 

10from docs_src.commands.index import tutorial001 as mod 1abcdefghi

11 

12runner = CliRunner() 1abcdefghi

13app = typer.Typer() 1abcdefghi

14app.command()(mod.main) 1abcdefghi

15 

16 

17def test_completion_show_no_shell(): 1abcdefghi

18 result = subprocess.run( 1abcdefghi

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

20 capture_output=True, 

21 encoding="utf-8", 

22 env={ 

23 **os.environ, 

24 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

25 }, 

26 ) 

27 assert "Option '--show-completion' requires an argument" in result.stderr 1abcdefghi

28 

29 

30def test_completion_show_bash(): 1abcdefghi

31 result = subprocess.run( 1abcdefghi

32 [ 

33 sys.executable, 

34 "-m", 

35 "coverage", 

36 "run", 

37 mod.__file__, 

38 "--show-completion", 

39 "bash", 

40 ], 

41 capture_output=True, 

42 encoding="utf-8", 

43 env={ 

44 **os.environ, 

45 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

46 }, 

47 ) 

48 assert ( 1abcdefghi

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

50 in result.stdout 

51 ) 

52 

53 

54def test_completion_source_zsh(): 1abcdefghi

55 result = subprocess.run( 1abcdefghi

56 [ 

57 sys.executable, 

58 "-m", 

59 "coverage", 

60 "run", 

61 mod.__file__, 

62 "--show-completion", 

63 "zsh", 

64 ], 

65 capture_output=True, 

66 encoding="utf-8", 

67 env={ 

68 **os.environ, 

69 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

70 }, 

71 ) 

72 assert "compdef _tutorial001py_completion tutorial001.py" in result.stdout 1abcdefghi

73 

74 

75def test_completion_source_fish(): 1abcdefghi

76 result = subprocess.run( 1abcdefghi

77 [ 

78 sys.executable, 

79 "-m", 

80 "coverage", 

81 "run", 

82 mod.__file__, 

83 "--show-completion", 

84 "fish", 

85 ], 

86 capture_output=True, 

87 encoding="utf-8", 

88 env={ 

89 **os.environ, 

90 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

91 }, 

92 ) 

93 assert "complete --command tutorial001.py --no-files" in result.stdout 1abcdefghi

94 

95 

96def test_completion_source_powershell(): 1abcdefghi

97 result = subprocess.run( 1abcdefghi

98 [ 

99 sys.executable, 

100 "-m", 

101 "coverage", 

102 "run", 

103 mod.__file__, 

104 "--show-completion", 

105 "powershell", 

106 ], 

107 capture_output=True, 

108 encoding="utf-8", 

109 env={ 

110 **os.environ, 

111 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

112 }, 

113 ) 

114 assert ( 1abcdefghi

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

116 in result.stdout 

117 ) 

118 

119 

120def test_completion_source_pwsh(): 1abcdefghi

121 result = subprocess.run( 1abcdefghi

122 [ 

123 sys.executable, 

124 "-m", 

125 "coverage", 

126 "run", 

127 mod.__file__, 

128 "--show-completion", 

129 "pwsh", 

130 ], 

131 capture_output=True, 

132 encoding="utf-8", 

133 env={ 

134 **os.environ, 

135 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True", 

136 }, 

137 ) 

138 assert ( 1abcdefghi

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

140 in result.stdout 

141 ) 

142 

143 

144def test_completion_show_invalid_shell(): 1abcdefghi

145 with mock.patch.object( 1abcdefghi

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

147 ): 

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

149 assert "Shell xshell not supported" in result.stdout 1abcdefghi