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

33 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 18:26 +0000

1import os 1abcdefgh

2import subprocess 1abcdefgh

3import sys 1abcdefgh

4from unittest import mock 1abcdefgh

5 

6import shellingham 1abcdefgh

7import typer 1abcdefgh

8from typer.testing import CliRunner 1abcdefgh

9 

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

11 

12runner = CliRunner() 1abcdefgh

13app = typer.Typer() 1abcdefgh

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

15 

16 

17def test_completion_show_no_shell(): 1abcdefgh

18 result = subprocess.run( 1abcdefgh

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

28 

29 

30def test_completion_show_bash(): 1abcdefgh

31 result = subprocess.run( 1abcdefgh

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

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

50 in result.stdout 

51 ) 

52 

53 

54def test_completion_source_zsh(): 1abcdefgh

55 result = subprocess.run( 1abcdefgh

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

73 

74 

75def test_completion_source_fish(): 1abcdefgh

76 result = subprocess.run( 1abcdefgh

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

94 

95 

96def test_completion_source_powershell(): 1abcdefgh

97 result = subprocess.run( 1abcdefgh

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

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

116 in result.stdout 

117 ) 

118 

119 

120def test_completion_source_pwsh(): 1abcdefgh

121 result = subprocess.run( 1abcdefgh

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

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

140 in result.stdout 

141 ) 

142 

143 

144def test_completion_show_invalid_shell(): 1abcdefgh

145 with mock.patch.object( 1abcdefgh

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

147 ): 

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

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