test_plugin_netrw.vim (4063B)
1 let s:netrw_path = $VIMRUNTIME . '/pack/dist/opt/netrw/autoload/netrw.vim' 2 let s:netrw_test_dir = 'samples' 3 let s:netrw_test_path = s:netrw_test_dir . '/netrw.vim' 4 5 "make copy of netrw script and add function to print local variables" 6 func s:appendDebugToNetrw(netrw_path, netrw_test_path) 7 let netrwScript = readfile(a:netrw_path) 8 9 let netrwScript += [ 10 \ '\n', 11 \ '"-- test helpers ---"', 12 \ 'function! TestNetrwCaptureRemotePath(dirname)', 13 \ ' call s:RemotePathAnalysis(a:dirname)', 14 \ ' return {"method": s:method, "user": s:user, "machine": s:machine, "port": s:port, "path": s:path, "fname": s:fname}', 15 \ 'endfunction' 16 \ ] 17 18 call writefile(netrwScript, a:netrw_test_path) 19 execute 'source' a:netrw_test_path 20 endfunction 21 22 func s:setup() 23 call s:appendDebugToNetrw(s:netrw_path, s:netrw_test_path) 24 endfunction 25 26 func s:cleanup() 27 call delete(s:netrw_test_path) 28 endfunction 29 30 func s:combine 31 \( usernames 32 \, methods 33 \, hosts 34 \, ports 35 \, dirs 36 \, files) 37 for username in a:usernames 38 for method in a:methods 39 for host in a:hosts 40 for port in a:ports 41 for dir in a:dirs 42 for file in a:files 43 " --- Build a full remote path --- 44 45 let port_str = empty(port) ? "" : ':' . port 46 let remote = printf('%s://%s@%s%s/%s%s', method, username, host, port_str, dir, file) 47 48 let result = TestNetrwCaptureRemotePath(remote) 49 50 call assert_equal(result.method, method) 51 call assert_equal(result.user, username) 52 call assert_equal(result.machine, host) 53 call assert_equal(result.port, port) 54 call assert_equal(result.path, dir . file) 55 endfor 56 endfor 57 endfor 58 endfor 59 endfor 60 endfor 61 endfunction 62 63 64 func Test_netrw_parse_remote_simple() 65 call s:setup() 66 let result = TestNetrwCaptureRemotePath('scp://user@localhost:2222/test.txt') 67 call assert_equal(result.method, 'scp') 68 call assert_equal(result.user, 'user') 69 call assert_equal(result.machine, 'localhost') 70 call assert_equal(result.port, '2222') 71 call assert_equal(result.path, 'test.txt') 72 call s:cleanup() 73 endfunction 74 75 "testing different combinations" 76 func Test_netrw_parse_regular_usernames() 77 call s:setup() 78 79 " --- sample data for combinations ---" 80 let usernames = ["root", "toor", "user01", "skillIssue"] 81 let methods = ["scp", "ssh", "ftp", "sftp"] 82 let hosts = ["localhost", "server.com", "fit-workspaces.ksi.fit.cvut.cz", "192.168.1.42"] 83 let ports = ["", "22","420", "443", "2222", "1234"] 84 let dirs = ["", "somefolder/", "path/to/the/bottom/of/the/world/please/send/help/"] 85 let files = ["test.txt", "tttt.vim", "Makefile"] 86 87 call s:combine(usernames, methods, hosts, ports, dirs, files) 88 89 call s:cleanup() 90 endfunc 91 92 "Host myserver 93 " HostName 192.168.1.42 94 " User alice 95 func Test_netrw_parse_ssh_config_entries() 96 call s:setup() 97 let result = TestNetrwCaptureRemotePath('scp://myserver//etc/nginx/nginx.conf') 98 call assert_equal(result.method, 'scp') 99 call assert_equal(result.user, '') 100 call assert_equal(result.machine, 'myserver') 101 call assert_equal(result.port, '') 102 call assert_equal(result.path, '/etc/nginx/nginx.conf') 103 call s:cleanup() 104 endfunction 105 106 "username containing special-chars" 107 func Test_netrw_parse_special_char_user() 108 call s:setup() 109 let result = TestNetrwCaptureRemotePath('scp://user-01@localhost:2222/test.txt') 110 call assert_equal(result.method, 'scp') 111 call assert_equal(result.user, 'user-01') 112 call assert_equal(result.machine, 'localhost') 113 call assert_equal(result.port, '2222') 114 call assert_equal(result.path, 'test.txt') 115 call s:cleanup() 116 endfunction 117 118 func Test_netrw_wipe_empty_buffer_fastpath() 119 let g:netrw_fastbrowse=0 120 packadd netrw 121 call setline(1, 'foobar') 122 let bufnr = bufnr('%') 123 tabnew 124 Explore 125 call search('README.txt', 'W') 126 exe ":norm \<cr>" 127 call assert_equal(4, bufnr('$')) 128 call assert_true(bufexists(bufnr)) 129 bw 130 131 unlet! netrw_fastbrowse 132 endfunction