blob: a7bc135d4750edeeb67e37514bbda1ce4606826f [file] [log] [blame]
Bram Moolenaar209d3872017-11-16 21:52:51 +01001" Tests for the 'find' command completion.
2
3" Do all the tests in a separate window to avoid E211 when we recursively
4" delete the Xfind directory during cleanup
5func Test_find_complete()
6 set belloff=all
7
8 " On windows a stale "Xfind" directory may exist, remove it so that
9 " we start from a clean state.
10 call delete("Xfind", "rf")
11 let cwd = getcwd()
12 let test_out = cwd . '/test.out'
13 call mkdir('Xfind')
14 cd Xfind
15
16 new
17 set path=
18 call assert_fails('call feedkeys(":find\t\n", "xt")', 'E345:')
19 close
20
21 new
22 set path=.
23 call assert_fails('call feedkeys(":find\t\n", "xt")', 'E32:')
24 close
25
26 new
27 set path=.,,
28 call assert_fails('call feedkeys(":find\t\n", "xt")', 'E32:')
29 close
30
31 new
32 set path=./**
33 call assert_fails('call feedkeys(":find\t\n", "xt")', 'E32:')
34 close
35
36 " We shouldn't find any file till this point
37
38 call mkdir('in/path', 'p')
39 exe 'cd ' . cwd
40 call writefile(['Holy Grail'], 'Xfind/file.txt')
41 call writefile(['Jimmy Hoffa'], 'Xfind/in/file.txt')
42 call writefile(['Another Holy Grail'], 'Xfind/in/stuff.txt')
43 call writefile(['E.T.'], 'Xfind/in/path/file.txt')
44
45 new
46 set path=Xfind/**
47 call feedkeys(":find file\t\n", "xt")
48 call assert_equal('Holy Grail', getline(1))
49 call feedkeys(":find file\t\t\n", "xt")
50 call assert_equal('Jimmy Hoffa', getline(1))
51 call feedkeys(":find file\t\t\t\n", "xt")
52 call assert_equal('E.T.', getline(1))
53
54 " Rerun the previous three find completions, using fullpath in 'path'
55 exec "set path=" . cwd . "/Xfind/**"
56
57 call feedkeys(":find file\t\n", "xt")
58 call assert_equal('Holy Grail', getline(1))
59 call feedkeys(":find file\t\t\n", "xt")
60 call assert_equal('Jimmy Hoffa', getline(1))
61 call feedkeys(":find file\t\t\t\n", "xt")
62 call assert_equal('E.T.', getline(1))
63
64 " Same steps again, using relative and fullpath items that point to the same
65 " recursive location.
66 " This is to test that there are no duplicates in the completion list.
67 set path+=Xfind/**
68 call feedkeys(":find file\t\n", "xt")
69 call assert_equal('Holy Grail', getline(1))
70 call feedkeys(":find file\t\t\n", "xt")
71 call assert_equal('Jimmy Hoffa', getline(1))
72 call feedkeys(":find file\t\t\t\n", "xt")
73 call assert_equal('E.T.', getline(1))
74 call feedkeys(":find file\t\t\n", "xt")
75
76 " Test find completion for directory of current buffer, which at this point
77 " is Xfind/in/file.txt.
78 set path=.
79 call feedkeys(":find st\t\n", "xt")
80 call assert_equal('Another Holy Grail', getline(1))
81
82 " Test find completion for empty path item ",," which is the current
83 " directory
84 cd Xfind
85 set path=,,
86 call feedkeys(":find f\t\n", "xt")
87 call assert_equal('Holy Grail', getline(1))
88
Bram Moolenaar8a37b032018-02-03 20:43:08 +010089 " Test that find completion on directory appends a slash
90 call feedkeys(":find in/pa\tfile.txt\n", "xt")
91 call assert_equal('E.T.', getline(1))
92 call feedkeys(":find ./i\tstuff.txt\n", "xt")
93 call assert_equal('Another Holy Grail', getline(1))
94
Bram Moolenaar209d3872017-11-16 21:52:51 +010095 " Test shortening of
96 "
97 " foo/x/bar/voyager.txt
98 " foo/y/bar/voyager.txt
99 "
100 " When current directory is above foo/ they should be shortened to (in order
101 " of appearance):
102 "
103 " x/bar/voyager.txt
104 " y/bar/voyager.txt
105 call mkdir('foo/x/bar', 'p')
106 call mkdir('foo/y/bar', 'p')
107 call writefile(['Voyager 1'], 'foo/x/bar/voyager.txt')
108 call writefile(['Voyager 2'], 'foo/y/bar/voyager.txt')
109
110 exec "set path=" . cwd . "/Xfind/**"
111 call feedkeys(":find voyager\t\n", "xt")
112 call assert_equal('Voyager 1', getline(1))
113 call feedkeys(":find voyager\t\t\n", "xt")
114 call assert_equal('Voyager 2', getline(1))
115
116 "
117 " When current directory is .../foo/y/bar they should be shortened to (in
118 " order of appearance):
119 "
120 " ./voyager.txt
121 " x/bar/voyager.txt
122 cd foo/y/bar
123 call feedkeys(":find voyager\t\n", "xt")
124 call assert_equal('Voyager 2', getline(1))
125 call feedkeys(":find voyager\t\t\n", "xt")
126 call assert_equal('Voyager 1', getline(1))
127
128 " Check the opposite too:
129 cd ../../x/bar
130 call feedkeys(":find voyager\t\n", "xt")
131 call assert_equal('Voyager 1', getline(1))
132 call feedkeys(":find voyager\t\t\n", "xt")
133 call assert_equal('Voyager 2', getline(1))
134
135 " Check for correct handling of shorten_fname()'s behavior on windows
136 exec "cd " . cwd . "/Xfind/in"
137 call feedkeys(":find file\t\n", "xt")
138 call assert_equal('Jimmy Hoffa', getline(1))
139
140 " Test for relative to current buffer 'path' item
141 exec "cd " . cwd . "/Xfind/"
142 set path=./path
143 " Open the file where Jimmy Hoffa is found
144 e in/file.txt
145 " Find the file containing 'E.T.' in the Xfind/in/path directory
146 call feedkeys(":find file\t\n", "xt")
147 call assert_equal('E.T.', getline(1))
148
149 " Test that completion works when path=.,,
150 set path=.,,
151 " Open Jimmy Hoffa file
152 e in/file.txt
153 call assert_equal('Jimmy Hoffa', getline(1))
154
155 " Search for the file containing Holy Grail in same directory as in/path.txt
156 call feedkeys(":find stu\t\n", "xt")
157 call assert_equal('Another Holy Grail', getline(1))
158
159 enew | only
160 exe 'cd ' . cwd
161 call delete('Xfind', 'rf')
162 set path&
163endfunc