Commits

ZyX_I  committed eca7b9c

@/os: Fixed os.path.relpath:
- dir relative to dir now returns . (before was empty string)
- ../../test/directory relative to $TESTDIR now returns directory (before was directory/../../test/directory)

  • Participants
  • Parent commits 058185e

Comments (0)

Files changed (3)

File plugin/frawor/os.vim

 endfunction
 "▶3 os.path.relpath   :: path[, curdir] → path
 function s:os.path.relpath(path, ...)
-    let components=s:os.path.split(s:os.path.abspath(
-                \                                   s:os.path.normpath(a:path)))
-    let tcomponents=s:os.path.split(s:os.path.abspath(
-                \                               (a:0)?(s:os.path.normpath(a:1)):
-                \                                     ('.')))
+    let components=s:os.path.split(s:os.path.normpath(
+                \                                    s:os.path.abspath(a:path)))
+    let tcomponents=s:os.path.split(s:os.path.normpath(
+                \                         s:os.path.abspath((a:0)?(a:1):('.'))))
     call map([components, tcomponents], 'empty(v:val[-1])?remove(v:val, -1):0')
     if components[0] isnot# tcomponents[0]
         " This is valid for windows: you can't construct a relative path if 
     while i<l && components[i] is# tcomponents[i]
         let i+=1
     endwhile
-    return s:os.path.join(repeat(['..'], len(tcomponents)-i)+components[(i):])
+    let r=s:os.path.join(repeat(['..'], len(tcomponents)-i)+components[(i):])
+    if empty(r)
+        let r='.'
+    endif
+    return r
 endfunction
 "▶3 os.path.basename  :: path → component
 function s:os.path.basename(path)

File test/os.ok

Binary file modified.

File test/rtp/plugin/os.vim

 call WriteFile('a/b/c relative to a: '.os.path.relpath(abc, a))
 call WriteFile('a/b/c relative to .: '.os.path.relpath(abc))
 call WriteFile('a/b/c relative to d: '.os.path.relpath(abc, 'd'))
+call WriteFile('../../test/directory relative to ../../test: '.
+            \os.path.relpath(os.path.join('..', '..', 'test', 'directory'),
+            \                os.path.join('..', '..', 'test')))
+call WriteFile('. relative to .: '.os.path.relpath('.', '.'))
 
 W: mkdir
 call os.mkdir(abc)