@@ -137,3 +137,51 @@ describe('trim_common_root', function()
137
137
}, result )
138
138
end )
139
139
end )
140
+
141
+ describe (' make_relative' , function ()
142
+ local path = fs_utils .get_real_path (file .filename ) --- @cast path string
143
+ local basename = vim .fs .basename (path )
144
+ local dirname = vim .fs .dirname (path )
145
+ local dirname_slash = vim .fs .joinpath (dirname , ' ' )
146
+ local root = path
147
+ for parent in vim .fs .parents (path ) do
148
+ root = parent
149
+ end
150
+
151
+ it (' gets the basename' , function ()
152
+ local expected = vim .fs .joinpath (' .' , basename )
153
+ local actual = fs_utils .make_relative (path , dirname )
154
+ assert .are .same (expected , actual )
155
+ end )
156
+
157
+ it (' gets the basename with trailing slash' , function ()
158
+ local expected = vim .fs .joinpath (' .' , basename )
159
+ local actual = fs_utils .make_relative (path , dirname_slash )
160
+ assert .are .same (expected , actual )
161
+ end )
162
+
163
+ it (' works one level up' , function ()
164
+ local parent_name = vim .fs .basename (dirname )
165
+ local expected = vim .fs .joinpath (' .' , parent_name , basename )
166
+ local actual = fs_utils .make_relative (path , vim .fs .dirname (dirname ))
167
+ assert .are .same (expected , actual )
168
+ end )
169
+
170
+ it (' works one level up with trailing slash' , function ()
171
+ local parent_name = vim .fs .basename (dirname )
172
+ local expected = vim .fs .joinpath (' .' , parent_name , basename )
173
+ local actual = fs_utils .make_relative (path , vim .fs .dirname (dirname ) .. ' /' )
174
+ assert .are .same (expected , actual )
175
+ end )
176
+
177
+ it (' produces a relative path even at the root' , function ()
178
+ local relpath = fs_utils .make_relative (path , root )
179
+ assert (vim .endswith (path , relpath ))
180
+ end )
181
+
182
+ it (' climbs up via ..' , function ()
183
+ local relpath = fs_utils .make_relative (root , path )
184
+ local only_cdup = vim .regex (' \\ V\\ (../\\ )\\ +' )
185
+ assert (only_cdup :match_str (relpath ))
186
+ end )
187
+ end )
0 commit comments