Skip to content

Commit 9af6d81

Browse files
committed
Fixes #2342
1. In T["Files utils"]["get_mimetype"]["can invoke file"], we mock vim.fn.executable so that it returns 1 and triggers the (mocked) file command. This test makes sure the get_mimetype function can parse the output of the file command. 2. Rename T["Files utils"]["get_mimetype"]["works without file"] to T["Files utils"]["get_mimetype"]["works with fallback"], and mock vim.fn.executable to return 0 so that it never invokes the file command. This test makes sure the hardcoded map is working.
1 parent 2eaf2fb commit 9af6d81

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

tests/utils/test_files.lua

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ end
162162
T["Files utils"]["get_mimetype"] = new_set()
163163
T["Files utils"]["get_mimetype"]["can invoke `file`"] = function()
164164
local orig_system = vim.system
165+
local orig_executable = vim.fn.executable
165166
local _cmd, _opts, _cb = nil, nil, nil
166167
---@diagnostic disable-next-line: duplicate-set-field
167168
vim.system = function(cmds, opts, cb)
@@ -175,14 +176,24 @@ T["Files utils"]["get_mimetype"]["can invoke `file`"] = function()
175176
}
176177
end
177178

179+
vim.fn.executable = function(s)
180+
if s == "file" then
181+
return 1
182+
else
183+
return orig_executable(s)
184+
end
185+
end
186+
178187
local _type = files.get_mimetype("some_file.txt")
179188
h.eq({ "file", "--mime-type", "some_file.txt" }, _cmd)
180189
h.eq("text/plain", _type)
181190
vim.system = orig_system
191+
vim.fn.executable = orig_executable
182192
end
183193

184-
T["Files utils"]["get_mimetype"]["works without `file`"] = function()
194+
T["Files utils"]["get_mimetype"]["works with fallback"] = function()
185195
local orig_system = vim.system
196+
local orig_executable = vim.fn.executable
186197

187198
local _cmd, _opts, _cb = nil, nil, nil
188199
---@diagnostic disable-next-line: duplicate-set-field
@@ -197,10 +208,19 @@ T["Files utils"]["get_mimetype"]["works without `file`"] = function()
197208
}
198209
end
199210

211+
vim.fn.executable = function(s)
212+
if s == "file" then
213+
return 0
214+
else
215+
return orig_executable(s)
216+
end
217+
end
218+
200219
local _type = files.get_mimetype("some_file.png")
201-
h.eq({ "file", "--mime-type", "some_file.png" }, _cmd)
220+
h.eq(nil, _cmd)
202221
h.eq("image/png", _type)
203222
vim.system = orig_system
223+
vim.fn.executable = orig_executable
204224
end
205225

206226
return T

0 commit comments

Comments
 (0)