summary refs log tree commit diff stats
path: root/tests/stdlib/tospaths.nim
blob: ce00b5a958c6d74bc06fb38340d2d77babc33d43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
discard """
  output: ""
"""
# test the ospaths module

import os, pathnorm

doAssert unixToNativePath("") == ""
doAssert unixToNativePath(".") == $CurDir
doAssert unixToNativePath("..") == $ParDir
doAssert isAbsolute(unixToNativePath("/"))
doAssert isAbsolute(unixToNativePath("/", "a"))
doAssert isAbsolute(unixToNativePath("/a"))
doAssert isAbsolute(unixToNativePath("/a", "a"))
doAssert isAbsolute(unixToNativePath("/a/b"))
doAssert isAbsolute(unixToNativePath("/a/b", "a"))
doAssert unixToNativePath("a/b") == joinPath("a", "b")

when defined(macos):
  doAssert unixToNativePath("./") == ":"
  doAssert unixToNativePath("./abc") == ":abc"
  doAssert unixToNativePath("../abc") == "::abc"
  doAssert unixToNativePath("../../abc") == ":::abc"
  doAssert unixToNativePath("/abc", "a") == "abc"
  doAssert unixToNativePath("/abc/def", "a") == "abc:def"
elif doslikeFileSystem:
  doAssert unixToNativePath("./") == ".\\"
  doAssert unixToNativePath("./abc") == ".\\abc"
  doAssert unixToNativePath("../abc") == "..\\abc"
  doAssert unixToNativePath("../../abc") == "..\\..\\abc"
  doAssert unixToNativePath("/abc", "a") == "a:\\abc"
  doAssert unixToNativePath("/abc/def", "a") == "a:\\abc\\def"
else:
  #Tests for unix
  doAssert unixToNativePath("./") == "./"
  doAssert unixToNativePath("./abc") == "./abc"
  doAssert unixToNativePath("../abc") == "../abc"
  doAssert unixToNativePath("../../abc") == "../../abc"
  doAssert unixToNativePath("/abc", "a") == "/abc"
  doAssert unixToNativePath("/abc/def", "a") == "/abc/def"

block extractFilenameTest:
  doAssert extractFilename("") == ""
  when defined(posix):
    doAssert extractFilename("foo/bar") == "bar"
    doAssert extractFilename("foo/bar.txt") == "bar.txt"
    doAssert extractFilename("foo/") == ""
    doAssert extractFilename("/") == ""
  when doslikeFileSystem:
    doAssert extractFilename(r"foo\bar") == "bar"
    doAssert extractFilename(r"foo\bar.txt") == "bar.txt"
    doAssert extractFilename(r"foo\") == ""
    doAssert extractFilename(r"C:\") == ""

block lastPathPartTest:
  doAssert lastPathPart("") == ""
  when defined(posix):
    doAssert lastPathPart("foo/bar.txt") == "bar.txt"
    doAssert lastPathPart("foo/") == "foo"
    doAssert lastPathPart("/") == ""
  when doslikeFileSystem:
    doAssert lastPathPart(r"foo\bar.txt") == "bar.txt"
    doAssert lastPathPart(r"foo\") == "foo"

template canon(x): untyped = normalizePath(x, '/')
doAssert canon"/foo/../bar" == "/bar"
doAssert canon"foo/../bar" == "bar"

doAssert canon"/f/../bar///" == "/bar"
doAssert canon"f/..////bar" == "bar"

doAssert canon"../bar" == "../bar"
doAssert canon"/../bar" == "/../bar"

doAssert canon("foo/../../bar/") == "../bar"
doAssert canon("./bla/blob/") == "bla/blob"
doAssert canon(".hiddenFile") == ".hiddenFile"
doAssert canon("./bla/../../blob/./zoo.nim") == "../blob/zoo.nim"

doAssert canon("C:/file/to/this/long") == "C:/file/to/this/long"
doAssert canon("") == ""
doAssert canon("foobar") == "foobar"
doAssert canon("f/////////") == "f"

doAssert relativePath("/foo/bar//baz.nim", "/foo", '/') == "bar/baz.nim"
doAssert normalizePath("./foo//bar/../baz", '/') == "foo/baz"

doAssert relativePath("/Users/me/bar/z.nim", "/Users/other/bad", '/') == "../../me/bar/z.nim"

doAssert relativePath("/Users/me/bar/z.nim", "/Users/other", '/') == "../me/bar/z.nim"
doAssert relativePath("/Users///me/bar//z.nim", "//Users/", '/') == "me/bar/z.nim"
doAssert relativePath("/Users/me/bar/z.nim", "/Users/me", '/') == "bar/z.nim"
doAssert relativePath("", "/users/moo", '/') == ""
doAssert relativePath("foo", "", '/') == "foo"

doAssert joinPath("usr", "") == unixToNativePath"usr/"
doAssert joinPath("", "lib") == "lib"
doAssert joinPath("", "/lib") == unixToNativePath"/lib"
doAssert joinPath("usr/", "/lib") == unixToNativePath"usr/lib"