@@ -86,3 +86,51 @@ describe('get_real_path', function()
86
86
assert .is .False (fs_utils .get_real_path (' .' ))
87
87
end )
88
88
end )
89
+
90
+ describe (' make_relative' , function ()
91
+ local path = fs_utils .get_real_path (file .filename ) --- @cast path string
92
+ local basename = vim .fs .basename (path )
93
+ local dirname = vim .fs .dirname (path )
94
+ local dirname_slash = vim .fs .joinpath (dirname , ' ' )
95
+ local root = path
96
+ for parent in vim .fs .parents (path ) do
97
+ root = parent
98
+ end
99
+
100
+ it (' gets the basename' , function ()
101
+ local expected = vim .fs .joinpath (' .' , basename )
102
+ local actual = fs_utils .make_relative (path , dirname )
103
+ assert .are .same (expected , actual )
104
+ end )
105
+
106
+ it (' gets the basename with trailing slash' , function ()
107
+ local expected = vim .fs .joinpath (' .' , basename )
108
+ local actual = fs_utils .make_relative (path , dirname_slash )
109
+ assert .are .same (expected , actual )
110
+ end )
111
+
112
+ it (' works one level up' , function ()
113
+ local parent_name = vim .fs .basename (dirname )
114
+ local expected = vim .fs .joinpath (' .' , parent_name , basename )
115
+ local actual = fs_utils .make_relative (path , vim .fs .dirname (dirname ))
116
+ assert .are .same (expected , actual )
117
+ end )
118
+
119
+ it (' works one level up with trailing slash' , function ()
120
+ local parent_name = vim .fs .basename (dirname )
121
+ local expected = vim .fs .joinpath (' .' , parent_name , basename )
122
+ local actual = fs_utils .make_relative (path , vim .fs .dirname (dirname ) .. ' /' )
123
+ assert .are .same (expected , actual )
124
+ end )
125
+
126
+ it (' produces a relative path even at the root' , function ()
127
+ local relpath = fs_utils .make_relative (path , root )
128
+ assert (vim .endswith (path , relpath ))
129
+ end )
130
+
131
+ it (' climbs up via ..' , function ()
132
+ local relpath = fs_utils .make_relative (root , path )
133
+ local only_cdup = vim .regex (' \\ V\\ (../\\ )\\ +' )
134
+ assert (only_cdup :match_str (relpath ))
135
+ end )
136
+ end )
0 commit comments