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

46 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-06-24 00:17 +0000

1import os 1cabdefgh

2import subprocess 1cabdefgh

3import sys 1cabdefgh

4from pathlib import Path 1cabdefgh

5 

6from docs_src.commands.index import tutorial001 as mod 1cabdefgh

7 

8from ..utils import needs_linux 1cabdefgh

9 

10 

11@needs_linux 1cabdefgh

12def test_show_completion(): 1abdefgh

13 result = subprocess.run( 1cab

14 [ 

15 "bash", 

16 "-c", 

17 f"'{sys.executable}' -m coverage run '{mod.__file__}' --show-completion", 

18 ], 

19 capture_output=True, 

20 encoding="utf-8", 

21 env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"}, 

22 ) 

23 assert "_TUTORIAL001.PY_COMPLETE=complete_bash" in result.stdout 1cab

24 

25 

26@needs_linux 1cabdefgh

27def test_install_completion(): 1abdefgh

28 bash_completion_path: Path = Path.home() / ".bashrc" 1cab

29 text = "" 1cab

30 if bash_completion_path.is_file(): # pragma: no cover 1cab

31 text = bash_completion_path.read_text() 1cab

32 result = subprocess.run( 1cab

33 [ 

34 "bash", 

35 "-c", 

36 f"'{sys.executable}' -m coverage run '{mod.__file__}' --install-completion", 

37 ], 

38 capture_output=True, 

39 encoding="utf-8", 

40 env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"}, 

41 ) 

42 new_text = bash_completion_path.read_text() 1cab

43 bash_completion_path.write_text(text) 1cab

44 assert "source" in new_text 1cab

45 assert str(Path(".bash_completions/tutorial001.py.sh")) in new_text 1cab

46 assert "completion installed in" in result.stdout 1cab

47 assert "Completion will take effect once you restart the terminal" in result.stdout 1cab

48 

49 

50def test_completion_invalid_instruction(): 1cabdefgh

51 result = subprocess.run( 1cabdefgh

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

53 capture_output=True, 

54 encoding="utf-8", 

55 env={ 

56 **os.environ, 

57 "_TUTORIAL001.PY_COMPLETE": "sourcebash", 

58 }, 

59 ) 

60 assert result.returncode != 0 1cabdefgh

61 assert "Invalid completion instruction." in result.stderr 1cabdefgh

62 

63 

64def test_completion_source_bash(): 1cabdefgh

65 result = subprocess.run( 1cabdefgh

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

67 capture_output=True, 

68 encoding="utf-8", 

69 env={ 

70 **os.environ, 

71 "_TUTORIAL001.PY_COMPLETE": "source_bash", 

72 }, 

73 ) 

74 assert ( 1cabdefgh

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

76 in result.stdout 

77 ) 

78 

79 

80def test_completion_source_invalid_shell(): 1cabdefgh

81 result = subprocess.run( 1cabdefgh

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

83 capture_output=True, 

84 encoding="utf-8", 

85 env={ 

86 **os.environ, 

87 "_TUTORIAL001.PY_COMPLETE": "source_xxx", 

88 }, 

89 ) 

90 assert "Shell xxx not supported." in result.stderr 1cabdefgh

91 

92 

93def test_completion_source_invalid_instruction(): 1cabdefgh

94 result = subprocess.run( 1cabdefgh

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

96 capture_output=True, 

97 encoding="utf-8", 

98 env={ 

99 **os.environ, 

100 "_TUTORIAL001.PY_COMPLETE": "explode_bash", 

101 }, 

102 ) 

103 assert 'Completion instruction "explode" not supported.' in result.stderr 1cabdefgh

104 

105 

106def test_completion_source_zsh(): 1cabdefgh

107 result = subprocess.run( 1cabdefgh

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

109 capture_output=True, 

110 encoding="utf-8", 

111 env={ 

112 **os.environ, 

113 "_TUTORIAL001.PY_COMPLETE": "source_zsh", 

114 }, 

115 ) 

116 assert "compdef _tutorial001py_completion tutorial001.py" in result.stdout 1cabdefgh

117 

118 

119def test_completion_source_fish(): 1cabdefgh

120 result = subprocess.run( 1cabdefgh

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

122 capture_output=True, 

123 encoding="utf-8", 

124 env={ 

125 **os.environ, 

126 "_TUTORIAL001.PY_COMPLETE": "source_fish", 

127 }, 

128 ) 

129 assert "complete --command tutorial001.py --no-files" in result.stdout 1cabdefgh

130 

131 

132def test_completion_source_powershell(): 1cabdefgh

133 result = subprocess.run( 1cabdefgh

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

135 capture_output=True, 

136 encoding="utf-8", 

137 env={ 

138 **os.environ, 

139 "_TUTORIAL001.PY_COMPLETE": "source_powershell", 

140 }, 

141 ) 

142 assert ( 1cabdefgh

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

144 in result.stdout 

145 ) 

146 

147 

148def test_completion_source_pwsh(): 1cabdefgh

149 result = subprocess.run( 1cabdefgh

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

151 capture_output=True, 

152 encoding="utf-8", 

153 env={ 

154 **os.environ, 

155 "_TUTORIAL001.PY_COMPLETE": "source_pwsh", 

156 }, 

157 ) 

158 assert ( 1cabdefgh

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

160 in result.stdout 

161 )