tos.nim 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. discard """
  2. output: '''
  3. All:
  4. __really_obscure_dir_name/are.x
  5. __really_obscure_dir_name/created
  6. __really_obscure_dir_name/dirs
  7. __really_obscure_dir_name/files.q
  8. __really_obscure_dir_name/some
  9. __really_obscure_dir_name/test
  10. __really_obscure_dir_name/testing.r
  11. __really_obscure_dir_name/these.txt
  12. Files:
  13. __really_obscure_dir_name/are.x
  14. __really_obscure_dir_name/files.q
  15. __really_obscure_dir_name/testing.r
  16. __really_obscure_dir_name/these.txt
  17. Dirs:
  18. __really_obscure_dir_name/created
  19. __really_obscure_dir_name/dirs
  20. __really_obscure_dir_name/some
  21. __really_obscure_dir_name/test
  22. Raises
  23. Raises
  24. '''
  25. joinable: false
  26. """
  27. # test os path creation, iteration, and deletion
  28. import os, strutils, pathnorm
  29. from stdtest/specialpaths import buildDir
  30. import std/[syncio, assertions]
  31. block fileOperations:
  32. let files = @["these.txt", "are.x", "testing.r", "files.q"]
  33. let dirs = @["some", "created", "test", "dirs"]
  34. let dname = "__really_obscure_dir_name"
  35. createDir(dname)
  36. doAssert dirExists(dname)
  37. block: # copyFile, copyFileToDir
  38. doAssertRaises(OSError): copyFile(dname/"nonexistent.txt", dname/"nonexistent.txt")
  39. let fname = "D20201009T112235"
  40. let fname2 = "D20201009T112235.2"
  41. let str = "foo1\0foo2\nfoo3\0"
  42. let file = dname/fname
  43. let file2 = dname/fname2
  44. writeFile(file, str)
  45. doAssert readFile(file) == str
  46. let sub = "sub"
  47. doAssertRaises(OSError): copyFile(file, dname/sub/fname2)
  48. doAssertRaises(OSError): copyFileToDir(file, dname/sub)
  49. doAssertRaises(ValueError): copyFileToDir(file, "")
  50. copyFile(file, file2)
  51. doAssert fileExists(file2)
  52. doAssert readFile(file2) == str
  53. createDir(dname/sub)
  54. copyFileToDir(file, dname/sub)
  55. doAssert fileExists(dname/sub/fname)
  56. removeDir(dname/sub)
  57. doAssert not dirExists(dname/sub)
  58. removeFile(file)
  59. removeFile(file2)
  60. # Test creating files and dirs
  61. for dir in dirs:
  62. createDir(dname/dir)
  63. doAssert dirExists(dname/dir)
  64. for file in files:
  65. let fh = open(dname/file, fmReadWrite)
  66. fh.close()
  67. doAssert fileExists(dname/file)
  68. echo "All:"
  69. template norm(x): untyped =
  70. (when defined(windows): x.replace('\\', '/') else: x)
  71. for path in walkPattern(dname/"*"):
  72. echo path.norm
  73. echo "Files:"
  74. for path in walkFiles(dname/"*"):
  75. echo path.norm
  76. echo "Dirs:"
  77. for path in walkDirs(dname/"*"):
  78. echo path.norm
  79. # Test removal of files dirs
  80. for dir in dirs:
  81. removeDir(dname/dir)
  82. doAssert: not dirExists(dname/dir)
  83. for file in files:
  84. removeFile(dname/file)
  85. doAssert: not fileExists(dname/file)
  86. removeDir(dname)
  87. doAssert: not dirExists(dname)
  88. # createDir should create recursive directories
  89. createDir(dirs[0] / dirs[1])
  90. doAssert dirExists(dirs[0] / dirs[1]) # true
  91. removeDir(dirs[0])
  92. # createDir should properly handle trailing separator
  93. createDir(dname / "")
  94. doAssert dirExists(dname) # true
  95. removeDir(dname)
  96. # createDir should raise IOError if the path exists
  97. # and is not a directory
  98. open(dname, fmWrite).close
  99. try:
  100. createDir(dname)
  101. except IOError:
  102. echo "Raises"
  103. removeFile(dname)
  104. # removeFile should not remove directory
  105. createDir(dname)
  106. try:
  107. removeFile(dname)
  108. except OSError:
  109. echo "Raises"
  110. removeDir(dname)
  111. # test copyDir:
  112. createDir("a/b")
  113. open("a/b/file.txt", fmWrite).close
  114. createDir("a/b/c")
  115. open("a/b/c/fileC.txt", fmWrite).close
  116. copyDir("a", "../dest/a")
  117. removeDir("a")
  118. doAssert dirExists("../dest/a/b")
  119. doAssert fileExists("../dest/a/b/file.txt")
  120. doAssert fileExists("../dest/a/b/c/fileC.txt")
  121. removeDir("../dest")
  122. # test copyDir:
  123. # if separator at the end of a path
  124. createDir("a/b")
  125. open("a/file.txt", fmWrite).close
  126. copyDir("a/", "../dest/a/")
  127. removeDir("a")
  128. doAssert dirExists("../dest/a/b")
  129. doAssert fileExists("../dest/a/file.txt")
  130. removeDir("../dest")
  131. # createDir should not fail if `dir` is empty
  132. createDir("")
  133. # Symlink handling in `copyFile`, `copyFileWithPermissions`, `copyFileToDir`,
  134. # `copyDir`, `copyDirWithPermissions`, `moveFile`, and `moveDir`.
  135. block:
  136. const symlinksAreHandled = not defined(windows)
  137. const dname = buildDir/"D20210116T140629"
  138. const subDir = dname/"sub"
  139. const subDir2 = dname/"sub2"
  140. const brokenSymlinkName = "D20210101T191320_BROKEN_SYMLINK"
  141. const brokenSymlink = dname/brokenSymlinkName
  142. const brokenSymlinkSrc = "D20210101T191320_nonexistent"
  143. const brokenSymlinkCopy = brokenSymlink & "_COPY"
  144. const brokenSymlinkInSubDir = subDir/brokenSymlinkName
  145. const brokenSymlinkInSubDir2 = subDir2/brokenSymlinkName
  146. createDir(subDir)
  147. createSymlink(brokenSymlinkSrc, brokenSymlink)
  148. # Test copyFile
  149. when symlinksAreHandled:
  150. doAssertRaises(OSError):
  151. copyFile(brokenSymlink, brokenSymlinkCopy)
  152. doAssertRaises(OSError):
  153. copyFile(brokenSymlink, brokenSymlinkCopy, {cfSymlinkFollow})
  154. copyFile(brokenSymlink, brokenSymlinkCopy, {cfSymlinkIgnore})
  155. doAssert not fileExists(brokenSymlinkCopy)
  156. copyFile(brokenSymlink, brokenSymlinkCopy, {cfSymlinkAsIs})
  157. when symlinksAreHandled:
  158. doAssert expandSymlink(brokenSymlinkCopy) == brokenSymlinkSrc
  159. removeFile(brokenSymlinkCopy)
  160. else:
  161. doAssert not fileExists(brokenSymlinkCopy)
  162. doAssertRaises(AssertionDefect):
  163. copyFile(brokenSymlink, brokenSymlinkCopy,
  164. {cfSymlinkAsIs, cfSymlinkFollow})
  165. # Test copyFileWithPermissions
  166. when symlinksAreHandled:
  167. doAssertRaises(OSError):
  168. copyFileWithPermissions(brokenSymlink, brokenSymlinkCopy)
  169. doAssertRaises(OSError):
  170. copyFileWithPermissions(brokenSymlink, brokenSymlinkCopy,
  171. options = {cfSymlinkFollow})
  172. copyFileWithPermissions(brokenSymlink, brokenSymlinkCopy,
  173. options = {cfSymlinkIgnore})
  174. doAssert not fileExists(brokenSymlinkCopy)
  175. copyFileWithPermissions(brokenSymlink, brokenSymlinkCopy,
  176. options = {cfSymlinkAsIs})
  177. when symlinksAreHandled:
  178. doAssert expandSymlink(brokenSymlinkCopy) == brokenSymlinkSrc
  179. removeFile(brokenSymlinkCopy)
  180. else:
  181. doAssert not fileExists(brokenSymlinkCopy)
  182. doAssertRaises(AssertionDefect):
  183. copyFileWithPermissions(brokenSymlink, brokenSymlinkCopy,
  184. options = {cfSymlinkAsIs, cfSymlinkFollow})
  185. # Test copyFileToDir
  186. when symlinksAreHandled:
  187. doAssertRaises(OSError):
  188. copyFileToDir(brokenSymlink, subDir)
  189. doAssertRaises(OSError):
  190. copyFileToDir(brokenSymlink, subDir, {cfSymlinkFollow})
  191. copyFileToDir(brokenSymlink, subDir, {cfSymlinkIgnore})
  192. doAssert not fileExists(brokenSymlinkInSubDir)
  193. copyFileToDir(brokenSymlink, subDir, {cfSymlinkAsIs})
  194. when symlinksAreHandled:
  195. doAssert expandSymlink(brokenSymlinkInSubDir) == brokenSymlinkSrc
  196. removeFile(brokenSymlinkInSubDir)
  197. else:
  198. doAssert not fileExists(brokenSymlinkInSubDir)
  199. createSymlink(brokenSymlinkSrc, brokenSymlinkInSubDir)
  200. # Test copyDir
  201. copyDir(subDir, subDir2)
  202. when symlinksAreHandled:
  203. doAssert expandSymlink(brokenSymlinkInSubDir2) == brokenSymlinkSrc
  204. else:
  205. doAssert not fileExists(brokenSymlinkInSubDir2)
  206. removeDir(subDir2)
  207. # Test copyDirWithPermissions
  208. copyDirWithPermissions(subDir, subDir2)
  209. when symlinksAreHandled:
  210. doAssert expandSymlink(brokenSymlinkInSubDir2) == brokenSymlinkSrc
  211. else:
  212. doAssert not fileExists(brokenSymlinkInSubDir2)
  213. removeDir(subDir2)
  214. # Test moveFile
  215. moveFile(brokenSymlink, brokenSymlinkCopy)
  216. when not defined(windows):
  217. doAssert expandSymlink(brokenSymlinkCopy) == brokenSymlinkSrc
  218. else:
  219. doAssert symlinkExists(brokenSymlinkCopy)
  220. removeFile(brokenSymlinkCopy)
  221. # Test moveDir
  222. moveDir(subDir, subDir2)
  223. when not defined(windows):
  224. doAssert expandSymlink(brokenSymlinkInSubDir2) == brokenSymlinkSrc
  225. else:
  226. doAssert symlinkExists(brokenSymlinkInSubDir2)
  227. removeDir(dname)
  228. block: # moveFile
  229. let tempDir = getTempDir() / "D20210609T151608"
  230. createDir(tempDir)
  231. defer: removeDir(tempDir)
  232. writeFile(tempDir / "a.txt", "")
  233. moveFile(tempDir / "a.txt", tempDir / "b.txt")
  234. doAssert not fileExists(tempDir / "a.txt")
  235. doAssert fileExists(tempDir / "b.txt")
  236. removeFile(tempDir / "b.txt")
  237. createDir(tempDir / "moveFile_test")
  238. writeFile(tempDir / "moveFile_test/a.txt", "")
  239. moveFile(tempDir / "moveFile_test/a.txt", tempDir / "moveFile_test/b.txt")
  240. doAssert not fileExists(tempDir / "moveFile_test/a.txt")
  241. doAssert fileExists(tempDir / "moveFile_test/b.txt")
  242. removeDir(tempDir / "moveFile_test")
  243. createDir(tempDir / "moveFile_test")
  244. writeFile(tempDir / "a.txt", "")
  245. moveFile(tempDir / "a.txt", tempDir / "moveFile_test/b.txt")
  246. doAssert not fileExists(tempDir / "a.txt")
  247. doAssert fileExists(tempDir / "moveFile_test/b.txt")
  248. removeDir(tempDir / "moveFile_test")
  249. block: # moveDir
  250. let tempDir = getTempDir() / "D20210609T161443"
  251. createDir(tempDir)
  252. defer: removeDir(tempDir)
  253. createDir(tempDir / "moveDir_test")
  254. moveDir(tempDir / "moveDir_test/", tempDir / "moveDir_test_dest")
  255. doAssert not dirExists(tempDir / "moveDir_test")
  256. doAssert dirExists(tempDir / "moveDir_test_dest")
  257. removeDir(tempDir / "moveDir_test_dest")
  258. createDir(tempDir / "moveDir_test")
  259. writeFile(tempDir / "moveDir_test/a.txt", "")
  260. moveDir(tempDir / "moveDir_test", tempDir / "moveDir_test_dest")
  261. doAssert not dirExists(tempDir / "moveDir_test")
  262. doAssert not fileExists(tempDir / "moveDir_test/a.txt")
  263. doAssert dirExists(tempDir / "moveDir_test_dest")
  264. doAssert fileExists(tempDir / "moveDir_test_dest/a.txt")
  265. removeDir(tempDir / "moveDir_test_dest")
  266. import times
  267. block modificationTime:
  268. # Test get/set modification times
  269. # Should support at least microsecond resolution
  270. let tm = fromUnix(0) + 100.microseconds
  271. writeFile("a", "")
  272. setLastModificationTime("a", tm)
  273. when defined(macosx):
  274. doAssert true
  275. else:
  276. doAssert getLastModificationTime("a") == tm
  277. removeFile("a")
  278. block walkDirRec:
  279. createDir("walkdir_test/a/b")
  280. open("walkdir_test/a/b/file_1", fmWrite).close()
  281. open("walkdir_test/a/file_2", fmWrite).close()
  282. for p in walkDirRec("walkdir_test"):
  283. doAssert p.fileExists
  284. doAssert p.startsWith("walkdir_test")
  285. var s: seq[string]
  286. for p in walkDirRec("walkdir_test", {pcFile}, {pcDir}, relative = true):
  287. s.add(p)
  288. doAssert s.len == 2
  289. doAssert "a" / "b" / "file_1" in s
  290. doAssert "a" / "file_2" in s
  291. removeDir("walkdir_test")
  292. import std/sequtils
  293. block: # walkDir
  294. doAssertRaises(OSError):
  295. for a in walkDir("nonexistent", checkDir = true): discard
  296. doAssertRaises(OSError):
  297. for p in walkDirRec("nonexistent", checkDir = true): discard
  298. when not defined(windows):
  299. block walkDirRelative:
  300. createDir("walkdir_test")
  301. createSymlink(".", "walkdir_test/c")
  302. for k, p in walkDir("walkdir_test", true):
  303. doAssert k == pcLinkToDir
  304. removeDir("walkdir_test")
  305. when defined(posix):
  306. block walkDirSpecial:
  307. createDir("walkdir_test")
  308. doAssert execShellCmd("mkfifo walkdir_test/fifo") == 0
  309. createSymlink("fifo", "walkdir_test/fifo_link")
  310. let withSpecialFiles = toSeq(walkDir("walkdir_test", relative = true))
  311. doAssert (withSpecialFiles.len == 2 and
  312. (pcFile, "fifo") in withSpecialFiles and
  313. (pcLinkToFile, "fifo_link") in withSpecialFiles)
  314. # now Unix special files are excluded from walkdir output:
  315. let skipSpecialFiles = toSeq(walkDir("walkdir_test", relative = true,
  316. skipSpecial = true))
  317. doAssert skipSpecialFiles.len == 0
  318. removeDir("walkdir_test")
  319. block normalizedPath:
  320. doAssert normalizedPath("") == ""
  321. block relative:
  322. doAssert normalizedPath(".") == "."
  323. doAssert normalizedPath("foo/..") == "."
  324. doAssert normalizedPath("foo//../bar/.") == "bar"
  325. doAssert normalizedPath("..") == ".."
  326. doAssert normalizedPath("../") == ".."
  327. doAssert normalizedPath("../..") == unixToNativePath"../.."
  328. doAssert normalizedPath("../a/..") == ".."
  329. doAssert normalizedPath("../a/../") == ".."
  330. doAssert normalizedPath("./") == "."
  331. block absolute:
  332. doAssert normalizedPath("/") == unixToNativePath"/"
  333. doAssert normalizedPath("/.") == unixToNativePath"/"
  334. doAssert normalizedPath("/..") == unixToNativePath"/.."
  335. doAssert normalizedPath("/../") == unixToNativePath"/.."
  336. doAssert normalizedPath("/../..") == unixToNativePath"/../.."
  337. doAssert normalizedPath("/../../") == unixToNativePath"/../.."
  338. doAssert normalizedPath("/../../../") == unixToNativePath"/../../.."
  339. doAssert normalizedPath("/a/b/../../foo") == unixToNativePath"/foo"
  340. doAssert normalizedPath("/a/b/../../../foo") == unixToNativePath"/../foo"
  341. doAssert normalizedPath("/./") == unixToNativePath"/"
  342. doAssert normalizedPath("//") == unixToNativePath"/"
  343. doAssert normalizedPath("///") == unixToNativePath"/"
  344. doAssert normalizedPath("/a//b") == unixToNativePath"/a/b"
  345. doAssert normalizedPath("/a///b") == unixToNativePath"/a/b"
  346. doAssert normalizedPath("/a/b/c/..") == unixToNativePath"/a/b"
  347. doAssert normalizedPath("/a/b/c/../") == unixToNativePath"/a/b"
  348. block isHidden:
  349. when defined(posix):
  350. doAssert ".foo.txt".isHidden
  351. doAssert "bar/.foo.ext".isHidden
  352. doAssert not "bar".isHidden
  353. doAssert not "foo/".isHidden
  354. doAssert ".foo/.".isHidden
  355. # Corner cases: `isHidden` is not yet `..` aware
  356. doAssert not ".foo/..".isHidden
  357. block absolutePath:
  358. doAssertRaises(ValueError): discard absolutePath("a", "b")
  359. doAssert absolutePath("a") == getCurrentDir() / "a"
  360. doAssert absolutePath("a", "/b") == "/b" / "a"
  361. when defined(posix):
  362. doAssert absolutePath("a", "/b/") == "/b" / "a"
  363. doAssert absolutePath("a", "/b/c") == "/b/c" / "a"
  364. doAssert absolutePath("/a", "b/") == "/a"
  365. block splitFile:
  366. doAssert splitFile("") == ("", "", "")
  367. doAssert splitFile("abc/") == ("abc", "", "")
  368. doAssert splitFile("/") == ("/", "", "")
  369. doAssert splitFile("./abc") == (".", "abc", "")
  370. doAssert splitFile(".txt") == ("", ".txt", "")
  371. doAssert splitFile("abc/.txt") == ("abc", ".txt", "")
  372. doAssert splitFile("abc") == ("", "abc", "")
  373. doAssert splitFile("abc.txt") == ("", "abc", ".txt")
  374. doAssert splitFile("/abc.txt") == ("/", "abc", ".txt")
  375. doAssert splitFile("/foo/abc.txt") == ("/foo", "abc", ".txt")
  376. doAssert splitFile("/foo/abc.txt.gz") == ("/foo", "abc.txt", ".gz")
  377. doAssert splitFile(".") == ("", ".", "")
  378. doAssert splitFile("abc/.") == ("abc", ".", "")
  379. doAssert splitFile("..") == ("", "..", "")
  380. doAssert splitFile("a/..") == ("a", "..", "")
  381. doAssert splitFile("/foo/abc....txt") == ("/foo", "abc...", ".txt")
  382. # execShellCmd is tested in tosproc
  383. block ospaths:
  384. doAssert unixToNativePath("") == ""
  385. doAssert unixToNativePath(".") == $CurDir
  386. doAssert unixToNativePath("..") == $ParDir
  387. doAssert isAbsolute(unixToNativePath("/"))
  388. doAssert isAbsolute(unixToNativePath("/", "a"))
  389. doAssert isAbsolute(unixToNativePath("/a"))
  390. doAssert isAbsolute(unixToNativePath("/a", "a"))
  391. doAssert isAbsolute(unixToNativePath("/a/b"))
  392. doAssert isAbsolute(unixToNativePath("/a/b", "a"))
  393. doAssert unixToNativePath("a/b") == joinPath("a", "b")
  394. when defined(macos):
  395. doAssert unixToNativePath("./") == ":"
  396. doAssert unixToNativePath("./abc") == ":abc"
  397. doAssert unixToNativePath("../abc") == "::abc"
  398. doAssert unixToNativePath("../../abc") == ":::abc"
  399. doAssert unixToNativePath("/abc", "a") == "abc"
  400. doAssert unixToNativePath("/abc/def", "a") == "abc:def"
  401. elif doslikeFileSystem:
  402. doAssert unixToNativePath("./") == ".\\"
  403. doAssert unixToNativePath("./abc") == ".\\abc"
  404. doAssert unixToNativePath("../abc") == "..\\abc"
  405. doAssert unixToNativePath("../../abc") == "..\\..\\abc"
  406. doAssert unixToNativePath("/abc", "a") == "a:\\abc"
  407. doAssert unixToNativePath("/abc/def", "a") == "a:\\abc\\def"
  408. else:
  409. #Tests for unix
  410. doAssert unixToNativePath("./") == "./"
  411. doAssert unixToNativePath("./abc") == "./abc"
  412. doAssert unixToNativePath("../abc") == "../abc"
  413. doAssert unixToNativePath("../../abc") == "../../abc"
  414. doAssert unixToNativePath("/abc", "a") == "/abc"
  415. doAssert unixToNativePath("/abc/def", "a") == "/abc/def"
  416. block extractFilenameTest:
  417. doAssert extractFilename("") == ""
  418. when defined(posix):
  419. doAssert extractFilename("foo/bar") == "bar"
  420. doAssert extractFilename("foo/bar.txt") == "bar.txt"
  421. doAssert extractFilename("foo/") == ""
  422. doAssert extractFilename("/") == ""
  423. when doslikeFileSystem:
  424. doAssert extractFilename(r"foo\bar") == "bar"
  425. doAssert extractFilename(r"foo\bar.txt") == "bar.txt"
  426. doAssert extractFilename(r"foo\") == ""
  427. doAssert extractFilename(r"C:\") == ""
  428. block lastPathPartTest:
  429. doAssert lastPathPart("") == ""
  430. when defined(posix):
  431. doAssert lastPathPart("foo/bar.txt") == "bar.txt"
  432. doAssert lastPathPart("foo/") == "foo"
  433. doAssert lastPathPart("/") == ""
  434. when doslikeFileSystem:
  435. doAssert lastPathPart(r"foo\bar.txt") == "bar.txt"
  436. doAssert lastPathPart(r"foo\") == "foo"
  437. template canon(x): untyped = normalizePath(x, '/')
  438. doAssert canon"/foo/../bar" == "/bar"
  439. doAssert canon"foo/../bar" == "bar"
  440. doAssert canon"/f/../bar///" == "/bar"
  441. doAssert canon"f/..////bar" == "bar"
  442. doAssert canon"../bar" == "../bar"
  443. doAssert canon"/../bar" == "/../bar"
  444. doAssert canon("foo/../../bar/") == "../bar"
  445. doAssert canon("./bla/blob/") == "bla/blob"
  446. doAssert canon(".hiddenFile") == ".hiddenFile"
  447. doAssert canon("./bla/../../blob/./zoo.nim") == "../blob/zoo.nim"
  448. doAssert canon("C:/file/to/this/long") == "C:/file/to/this/long"
  449. doAssert canon("") == ""
  450. doAssert canon("foobar") == "foobar"
  451. doAssert canon("f/////////") == "f"
  452. doAssert relativePath("/foo/bar//baz.nim", "/foo", '/') == "bar/baz.nim"
  453. doAssert normalizePath("./foo//bar/../baz", '/') == "foo/baz"
  454. doAssert relativePath("/Users/me/bar/z.nim", "/Users/other/bad", '/') == "../../me/bar/z.nim"
  455. doAssert relativePath("/Users/me/bar/z.nim", "/Users/other", '/') == "../me/bar/z.nim"
  456. # `//` is a UNC path, `/` is the current working directory's drive, so can't
  457. # run this test on Windows.
  458. when not doslikeFileSystem:
  459. doAssert relativePath("/Users///me/bar//z.nim", "//Users/", '/') == "me/bar/z.nim"
  460. doAssert relativePath("/Users/me/bar/z.nim", "/Users/me", '/') == "bar/z.nim"
  461. doAssert relativePath("", "/users/moo", '/') == ""
  462. doAssert relativePath("foo", "", '/') == "foo"
  463. doAssert relativePath("/foo", "/Foo", '/') == (when FileSystemCaseSensitive: "../foo" else: ".")
  464. doAssert relativePath("/Foo", "/foo", '/') == (when FileSystemCaseSensitive: "../Foo" else: ".")
  465. doAssert relativePath("/foo", "/fOO", '/') == (when FileSystemCaseSensitive: "../foo" else: ".")
  466. doAssert relativePath("/foO", "/foo", '/') == (when FileSystemCaseSensitive: "../foO" else: ".")
  467. doAssert relativePath("foo", ".", '/') == "foo"
  468. doAssert relativePath(".", ".", '/') == "."
  469. doAssert relativePath("..", ".", '/') == ".."
  470. doAssert relativePath("foo", "foo") == "."
  471. doAssert relativePath("", "foo") == ""
  472. doAssert relativePath("././/foo", "foo//./") == "."
  473. doAssert relativePath(getCurrentDir() / "bar", "foo") == "../bar".unixToNativePath
  474. doAssert relativePath("bar", getCurrentDir() / "foo") == "../bar".unixToNativePath
  475. when doslikeFileSystem:
  476. doAssert relativePath(r"c:\foo.nim", r"C:\") == r"foo.nim"
  477. doAssert relativePath(r"c:\foo\bar\baz.nim", r"c:\foo") == r"bar\baz.nim"
  478. doAssert relativePath(r"c:\foo\bar\baz.nim", r"d:\foo") == r"c:\foo\bar\baz.nim"
  479. doAssert relativePath(r"\foo\baz.nim", r"\foo") == r"baz.nim"
  480. doAssert relativePath(r"\foo\bar\baz.nim", r"\bar") == r"..\foo\bar\baz.nim"
  481. doAssert relativePath(r"\\foo\bar\baz.nim", r"\\foo\bar") == r"baz.nim"
  482. doAssert relativePath(r"\\foo\bar\baz.nim", r"\\foO\bar") == r"baz.nim"
  483. doAssert relativePath(r"\\foo\bar\baz.nim", r"\\bar\bar") == r"\\foo\bar\baz.nim"
  484. doAssert relativePath(r"\\foo\bar\baz.nim", r"\\foo\car") == r"\\foo\bar\baz.nim"
  485. doAssert relativePath(r"\\foo\bar\baz.nim", r"\\goo\bar") == r"\\foo\bar\baz.nim"
  486. doAssert relativePath(r"\\foo\bar\baz.nim", r"c:\") == r"\\foo\bar\baz.nim"
  487. doAssert relativePath(r"\\foo\bar\baz.nim", r"\foo") == r"\\foo\bar\baz.nim"
  488. doAssert relativePath(r"c:\foo.nim", r"\foo") == r"c:\foo.nim"
  489. doAssert joinPath("usr", "") == unixToNativePath"usr"
  490. doAssert joinPath("", "lib") == "lib"
  491. doAssert joinPath("", "/lib") == unixToNativePath"/lib"
  492. doAssert joinPath("usr/", "/lib") == unixToNativePath"usr/lib"
  493. doAssert joinPath("", "") == unixToNativePath"" # issue #13455
  494. doAssert joinPath("", "/") == unixToNativePath"/"
  495. doAssert joinPath("/", "/") == unixToNativePath"/"
  496. doAssert joinPath("/", "") == unixToNativePath"/"
  497. doAssert joinPath("/" / "") == unixToNativePath"/" # weird test case...
  498. doAssert joinPath("/", "/a/b/c") == unixToNativePath"/a/b/c"
  499. doAssert joinPath("foo/", "") == unixToNativePath"foo/"
  500. doAssert joinPath("foo/", "abc") == unixToNativePath"foo/abc"
  501. doAssert joinPath("foo//./", "abc/.//") == unixToNativePath"foo/abc/"
  502. doAssert joinPath("foo", "abc") == unixToNativePath"foo/abc"
  503. doAssert joinPath("", "abc") == unixToNativePath"abc"
  504. doAssert joinPath("zook/.", "abc") == unixToNativePath"zook/abc"
  505. # controversial: inconsistent with `joinPath("zook/.","abc")`
  506. # on linux, `./foo` and `foo` are treated a bit differently for executables
  507. # but not `./foo/bar` and `foo/bar`
  508. doAssert joinPath(".", "/lib") == unixToNativePath"./lib"
  509. doAssert joinPath(".", "abc") == unixToNativePath"./abc"
  510. # cases related to issue #13455
  511. doAssert joinPath("foo", "", "") == "foo"
  512. doAssert joinPath("foo", "") == "foo"
  513. doAssert joinPath("foo/", "") == unixToNativePath"foo/"
  514. doAssert joinPath("foo/", ".") == "foo"
  515. doAssert joinPath("foo", "./") == unixToNativePath"foo/"
  516. doAssert joinPath("foo", "", "bar/") == unixToNativePath"foo/bar/"
  517. # issue #13579
  518. doAssert joinPath("/foo", "../a") == unixToNativePath"/a"
  519. doAssert joinPath("/foo/", "../a") == unixToNativePath"/a"
  520. doAssert joinPath("/foo/.", "../a") == unixToNativePath"/a"
  521. doAssert joinPath("/foo/.b", "../a") == unixToNativePath"/foo/a"
  522. doAssert joinPath("/foo///", "..//a/") == unixToNativePath"/a/"
  523. doAssert joinPath("foo/", "../a") == unixToNativePath"a"
  524. when doslikeFileSystem:
  525. doAssert joinPath("C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools\\", "..\\..\\VC\\vcvarsall.bat") == r"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
  526. doAssert joinPath("C:\\foo", "..\\a") == r"C:\a"
  527. doAssert joinPath("C:\\foo\\", "..\\a") == r"C:\a"
  528. block getTempDir:
  529. block TMPDIR:
  530. # TMPDIR env var is not used if either of these are defined.
  531. when not (defined(tempDir) or defined(windows) or defined(android)):
  532. if existsEnv("TMPDIR"):
  533. let origTmpDir = getEnv("TMPDIR")
  534. putEnv("TMPDIR", "/mytmp")
  535. doAssert getTempDir() == "/mytmp/"
  536. delEnv("TMPDIR")
  537. doAssert getTempDir() == "/tmp/"
  538. putEnv("TMPDIR", origTmpDir)
  539. else:
  540. doAssert getTempDir() == "/tmp/"
  541. block: # getCacheDir
  542. doAssert getCacheDir().dirExists
  543. block isRelativeTo:
  544. doAssert isRelativeTo("/foo", "/")
  545. doAssert isRelativeTo("/foo/bar", "/foo")
  546. doAssert isRelativeTo("foo/bar", "foo")
  547. doAssert isRelativeTo("/foo/bar.nim", "/foo/bar.nim")
  548. doAssert isRelativeTo("./foo/", "foo")
  549. doAssert isRelativeTo("foo", "./foo/")
  550. doAssert isRelativeTo(".", ".")
  551. doAssert isRelativeTo("foo/bar", ".")
  552. doAssert not isRelativeTo("foo/bar.nims", "foo/bar.nim")
  553. doAssert not isRelativeTo("/foo2", "/foo")
  554. block: # quoteShellWindows
  555. doAssert quoteShellWindows("aaa") == "aaa"
  556. doAssert quoteShellWindows("aaa\"") == "aaa\\\""
  557. doAssert quoteShellWindows("") == "\"\""
  558. block: # quoteShellCommand
  559. when defined(windows):
  560. doAssert quoteShellCommand(["a b c", "d", "e"]) == """"a b c" d e"""
  561. doAssert quoteShellCommand(["""ab"c""", r"\", "d"]) == """ab\"c \ d"""
  562. doAssert quoteShellCommand(["""ab"c""", """ \""", "d"]) == """ab\"c " \\" d"""
  563. doAssert quoteShellCommand(["""a\\\b""", """de fg""", "h"]) == """a\\\b "de fg" h"""
  564. doAssert quoteShellCommand(["""a\"b""", "c", "d"]) == """a\\\"b c d"""
  565. doAssert quoteShellCommand(["""a\\b c""", "d", "e"]) == """"a\\b c" d e"""
  566. doAssert quoteShellCommand(["""a\\b\ c""", "d", "e"]) == """"a\\b\ c" d e"""
  567. doAssert quoteShellCommand(["ab", ""]) == """ab """""
  568. block: # quoteShellPosix
  569. doAssert quoteShellPosix("aaa") == "aaa"
  570. doAssert quoteShellPosix("aaa a") == "'aaa a'"
  571. doAssert quoteShellPosix("") == "''"
  572. doAssert quoteShellPosix("a'a") == "'a'\"'\"'a'"
  573. block: # quoteShell
  574. when defined(posix):
  575. doAssert quoteShell("") == "''"
  576. block: # normalizePathEnd
  577. # handle edge cases correctly: shouldn't affect whether path is
  578. # absolute/relative
  579. doAssert "".normalizePathEnd(true) == ""
  580. doAssert "".normalizePathEnd(false) == ""
  581. doAssert "/".normalizePathEnd(true) == $DirSep
  582. doAssert "/".normalizePathEnd(false) == $DirSep
  583. when defined(posix):
  584. doAssert "//".normalizePathEnd(false) == "/"
  585. doAssert "foo.bar//".normalizePathEnd == "foo.bar"
  586. doAssert "bar//".normalizePathEnd(trailingSep = true) == "bar/"
  587. when defined(windows):
  588. doAssert r"C:\foo\\".normalizePathEnd == r"C:\foo"
  589. doAssert r"C:\foo".normalizePathEnd(trailingSep = true) == r"C:\foo\"
  590. # this one is controversial: we could argue for returning `D:\` instead,
  591. # but this is simplest.
  592. doAssert r"D:\".normalizePathEnd == r"D:"
  593. doAssert r"E:/".normalizePathEnd(trailingSep = true) == r"E:\"
  594. doAssert "/".normalizePathEnd == r"\"
  595. import sugar
  596. block: # normalizeExe
  597. doAssert "".dup(normalizeExe) == ""
  598. when defined(posix):
  599. doAssert "foo".dup(normalizeExe) == "./foo"
  600. doAssert "foo/../bar".dup(normalizeExe) == "foo/../bar"
  601. when defined(windows):
  602. doAssert "foo".dup(normalizeExe) == "foo"
  603. block: # isAdmin
  604. let isAzure = existsEnv("TF_BUILD") # xxx factor with testament.specs.isAzure
  605. # In Azure on Windows tests run as an admin user
  606. if isAzure and defined(windows): doAssert isAdmin()
  607. # In Azure on POSIX tests run as a normal user
  608. if isAzure and defined(posix): doAssert not isAdmin()
  609. import sugar
  610. block: # normalizeExe
  611. doAssert "".dup(normalizeExe) == ""
  612. when defined(posix):
  613. doAssert "foo".dup(normalizeExe) == "./foo"
  614. doAssert "foo/../bar".dup(normalizeExe) == "foo/../bar"
  615. when defined(windows):
  616. doAssert "foo".dup(normalizeExe) == "foo"
  617. block: # isAdmin
  618. let isAzure = existsEnv("TF_BUILD") # xxx factor with testament.specs.isAzure
  619. # In Azure on Windows tests run as an admin user
  620. if isAzure and defined(windows): doAssert isAdmin()
  621. # In Azure on POSIX tests run as a normal user
  622. if isAzure and defined(posix): doAssert not isAdmin()
  623. when doslikeFileSystem:
  624. import std/private/ntpath
  625. block: # Bug #19103 UNC paths
  626. # Easiest way of generating a valid, readable and writable UNC path
  627. let tempDir = r"\\?\" & getTempDir()
  628. doAssert dirExists tempDir
  629. createDir tempDir / "test"
  630. removeDir tempDir / "test"
  631. createDir tempDir / "recursive" / "test"
  632. removeDir tempDir / "recursive" / "test"
  633. let tempDir2 = getTempDir()
  634. let (drive, pathNoDrive) = splitDrive(tempDir2)
  635. setCurrentDir drive
  636. doAssert cmpIgnoreCase(getCurrentDir().splitDrive.drive, drive) == 0
  637. # Test `\Users` path syntax on Windows by stripping away drive. `\`
  638. # resolves to the drive in current working directory. This drive will be
  639. # the same as `tempDir2` because of the `setCurrentDir` above.
  640. doAssert pathNoDrive[0] == '\\'
  641. createDir pathNoDrive / "test"
  642. doAssert dirExists pathNoDrive / "test"
  643. removeDir pathNoDrive / "test"
  644. doAssert splitPath("//?/c:") == ("//?/c:", "")
  645. doAssert relativePath("//?/c:///Users//me", "//?/c:", '/') == "Users/me"
  646. doAssert parentDir(r"\\?\c:") == r""
  647. doAssert parentDir(r"//?/c:/Users") == r"\\?\c:"
  648. doAssert parentDir(r"\\localhost\c$") == r""
  649. doAssert parentDir(r"\Users") == r"\"
  650. doAssert tailDir("//?/c:") == ""
  651. doAssert tailDir("//?/c:/Users") == "Users"
  652. doAssert tailDir(r"\\localhost\c$\Windows\System32") == r"Windows\System32"
  653. doAssert isRootDir("//?/c:")
  654. doAssert isRootDir("//?/UNC/localhost/c$")
  655. doAssert not isRootDir(r"\\?\c:\Users")
  656. doAssert parentDirs(r"C:\Users", fromRoot = true).toSeq == @[r"C:\", r"C:\Users"]
  657. doAssert parentDirs(r"C:\Users", fromRoot = false).toSeq == @[r"C:\Users", r"C:"]
  658. doAssert parentDirs(r"\\?\c:\Users", fromRoot = true).toSeq ==
  659. @[r"\\?\c:\", r"\\?\c:\Users"]
  660. doAssert parentDirs(r"\\?\c:\Users", fromRoot = false).toSeq ==
  661. @[r"\\?\c:\Users", r"\\?\c:"]
  662. doAssert parentDirs(r"//localhost/c$/Users", fromRoot = true).toSeq ==
  663. @[r"//localhost/c$/", r"//localhost/c$/Users"]
  664. doAssert parentDirs(r"//?/UNC/localhost/c$/Users", fromRoot = false).toSeq ==
  665. @[r"//?/UNC/localhost/c$/Users", r"\\?\UNC\localhost\c$"]
  666. doAssert parentDirs(r"\Users", fromRoot = true).toSeq == @[r"\", r"\Users"]
  667. doAssert parentDirs(r"\Users", fromRoot = false).toSeq == @[r"\Users", r"\"]
  668. doAssert r"//?/c:" /../ "d/e" == r"\\?\c:\d\e"
  669. doAssert r"//?/c:/Users" /../ "d/e" == r"\\?\c:\d\e"
  670. doAssert r"\\localhost\c$" /../ "d/e" == r"\\localhost\c$\d\e"
  671. doAssert splitFile("//?/c:") == ("//?/c:", "", "")
  672. doAssert splitFile("//?/c:/Users") == ("//?/c:", "Users", "")
  673. doAssert splitFile(r"\\localhost\c$\test.txt") == (r"\\localhost\c$", "test", ".txt")
  674. else:
  675. block: # parentDirs
  676. doAssert parentDirs("/home", fromRoot=true).toSeq == @["/", "/home"]
  677. doAssert parentDirs("/home", fromRoot=false).toSeq == @["/home", "/"]
  678. doAssert parentDirs("home", fromRoot=true).toSeq == @["home"]
  679. doAssert parentDirs("home", fromRoot=false).toSeq == @["home"]
  680. doAssert parentDirs("/home/user", fromRoot=true).toSeq == @["/", "/home/", "/home/user"]
  681. doAssert parentDirs("/home/user", fromRoot=false).toSeq == @["/home/user", "/home", "/"]
  682. doAssert parentDirs("home/user", fromRoot=true).toSeq == @["home/", "home/user"]
  683. doAssert parentDirs("home/user", fromRoot=false).toSeq == @["home/user", "home"]
  684. # https://github.com/nim-lang/Nim/pull/19643#issuecomment-1235102314
  685. block: # isValidFilename
  686. # Negative Tests.
  687. doAssert not isValidFilename("abcd", maxLen = 2)
  688. doAssert not isValidFilename("0123456789", maxLen = 8)
  689. doAssert not isValidFilename("con")
  690. doAssert not isValidFilename("aux")
  691. doAssert not isValidFilename("prn")
  692. doAssert not isValidFilename("OwO|UwU")
  693. doAssert not isValidFilename(" foo")
  694. doAssert not isValidFilename("foo ")
  695. doAssert not isValidFilename("foo.")
  696. doAssert not isValidFilename("con.txt")
  697. doAssert not isValidFilename("aux.bat")
  698. doAssert not isValidFilename("prn.exe")
  699. doAssert not isValidFilename("nim>.nim")
  700. doAssert not isValidFilename(" foo.log")
  701. # Positive Tests.
  702. doAssert isValidFilename("abcd", maxLen = 42.Positive)
  703. doAssert isValidFilename("c0n")
  704. doAssert isValidFilename("foo.aux")
  705. doAssert isValidFilename("bar.prn")
  706. doAssert isValidFilename("OwO_UwU")
  707. doAssert isValidFilename("cron")
  708. doAssert isValidFilename("ux.bat")
  709. doAssert isValidFilename("nim.nim")
  710. doAssert isValidFilename("foo.log")