@@ -9,8 +9,6 @@ pub(crate) fn embedded_shell_integration_script() -> String {
99#[ cfg( test) ]
1010mod tests {
1111 use super :: { embedded_shell_integration_script, normalized_shell_integration_script} ;
12- #[ cfg( unix) ]
13- use std:: { fs, os:: unix:: fs:: PermissionsExt , process:: Command } ;
1412
1513 #[ test]
1614 fn normalized_shell_integration_script_converts_crlf_to_lf ( ) {
@@ -28,227 +26,4 @@ mod tests {
2826 "嵌入式 shell integration 脚本不应保留 CR,避免远端 shell 解析失败"
2927 ) ;
3028 }
31-
32- #[ test]
33- fn embedded_shell_integration_script_enables_vim_mouse ( ) {
34- let script = embedded_shell_integration_script ( ) ;
35- assert ! ( script. contains( "--cmd 'set mouse=a'" ) ) ;
36- assert ! ( script. contains( "--cmd 'nnoremap <ScrollWheelUp> gkzz'" ) ) ;
37- assert ! ( script. contains( "--cmd 'nnoremap <ScrollWheelDown> gjzz'" ) ) ;
38- assert ! ( script. contains( "function vim {" ) ) ;
39- assert ! ( script. contains( "__onetcli_can_wrap_command vim" ) ) ;
40- }
41-
42- #[ cfg( unix) ]
43- #[ test]
44- fn bash_vim_wrapper_preserves_args ( ) {
45- assert_vim_wrapper_preserves_args ( "bash" ) ;
46- }
47-
48- #[ cfg( unix) ]
49- #[ test]
50- fn zsh_vim_wrapper_preserves_args ( ) {
51- if !shell_available ( "zsh" ) {
52- return ;
53- }
54- assert_vim_wrapper_preserves_args ( "zsh" ) ;
55- }
56-
57- #[ cfg( unix) ]
58- #[ test]
59- fn bash_vim_wrapper_does_not_override_alias ( ) {
60- assert_vim_wrapper_does_not_override_alias ( "bash" ) ;
61- }
62-
63- #[ cfg( unix) ]
64- #[ test]
65- fn zsh_vim_wrapper_does_not_override_alias ( ) {
66- if !shell_available ( "zsh" ) {
67- return ;
68- }
69- assert_vim_wrapper_does_not_override_alias ( "zsh" ) ;
70- }
71-
72- #[ cfg( unix) ]
73- #[ test]
74- fn bash_vim_wrapper_does_not_override_function ( ) {
75- assert_vim_wrapper_does_not_override_function ( "bash" ) ;
76- }
77-
78- #[ cfg( unix) ]
79- #[ test]
80- fn zsh_vim_wrapper_does_not_override_function ( ) {
81- if !shell_available ( "zsh" ) {
82- return ;
83- }
84- assert_vim_wrapper_does_not_override_function ( "zsh" ) ;
85- }
86-
87- #[ cfg( unix) ]
88- #[ test]
89- fn bash_vim_mouse_can_be_disabled ( ) {
90- assert_vim_mouse_can_be_disabled ( "bash" ) ;
91- }
92-
93- #[ cfg( unix) ]
94- #[ test]
95- fn zsh_vim_mouse_can_be_disabled ( ) {
96- if !shell_available ( "zsh" ) {
97- return ;
98- }
99- assert_vim_mouse_can_be_disabled ( "zsh" ) ;
100- }
101-
102- #[ cfg( unix) ]
103- #[ test]
104- fn bash_nvim_wrapper_preserves_args ( ) {
105- assert_nvim_wrapper_preserves_args ( "bash" ) ;
106- }
107-
108- #[ cfg( unix) ]
109- #[ test]
110- fn zsh_nvim_wrapper_preserves_args ( ) {
111- if !shell_available ( "zsh" ) {
112- return ;
113- }
114- assert_nvim_wrapper_preserves_args ( "zsh" ) ;
115- }
116-
117- #[ cfg( unix) ]
118- fn assert_vim_wrapper_preserves_args ( shell : & str ) {
119- let output = run_interactive_shell (
120- shell,
121- "source \" $ONETCLI_TEST_SCRIPT\" \n vim 'a b.txt' -- '--weird;$HOME'" ,
122- ) ;
123-
124- assert ! ( output. status. success( ) ) ;
125- assert_eq ! (
126- strip_shell_integration_osc( & String :: from_utf8_lossy( & output. stdout) ) ,
127- "--cmd\n set mouse=a\n --cmd\n nnoremap <ScrollWheelUp> gkzz\n --cmd\n nnoremap <ScrollWheelDown> gjzz\n --cmd\n inoremap <ScrollWheelUp> <C-o>gk<C-o>zz\n --cmd\n inoremap <ScrollWheelDown> <C-o>gj<C-o>zz\n a b.txt\n --\n --weird;$HOME\n "
128- ) ;
129- }
130-
131- #[ cfg( unix) ]
132- fn assert_vim_wrapper_does_not_override_alias ( shell : & str ) {
133- let output = run_interactive_shell (
134- shell,
135- "shopt -s expand_aliases 2>/dev/null || true\n alias vim='echo alias-safe'\n source \" $ONETCLI_TEST_SCRIPT\" \n vim" ,
136- ) ;
137-
138- assert ! ( output. status. success( ) ) ;
139- assert_eq ! (
140- strip_shell_integration_osc( & String :: from_utf8_lossy( & output. stdout) ) ,
141- "alias-safe\n "
142- ) ;
143- }
144-
145- #[ cfg( unix) ]
146- fn assert_vim_wrapper_does_not_override_function ( shell : & str ) {
147- let output = run_interactive_shell (
148- shell,
149- "vim() { echo function-safe; }\n source \" $ONETCLI_TEST_SCRIPT\" \n vim" ,
150- ) ;
151-
152- assert ! ( output. status. success( ) ) ;
153- assert_eq ! (
154- strip_shell_integration_osc( & String :: from_utf8_lossy( & output. stdout) ) ,
155- "function-safe\n "
156- ) ;
157- }
158-
159- #[ cfg( unix) ]
160- fn assert_vim_mouse_can_be_disabled ( shell : & str ) {
161- let output = run_interactive_shell (
162- shell,
163- "source \" $ONETCLI_TEST_SCRIPT\" \n ONETCLI_VIM_MOUSE=0 vim file.txt" ,
164- ) ;
165-
166- assert ! ( output. status. success( ) ) ;
167- assert_eq ! (
168- strip_shell_integration_osc( & String :: from_utf8_lossy( & output. stdout) ) ,
169- "file.txt\n "
170- ) ;
171- }
172-
173- #[ cfg( unix) ]
174- fn assert_nvim_wrapper_preserves_args ( shell : & str ) {
175- let output = run_interactive_shell ( shell, "source \" $ONETCLI_TEST_SCRIPT\" \n nvim file.txt" ) ;
176-
177- assert ! ( output. status. success( ) ) ;
178- assert_eq ! (
179- strip_shell_integration_osc( & String :: from_utf8_lossy( & output. stdout) ) ,
180- "--cmd\n set mouse=a\n --cmd\n nnoremap <ScrollWheelUp> gkzz\n --cmd\n nnoremap <ScrollWheelDown> gjzz\n --cmd\n inoremap <ScrollWheelUp> <C-o>gk<C-o>zz\n --cmd\n inoremap <ScrollWheelDown> <C-o>gj<C-o>zz\n file.txt\n "
181- ) ;
182- }
183-
184- #[ cfg( unix) ]
185- fn shell_available ( shell : & str ) -> bool {
186- let available = Command :: new ( shell) . arg ( "--version" ) . output ( ) . is_ok ( ) ;
187- if !available {
188- eprintln ! ( "跳过 {shell} 行为测试:当前环境未安装该 shell" ) ;
189- }
190- available
191- }
192-
193- #[ cfg( unix) ]
194- fn run_interactive_shell ( shell : & str , command : & str ) -> std:: process:: Output {
195- let temp_dir = std:: env:: temp_dir ( ) . join ( format ! (
196- "onetcli-shell-integration-test-{}-{:?}" ,
197- std:: process:: id( ) ,
198- std:: thread:: current( ) . id( )
199- ) ) ;
200- let bin_dir = temp_dir. join ( "bin" ) ;
201- let home_dir = temp_dir. join ( "home" ) ;
202- let zdot_dir = temp_dir. join ( "zsh" ) ;
203- fs:: create_dir_all ( & bin_dir) . expect ( "应创建测试 bin 目录" ) ;
204- fs:: create_dir_all ( & home_dir) . expect ( "应创建测试 HOME 目录" ) ;
205- fs:: create_dir_all ( & zdot_dir) . expect ( "应创建测试 ZDOTDIR 目录" ) ;
206-
207- let script_path = temp_dir. join ( "shell_integration.sh" ) ;
208- fs:: write ( & script_path, embedded_shell_integration_script ( ) ) . expect ( "应写入集成脚本" ) ;
209-
210- write_fake_editor ( & bin_dir. join ( "vim" ) ) ;
211- write_fake_editor ( & bin_dir. join ( "nvim" ) ) ;
212-
213- let command_path = temp_dir. join ( "command.sh" ) ;
214- fs:: write ( & command_path, command) . expect ( "应写入测试命令脚本" ) ;
215-
216- let path = format ! (
217- "{}:{}" ,
218- bin_dir. display( ) ,
219- std:: env:: var( "PATH" ) . unwrap_or_default( )
220- ) ;
221- let output = Command :: new ( shell)
222- . arg ( "-i" )
223- . arg ( & command_path)
224- . env ( "PATH" , path)
225- . env ( "HOME" , & home_dir)
226- . env ( "ZDOTDIR" , & zdot_dir)
227- . env ( "ONETCLI_TEST_SCRIPT" , & script_path)
228- . output ( )
229- . expect ( "应执行 shell 行为测试" ) ;
230-
231- let _ = fs:: remove_dir_all ( & temp_dir) ;
232- output
233- }
234-
235- #[ cfg( unix) ]
236- fn write_fake_editor ( path : & std:: path:: Path ) {
237- fs:: write (
238- path,
239- "#!/bin/sh\n for arg in \" $@\" ; do printf '%s\\ n' \" $arg\" ; done\n " ,
240- )
241- . expect ( "应写入 fake editor" ) ;
242- fs:: set_permissions ( path, fs:: Permissions :: from_mode ( 0o755 ) )
243- . expect ( "应设置 fake editor 可执行权限" ) ;
244- }
245-
246- #[ cfg( unix) ]
247- fn strip_shell_integration_osc ( output : & str ) -> String {
248- output
249- . replace ( "\u{1b} ]133;C\u{7} " , "" )
250- . replace ( "\u{1b} ]133;D;0\u{7} " , "" )
251- . replace ( "\u{1b} ]133;A\u{7} " , "" )
252- . replace ( "\u{1b} ]133;B\u{7} " , "" )
253- }
25429}
0 commit comments