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