Skip to content

Commit 0939120

Browse files
committed
test: add ui spec and more coverage to utils spec
1 parent db4c7ca commit 0939120

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

spec/ui_spec.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package.path = "lua/?.lua;lua/?/init.lua;" .. package.path
2+
_G.vim = {}
3+
local ui = require "remote-sshfs.ui"
4+
5+
describe("remote-sshfs.ui", function()
6+
local orig_vim
7+
8+
before_each(function()
9+
orig_vim = _G.vim
10+
_G.vim = {
11+
ui = {},
12+
schedule = function(fn) fn() end,
13+
cmd = function(cmd) _G.vim._cmd = cmd end,
14+
api = { nvim_replace_termcodes = function(str) return str end, nvim_feedkeys = function(keys, mode) _G.vim._feed = {keys, mode} end },
15+
opt = { cmdheight = { _value = 1 } },
16+
}
17+
_G.vim.ui.select = function(items, opts, cb) cb("selected") end
18+
_G.vim.ui.input = function(opts, cb) cb("inputted") end
19+
end)
20+
21+
after_each(function()
22+
_G.vim = orig_vim
23+
end)
24+
25+
it("prompt uses vim.ui.select when select_prompts is true", function()
26+
ui.setup({ ui = { select_prompts = true } })
27+
local result
28+
ui.prompt("prompt?", "select?", { "a", "b" }, { "A", "B" }, function(choice) result = choice end)
29+
assert.are.equal("selected", result)
30+
end)
31+
32+
it("prompt uses vim.ui.input when select_prompts is false", function()
33+
ui.setup({ ui = { select_prompts = false } })
34+
local result
35+
ui.prompt("prompt?", "select?", { "a", "b" }, { "A", "B" }, function(choice) result = choice end)
36+
assert.are.equal("inputted", result)
37+
end)
38+
39+
it("prompt_yes_no calls prompt and schedules startinsert", function()
40+
ui.setup({ ui = { select_prompts = false } })
41+
local result
42+
ui.prompt_yes_no("prompt", function(choice) result = choice end)
43+
assert.are.equal("inputted", result)
44+
assert.is.truthy(vim._cmd:match("startinsert"))
45+
end)
46+
47+
it("clear_prompt sends <C-c> when cmdheight non-zero", function()
48+
ui.clear_prompt()
49+
assert.are.equal("<C-c>", vim._feed[1])
50+
assert.are.equal("n", vim._feed[2])
51+
end)
52+
end)

