robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

taorefactor(path): pathcmp() #41035f0a0182285

master
9.4 KiB267 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, setup, teardown =
5  t.describe, t.it, t.before_each, t.setup, t.teardown
6local clear = n.clear
7local eq = t.eq
8local exec_lua = n.exec_lua
9local fnamemodify = n.fn.fnamemodify
10local getcwd = n.fn.getcwd
11local command = n.command
12local poke_eventloop = n.poke_eventloop
13local write_file = t.write_file
14local is_os = t.is_os
15local chdir = n.fn.chdir
16
17describe('fnamemodify()', function()
18  setup(function()
19    write_file('Xtest-fnamemodify.txt', [[foobar]])
20    t.mkdir('foo')
21    write_file('foo/bar', [[bar]])
22  end)
23
24  before_each(clear)
25
26  teardown(function()
27    os.remove('Xtest-fnamemodify.txt')
28    n.rmdir('foo')
29  end)
30
31  it('root path', function()
32    local root = assert(t.fix_slashes(n.pathroot()))
33    eq(root, fnamemodify([[/]], ':p:h'))
34    eq(root, fnamemodify([[/]], ':p'))
35    if is_os('win') then
36      eq(root, fnamemodify([[\]], ':p:h'))
37      eq(root, fnamemodify([[\]], ':p'))
38      command('set shellslash')
39      eq(root, fnamemodify([[\]], ':p:h'))
40      eq(root, fnamemodify([[\]], ':p'))
41      eq(root, fnamemodify([[/]], ':p:h'))
42      eq(root, fnamemodify([[/]], ':p'))
43
44      local letter_colon = root:sub(1, 2)
45      local old_dir = ('%s/'):format(getcwd())
46      local foo_dir = old_dir .. 'foo/'
47      eq(old_dir, fnamemodify(letter_colon, ':p'))
48      eq(old_dir, fnamemodify(letter_colon .. '.', ':p'))
49      eq(old_dir, fnamemodify(letter_colon .. './', ':p'))
50      eq(foo_dir, fnamemodify(letter_colon .. './foo', ':p'))
51      eq(foo_dir, fnamemodify(letter_colon .. 'foo', ':p'))
52      chdir('foo')
53      eq(old_dir, fnamemodify(letter_colon .. '..', ':p'))
54      eq(old_dir, fnamemodify(letter_colon .. '../', ':p'))
55      eq(foo_dir .. 'bar', fnamemodify(letter_colon .. 'bar', ':p'))
56    end
57  end)
58
59  it('~user in a fast event #40707', function()
60    exec_lua([[
61      local timer = assert(vim.uv.new_timer())
62      timer:start(0, 0, function()
63        timer:close()
64        assert(vim.in_fast_event())
65        _G.result = vim.fn.fnamemodify('~neovim_user_not_found_test/', ':p')
66      end)
67    ]])
68    poke_eventloop()
69    local expected = '~neovim_user_not_found_test/'
70    if is_os('win') then
71      -- Windows never expands ~user, ':p' just prefixes the cwd.
72      expected = fnamemodify('.', ':p') .. expected
73    end
74    eq(expected, exec_lua('return _G.result'))
75  end)
76
77  it(':8 (DOS 8.3 short format)', function()
78    eq('Xtest-fnamemodify.txt', fnamemodify([[Xtest-fnamemodify.txt]], ':8'))
79  end)
80
81  it('examples from ":help filename-modifiers"', function()
82    -- src/ cannot be a symlink in this test.
83    n.api.nvim_set_current_dir(t.paths.test_source_path)
84
85    local filename = 'src/version.c'
86    local cwd = vim.fs.normalize(getcwd())
87
88    eq(('%s/src/version.c'):format(cwd), fnamemodify(filename, ':p'))
89
90    eq('src/version.c', fnamemodify(filename, ':p:.'))
91    eq(('%s/src'):format(cwd), fnamemodify(filename, ':p:h'))
92    eq(cwd, fnamemodify(filename, ':p:h:h'))
93    eq('version.c', fnamemodify(filename, ':p:t'))
94    eq(('%s/src/version'):format(cwd), fnamemodify(filename, ':p:r'))
95
96    eq(('%s/src/main.c'):format(cwd), fnamemodify(filename, ':s?version?main?:p'))
97
98    local converted_cwd = cwd:gsub('/', '\\')
99    eq(converted_cwd .. '\\src\\version.c', fnamemodify(filename, ':p:gs?/?\\\\?'))
100
101    eq('src', fnamemodify(filename, ':h'))
102    eq('version.c', fnamemodify(filename, ':t'))
103    eq('src/version', fnamemodify(filename, ':r'))
104    eq('version', fnamemodify(filename, ':t:r'))
105    eq('c', fnamemodify(filename, ':e'))
106
107    eq('src/main.c', fnamemodify(filename, ':s?version?main?'))
108  end)
109
110  it('advanced examples from ":help filename-modifiers"', function()
111    local filename = 'src/version.c.gz'
112
113    eq('gz', fnamemodify(filename, ':e'))
114    eq('c.gz', fnamemodify(filename, ':e:e'))
115    eq('c.gz', fnamemodify(filename, ':e:e:e'))
116
117    eq('c', fnamemodify(filename, ':e:e:r'))
118
119    eq('src/version.c', fnamemodify(filename, ':r'))
120    eq('c', fnamemodify(filename, ':r:e'))
121
122    eq('src/version', fnamemodify(filename, ':r:r'))
123    eq('src/version', fnamemodify(filename, ':r:r:r'))
124  end)
125
126  it(':h', function()
127    -- generic path
128    eq('.', fnamemodify('hello.txt', ':h'))
129    -- Repeated ":h" on a bare name: the first ":h" replaces the buffer with ".", the
130    -- next must re-anchor into it and not read the freed buffer (Coverity CID 649200).
131    eq('.', fnamemodify('hello.txt', ':h:h'))
132    eq('.', fnamemodify('hello.txt', ':h:h:h'))
133    eq('path/to', fnamemodify('path/to/hello.txt', ':h'))
134    eq('/', fnamemodify('/', ':h'))
135    eq('/', fnamemodify('/foo', ':h'))
136
137    -- collapses more than two leading slashes into a single slash
138    eq('/', fnamemodify('///', ':h'))
139    eq('/', fnamemodify('////', ':h'))
140    eq('/', fnamemodify('///foo', ':h'))
141    eq('/foo', fnamemodify('///foo/bar', ':h'))
142    eq('/foo', fnamemodify('///foo////bar', ':h'))
143    eq('/foo', fnamemodify('////foo/bar', ':h'))
144
145    -- preserves exactly two leading slashes
146    eq('//', fnamemodify('//', ':h'))
147
148    if not is_os('win') then
149      -- POSIX permits special handling for exactly two leading slashes.
150      eq('//', fnamemodify('//', ':h'))
151      eq('//', fnamemodify('//foo', ':h'))
152      eq('//foo', fnamemodify('//foo/bar', ':h'))
153      eq('//foo', fnamemodify('//foo///bar', ':h'))
154    else
155      eq('//server', fnamemodify('//server', ':h'))
156      eq('//server/share', fnamemodify('//server/share', ':h'))
157      eq('//server/share/', fnamemodify('//server/share/', ':h'))
158      eq('//server///share', fnamemodify('//server///share', ':h'))
159      eq('//server/share/', fnamemodify('//server/share/foo', ':h'))
160
161      eq([[//foo/C$]], fnamemodify([[\\foo\C$]], ':h'))
162      eq([[//foo/C$/]], fnamemodify([[\\foo\C$\]], ':h'))
163      eq([[//foo/C$/]], fnamemodify([[\\foo\C$\bar]], ':h'))
164      eq([[//foo/C$/]], fnamemodify([[\\foo\C$\bar]], ':h:h'))
165      eq([[//foo/C$/]], fnamemodify([[//foo\C$/bar]], ':h'))
166      -- `C$` is a share name, not a file name
167      eq('', fnamemodify('//foo/C$/bar', ':h:t'))
168
169      eq('C:', fnamemodify('C:foo', ':h'))
170      eq('C:/', fnamemodify('C:/foo', ':h'))
171
172      eq('//?/C:/', fnamemodify('//?/C:/', ':h'))
173      eq('//?/C:/', fnamemodify('//?/C:/foo', ':h'))
174      eq(
175        '//?/Volume{b75e2c83-0000-0000-0000-602f00000000}/',
176        fnamemodify('//?/Volume{b75e2c83-0000-0000-0000-602f00000000}/foo', ':h')
177      )
178      eq(
179        '//?/Volume{b75e2c83-0000-0000-0000-602f00000000}/',
180        fnamemodify('///?/Volume{b75e2c83-0000-0000-0000-602f00000000}/foo', ':h')
181      )
182      eq('//?/UNC/server', fnamemodify('//?/UNC/server', ':h'))
183      eq('//?/UNC/server/share', fnamemodify('//?/UNC/server/share', ':h'))
184      eq('//?/UNC/server/share/', fnamemodify('//?/UNC/server/share/foo', ':h'))
185
186      -- relative "./" and ".\" prefixes
187      eq('./path/to', fnamemodify('./path/to/hello.txt', ':h'))
188      eq('./path', fnamemodify('./path/to/hello.txt', ':h:h'))
189      eq('./path/to', fnamemodify([[.\path\to\hello.txt]], ':h'))
190    end
191  end)
192
193  it(':t', function()
194    eq('hello.txt', fnamemodify('hello.txt', ':t'))
195    eq('hello.txt', fnamemodify('path/to/hello.txt', ':t'))
196  end)
197
198  it(':r', function()
199    eq('hello', fnamemodify('hello.txt', ':r'))
200    eq('path/to/hello', fnamemodify('path/to/hello.txt', ':r'))
201  end)
202
203  it(':e', function()
204    eq('txt', fnamemodify('hello.txt', ':e'))
205    eq('txt', fnamemodify('path/to/hello.txt', ':e'))
206  end)
207
208  it(':~', function()
209    local cwd = vim.fs.normalize(fnamemodify('.', ':p'))
210    local full_path = ('%s/foo'):format(cwd)
211
212    command(([[let $HOME='%s']]):format(cwd))
213    eq('~/foo', fnamemodify(full_path, ':~'))
214
215    command(([[let $HOME='%s/']]):format(cwd)) -- a trailing slash
216    -- `init_home` calls `os_realpath` on Unix, which removes the trailing slash
217    eq(is_os('win') and full_path or '~/foo', fnamemodify(full_path, ':~'))
218  end)
219
220  it('regex replacements', function()
221    eq('content-there-here.txt', fnamemodify('content-here-here.txt', ':s/here/there/'))
222    eq('content-there-there.txt', fnamemodify('content-here-here.txt', ':gs/here/there/'))
223  end)
224
225  it('shell escape', function()
226    local expected
227
228    if is_os('win') then
229      -- we expand with double-quotes on Windows
230      expected = [["hello there! quote ' newline]] .. '\n' .. [["]]
231    else
232      expected = [['hello there! quote '\'' newline]] .. '\n' .. [[']]
233    end
234
235    eq(expected, fnamemodify("hello there! quote ' newline\n", ':S'))
236  end)
237
238  it('can combine :e and :r', function()
239    -- simple, single extension filename
240    eq('c', fnamemodify('a.c', ':e'))
241    eq('c', fnamemodify('a.c', ':e:e'))
242    eq('c', fnamemodify('a.c', ':e:e:r'))
243    eq('c', fnamemodify('a.c', ':e:e:r:r'))
244
245    -- multi extension filename
246    eq('rb', fnamemodify('a.spec.rb', ':e:r'))
247    eq('rb', fnamemodify('a.spec.rb', ':e:r:r'))
248
249    eq('spec', fnamemodify('a.spec.rb', ':e:e:r'))
250    eq('spec', fnamemodify('a.spec.rb', ':e:e:r:r'))
251
252    eq('spec', fnamemodify('a.b.spec.rb', ':e:e:r'))
253    eq('b.spec', fnamemodify('a.b.spec.rb', ':e:e:e:r'))
254    eq('b', fnamemodify('a.b.spec.rb', ':e:e:e:r:r'))
255
256    eq('spec', fnamemodify('a.b.spec.rb', ':r:e'))
257    eq('b', fnamemodify('a.b.spec.rb', ':r:r:e'))
258
259    -- extraneous :e expansions
260    eq('c', fnamemodify('a.b.c.d.e', ':r:r:e'))
261    eq('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e'))
262
263    -- :e never includes the whole filename, so "a.b":e:e:e --> "b"
264    eq('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e:e'))
265    eq('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e:e:e'))
266  end)
267end)