tospaths.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. discard """
  2. output: ""
  3. """
  4. # test the ospaths module
  5. import os, pathnorm
  6. doAssert unixToNativePath("") == ""
  7. doAssert unixToNativePath(".") == $CurDir
  8. doAssert unixToNativePath("..") == $ParDir
  9. doAssert isAbsolute(unixToNativePath("/"))
  10. doAssert isAbsolute(unixToNativePath("/", "a"))
  11. doAssert isAbsolute(unixToNativePath("/a"))
  12. doAssert isAbsolute(unixToNativePath("/a", "a"))
  13. doAssert isAbsolute(unixToNativePath("/a/b"))
  14. doAssert isAbsolute(unixToNativePath("/a/b", "a"))
  15. doAssert unixToNativePath("a/b") == joinPath("a", "b")
  16. when defined(macos):
  17. doAssert unixToNativePath("./") == ":"
  18. doAssert unixToNativePath("./abc") == ":abc"
  19. doAssert unixToNativePath("../abc") == "::abc"
  20. doAssert unixToNativePath("../../abc") == ":::abc"
  21. doAssert unixToNativePath("/abc", "a") == "abc"
  22. doAssert unixToNativePath("/abc/def", "a") == "abc:def"
  23. elif doslikeFileSystem:
  24. doAssert unixToNativePath("./") == ".\\"
  25. doAssert unixToNativePath("./abc") == ".\\abc"
  26. doAssert unixToNativePath("../abc") == "..\\abc"
  27. doAssert unixToNativePath("../../abc") == "..\\..\\abc"
  28. doAssert unixToNativePath("/abc", "a") == "a:\\abc"
  29. doAssert unixToNativePath("/abc/def", "a") == "a:\\abc\\def"
  30. else:
  31. #Tests for unix
  32. doAssert unixToNativePath("./") == "./"
  33. doAssert unixToNativePath("./abc") == "./abc"
  34. doAssert unixToNativePath("../abc") == "../abc"
  35. doAssert unixToNativePath("../../abc") == "../../abc"
  36. doAssert unixToNativePath("/abc", "a") == "/abc"
  37. doAssert unixToNativePath("/abc/def", "a") == "/abc/def"
  38. block extractFilenameTest:
  39. doAssert extractFilename("") == ""
  40. when defined(posix):
  41. doAssert extractFilename("foo/bar") == "bar"
  42. doAssert extractFilename("foo/bar.txt") == "bar.txt"
  43. doAssert extractFilename("foo/") == ""
  44. doAssert extractFilename("/") == ""
  45. when doslikeFileSystem:
  46. doAssert extractFilename(r"foo\bar") == "bar"
  47. doAssert extractFilename(r"foo\bar.txt") == "bar.txt"
  48. doAssert extractFilename(r"foo\") == ""
  49. doAssert extractFilename(r"C:\") == ""
  50. block lastPathPartTest:
  51. doAssert lastPathPart("") == ""
  52. when defined(posix):
  53. doAssert lastPathPart("foo/bar.txt") == "bar.txt"
  54. doAssert lastPathPart("foo/") == "foo"
  55. doAssert lastPathPart("/") == ""
  56. when doslikeFileSystem:
  57. doAssert lastPathPart(r"foo\bar.txt") == "bar.txt"
  58. doAssert lastPathPart(r"foo\") == "foo"
  59. template canon(x): untyped = normalizePath(x, '/')
  60. doAssert canon"/foo/../bar" == "/bar"
  61. doAssert canon"foo/../bar" == "bar"
  62. doAssert canon"/f/../bar///" == "/bar"
  63. doAssert canon"f/..////bar" == "bar"
  64. doAssert canon"../bar" == "../bar"
  65. doAssert canon"/../bar" == "/../bar"
  66. doAssert canon("foo/../../bar/") == "../bar"
  67. doAssert canon("./bla/blob/") == "bla/blob"
  68. doAssert canon(".hiddenFile") == ".hiddenFile"
  69. doAssert canon("./bla/../../blob/./zoo.nim") == "../blob/zoo.nim"
  70. doAssert canon("C:/file/to/this/long") == "C:/file/to/this/long"
  71. doAssert canon("") == ""
  72. doAssert canon("foobar") == "foobar"
  73. doAssert canon("f/////////") == "f"
  74. doAssert relativePath("/foo/bar//baz.nim", "/foo", '/') == "bar/baz.nim"
  75. doAssert normalizePath("./foo//bar/../baz", '/') == "foo/baz"
  76. doAssert relativePath("/Users/me/bar/z.nim", "/Users/other/bad", '/') == "../../me/bar/z.nim"
  77. doAssert relativePath("/Users/me/bar/z.nim", "/Users/other", '/') == "../me/bar/z.nim"
  78. doAssert relativePath("/Users///me/bar//z.nim", "//Users/", '/') == "me/bar/z.nim"
  79. doAssert relativePath("/Users/me/bar/z.nim", "/Users/me", '/') == "bar/z.nim"
  80. doAssert relativePath("", "/users/moo", '/') == ""
  81. doAssert relativePath("foo", "", '/') == "foo"
  82. doAssert joinPath("usr", "") == unixToNativePath"usr/"
  83. doAssert joinPath("", "lib") == "lib"
  84. doAssert joinPath("", "/lib") == unixToNativePath"/lib"
  85. doAssert joinPath("usr/", "/lib") == unixToNativePath"usr/lib"