spec/utils_spec.lua

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package.path = "lua/?.lua;lua/?/init.lua;" .. package.path
2+
_G.vim = {}
3+
local utils = require "remote-sshfs.utils"
4+
5+
describe("remote-sshfs.utils", function()
6+
describe("setup_sshfs", function()
7+
local created_path
8+
local orig_vim
9+
10+
before_each(function()
11+
orig_vim = _G.vim
12+
_G.vim = { loop = {}, notify = function() end }
13+
_G.vim.loop.fs_stat = function(path) return nil end
14+
_G.vim.loop.fs_mkdir = function(path, mode) created_path = path; return true end
15+
end)
16+
after_each(function()
17+
_G.vim = orig_vim
18+
end)
19+
20+
it("creates mount base dir when not exists", function()
21+
utils.setup_sshfs({ mounts = { base_dir = "/tmp/sshfs" } })
22+
assert.are.equal("/tmp/sshfs", created_path)
23+
end)
24+
end)
25+
26+
describe("file_exists", function()
27+
local orig_vim
28+
before_each(function()
29+
orig_vim = _G.vim
30+
_G.vim = { loop = {} }
31+
end)
32+
after_each(function()
33+
_G.vim = orig_vim
34+
end)
35+
36+
it("returns true when fs_stat returns non-nil", function()
37+
vim.loop.fs_stat = function() return {}, nil end
38+
assert.is_true(utils.file_exists("somepath"))
39+
end)
40+
41+
it("returns false when fs_stat returns nil and error", function()
42+
vim.loop.fs_stat = function() return nil, 'err' end
43+
assert.is_false(utils.file_exists("somepath"))
44+
end)
45+
end)
46+
47+
describe("setup_mount_dir", function()
48+
local created_path, callback_ok
49+
local orig_vim
50+
51+
before_each(function()
52+
orig_vim = _G.vim
53+
_G.vim = { loop = {}, notify = function() end }
54+
created_path = nil
55+
callback_ok = false
56+
_G.vim.loop.fs_stat = function() return nil, 'err' end
57+
_G.vim.loop.fs_mkdir = function(path, mode) created_path = path; return true end
58+
end)
59+
after_each(function()
60+
_G.vim = orig_vim
61+
end)
62+
63+
it("creates mount dir and calls callback when missing", function()
64+
utils.setup_mount_dir("/tmp/mount", function() callback_ok = true end)
65+
assert.are.equal("/tmp/mount", created_path)
66+
assert.is_true(callback_ok)
67+
end)
68+
69+
it("calls callback when dir already exists", function()
70+
vim.loop.fs_stat = function() return {} end
71+
utils.setup_mount_dir("/tmp/mount", function() callback_ok = true end)
72+
assert.is_true(callback_ok)
73+
end)
74+
end)
75+
76+
describe("cleanup_mount_dir", function()
77+
local removed_path, callback_ok
78+
local orig_vim
79+
80+
before_each(function()
81+
orig_vim = _G.vim
82+
_G.vim = { loop = {}, notify = function() end }
83+
removed_path = nil
84+
callback_ok = false
85+
_G.vim.loop.fs_rmdir = function(path) removed_path = path; return true end
86+
end)
87+
after_each(function()
88+
_G.vim = orig_vim
89+
end)
90+
91+
it("removes mount dir and calls callback when exists", function()
92+
utils.file_exists = function() return true end
93+
utils.cleanup_mount_dir("/tmp/mount", function() callback_ok = true end)
94+
assert.are.equal("/tmp/mount", removed_path)
95+
assert.is_true(callback_ok)
96+
end)
97+
98+
it("calls callback when dir does not exist without removing", function()
99+
utils.file_exists = function() return false end
100+
utils.cleanup_mount_dir("/tmp/mount", function() callback_ok = true end)
101+
assert.is_true(callback_ok)
102+
end)
103+
104+
it("does not call callback when removal fails", function()
105+
utils.file_exists = function() return true end
106+
vim.loop.fs_rmdir = function() return false end
107+
utils.cleanup_mount_dir("/tmp/mount", function() callback_ok = true end)
108+
assert.is_false(callback_ok)
109+
end)
110+
end)
111+
112+
describe("parse_hosts_from_configs", function()
113+
local tmpfile, orig_vim
114+
115+
before_each(function()
116+
orig_vim = _G.vim
117+
_G.vim = {
118+
fn = {
119+
expand = function(x) return x end,
120+
filereadable = function() return 1 end,
121+
executable = function() return 0 end,
122+
systemlist = function() return {} end,
123+
},
124+
v = { shell_error = 1 },
125+
}
126+
tmpfile = "ssh_config_test"
127+
local lines = {
128+
"# test config",
129+
"Host a b",
130+
" HostName host.example.com",
131+
" User tony",
132+
"Host single",
133+
}
134+
local f = io.open(tmpfile, "w")
135+
f:write(table.concat(lines, "\n"))
136+
f:close()
137+
end)
138+
139+
after_each(function()
140+
_G.vim = orig_vim
141+
os.remove(tmpfile)
142+
end)
143+
144+
it("parses hosts and attributes without ssh overrides", function()
145+
local res = utils.parse_hosts_from_configs({ tmpfile })
146+
assert.are.equal(tmpfile, res['a'].Config)
147+
assert.are.equal('a', res['a'].Name)
148+
assert.are.equal('host.example.com', res['a'].HostName)
149+
assert.are.equal('tony', res['a'].User)
150+
assert.are.equal(tmpfile, res['b'].Config)
151+
assert.are.equal('b', res['b'].Name)
152+
assert.are.equal('host.example.com', res['b'].HostName)
153+
assert.are.equal('tony', res['b'].User)
154+
assert.are.equal(tmpfile, res['single'].Config)
155+
assert.are.equal('single', res['single'].Name)
156+
end)
157+
end)
158+
end)

0 commit comments

Comments
 (0)