test-fslib.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. (use-modules
  2. ;; for unit testing forms
  3. (srfi srfi-64))
  4. (use-modules
  5. ;; import the module to test
  6. (fslib))
  7. (test-begin "fslib-test")
  8. (test-group
  9. "absolute-fsingp-test"
  10. (define fsing-to-current-dir
  11. (dirname (or (current-filename)
  12. (canonicalize-path "."))))
  13. (define non-existing-file-name
  14. "non-existing-file.txt")
  15. ;; absolute fsing existing
  16. (test-assert (absolute-fsing? (absolute-fsing fsing-to-current-dir)))
  17. ;; absolute fsing not existing
  18. (test-assert (absolute-fsing? (absolute-fsing "/a/b/c")))
  19. (test-assert "absolute-fsing? recognizes relative fsings as not absolute - 00"
  20. (not
  21. (absolute-fsing? "../a/b/c")))
  22. (test-assert "absolute-fsing? recognizes relative fsings as not absolute - 01"
  23. (not
  24. (absolute-fsing? "./a/b/c")))
  25. (test-assert "absolute-fsing? recognizes relative fsings as not absolute - 02"
  26. (absolute-fsing? "/a/../b/c"))
  27. (test-assert "absolute-fsing? recognizes relative fsings as not absolute - 03"
  28. (absolute-fsing? "/a/b/./c"))
  29. ;; non-absolute fsing existing
  30. (test-assert (absolute-fsing? (absolute-fsing (current-filename))))
  31. ;; non-absolute fsing not existing
  32. (test-assert (not (file-exists? non-existing-file-name))))
  33. (test-group
  34. "absolute-fsing-test"
  35. (define fsing-to-current-dir
  36. (dirname (or (current-filename)
  37. (canonicalize-path "."))))
  38. (define non-existing-file-name
  39. "non-existing-file.txt")
  40. (simple-format (current-output-port)
  41. "fsing-to-current-dir in tests: ~a\n"
  42. fsing-to-current-dir)
  43. ;; absolute fsing existing
  44. (test-equal fsing-to-current-dir (absolute-fsing fsing-to-current-dir))
  45. ;; absolute fsing not existing
  46. (test-equal "/a/b/c" (absolute-fsing "/a/b/c"))
  47. ;; non-absolute fsing existing
  48. (test-equal "absolute-fsing gives correct absolute fsing for current directory -- 00"
  49. (string-append fsing-to-current-dir
  50. file-name-separator-string
  51. (basename (current-filename)))
  52. (absolute-fsing (current-filename) #:working-directory fsing-to-current-dir))
  53. ;; non-absolute fsing not existing
  54. (test-assert (not (file-exists? non-existing-file-name)))
  55. (test-equal (string-append fsing-to-current-dir
  56. file-name-separator-string
  57. non-existing-file-name)
  58. (absolute-fsing non-existing-file-name #:working-directory fsing-to-current-dir))
  59. (test-equal "absolute-fsing gives correct fsing for non-existing file in directory -- 00"
  60. (fsing-join (dirname fsing-to-current-dir) non-existing-file-name)
  61. (absolute-fsing non-existing-file-name))
  62. (test-equal "absolute-fsing is able to deal with non-existing fsing prefixed with ../"
  63. (fsing-join (dirname fsing-to-current-dir) ".." "a" "b" "c.txt")
  64. (absolute-fsing "../a/b/c.txt"))
  65. (test-equal "absolute-fsing is able to deal with non-existing fsing prefixed with ../ and canonicalizes it"
  66. (fsing-join (dirname (dirname fsing-to-current-dir))
  67. "a" "b" "c.txt")
  68. (absolute-fsing "../a/b/c.txt"
  69. #:canonicalize #t))
  70. (test-equal "absolute-fsing is able to deal with non-existing fsing prefixed with ./"
  71. (fsing-join (dirname fsing-to-current-dir) "." "a" "b" "c.txt")
  72. (absolute-fsing "./a/b/c.txt"))
  73. (test-equal "absolute-fsing is able to deal with non-existing fsing prefixed with ./ and canonicalizes it"
  74. (fsing-join (dirname fsing-to-current-dir) "a" "b" "c.txt")
  75. (absolute-fsing "./a/b/c.txt"
  76. #:canonicalize #t))
  77. (test-equal "absolute-fsing is able to deal with lots of stuff at once and canonicalizes it"
  78. (fsing-join fsing-to-current-dir
  79. ".." ".." "a" "b" ".." "c" "d." "." ".." "d")
  80. (absolute-fsing "../..//a/b/../c/d././../d"
  81. #:working-directory fsing-to-current-dir))
  82. (test-equal "absolute-fsing is able to deal with lots of stuff at once and canonicalizes it -- 00"
  83. (fsing-join (dirname (dirname fsing-to-current-dir)) "a" "c" "d")
  84. (absolute-fsing "../..//a/b/../c/d././../d"
  85. #:working-directory fsing-to-current-dir
  86. #:canonicalize #t))
  87. (test-equal "absolute-fsing is able to deal with lots of stuff at once and canonicalizes it -- 01"
  88. (fsing-join (dirname (dirname fsing-to-current-dir)) "a" "c" "d." "e")
  89. (absolute-fsing "../..//a/b/../c/d././e"
  90. #:working-directory fsing-to-current-dir
  91. #:canonicalize #t))
  92. (test-equal "absolute-fsing canonicalizes absolute fsing -- 00"
  93. (fsing-join "/a" "c" "d")
  94. (absolute-fsing "/a/b/../c/d/e/.." #:canonicalize #t))
  95. (test-equal "absolute-fsing canonicalizes absolute fsing -- 01"
  96. (fsing-join "/a" "b")
  97. (absolute-fsing "/a/b/./c/d/e/../../../" #:canonicalize #t)))
  98. (test-group
  99. "fsing-join-test"
  100. (test-equal "fsing-join leaves single separator intact"
  101. file-name-separator-string
  102. (fsing-join file-name-separator-string))
  103. (test-equal "fsing-join of simple parts -- 00"
  104. (string-join '("a" "b" "c") file-name-separator-string)
  105. (fsing-join "a" "b" "c"))
  106. (test-equal "fsing-join leaves initial separator intact -- 00"
  107. "/a/b/c"
  108. (fsing-join file-name-separator-string "a" "b" "c"))
  109. (test-equal "fsing-join ignores empty initial string -- 00"
  110. "a/b/c"
  111. (fsing-join "" "a" "b" "c"))
  112. (test-equal "fsing-join makes fsing separator for intermediate empty string"
  113. "a/b/c"
  114. (fsing-join "" "a" "b" "" "c"))
  115. (test-equal "fsing-join restarts fsing when intermediate absolute fsing is found - 00"
  116. "/c"
  117. (fsing-join "a" "b" "/c"))
  118. (test-equal "fsing-join restarts fsing when intermediate absolute fsing is found - 01"
  119. "/b/c"
  120. (fsing-join "" "a" "/b" "c"))
  121. (test-equal "fsing-join restarts fsing when intermediate absolute fsing is found - 02"
  122. "/c"
  123. (fsing-join "" "a" "b" "/c"))
  124. (test-equal "fsing-join can deal with ../ in parts - 00"
  125. "ab/cd/../"
  126. (fsing-join "" "ab" "cd" "../"))
  127. (test-equal "fsing-join can deal with ../ in parts - 01"
  128. "ab/cd/../ef"
  129. (fsing-join "" "ab" "cd" "../" "ef"))
  130. (test-equal "fsing-join can deal with .. in parts - 01"
  131. "ab/cd/.."
  132. (fsing-join "" "ab" "cd" ".."))
  133. (test-equal "fsing-join can deal with .. in parts - 00"
  134. "ab/../ab/cd/ef"
  135. (fsing-join "" "ab" ".." "ab" "cd" "ef"))
  136. (test-equal "fsing-join can deal with . in parts"
  137. "ab/./cd/ef"
  138. (fsing-join "" "ab" "." "cd" "ef"))
  139. (test-equal "fsing-join can deal with multiple / at the end of parts - 00"
  140. ;; The idea is not to add any "/", if a part already ends
  141. ;; in a "/".
  142. "a///b/../c"
  143. (fsing-join "a///" "b" ".." "c"))
  144. (test-equal "fsing-join can deal with multiple / at the end of parts - 01"
  145. "/b/../c"
  146. (fsing-join "a///" "/b" ".." "c"))
  147. (test-equal "fsing-join can deal with initial / for the first argument - 00"
  148. "/a/bc/def/second/part.txt"
  149. (fsing-join "/a/bc/def" "second/part.txt"))
  150. (test-equal "fsing-join can deal with initial / for the first argument - 01"
  151. "/a/bc/def/second/part"
  152. (fsing-join "/a/bc/def" "second/part"))
  153. (test-equal "fsing-join can deal with initial / for the first argument - 02"
  154. "/a/bc/def/second"
  155. (fsing-join "/a/bc/def" "second"))
  156. (test-equal "fsing-join can deal with initial / for the first argument - 03"
  157. "/a/bc/def/second/"
  158. (fsing-join "/a/bc/def" "second/")))
  159. (test-group
  160. "file-extension-test"
  161. (test-equal "file-extension gets correct extension for relative fsing"
  162. "txt"
  163. (file-extension "../..//a/b/../c/d././../d/new.txt"))
  164. (test-equal "file-extension gets correct extension for absolute fsing"
  165. "png"
  166. (file-extension "/a/b/../c/d././../d/new.png"))
  167. (test-equal "file-extension gets correct extension for fsing without extension"
  168. #f
  169. (file-extension "/a/b/../c/d././../d/no-extension-here"))
  170. (test-equal "file-extension gets correct extension for fsing with trailing dot"
  171. #f
  172. (file-extension "/a/b/../c/d././../d/no-extension-here.")))
  173. (test-group
  174. "file-name-test"
  175. ;; normal cases for 2 different file extensions
  176. ;; relative
  177. (test-equal "file-name gets correct name - 00"
  178. "myfilename"
  179. (file-name "../d/myfilename.txt"))
  180. (test-equal "file-name gets correct name - 01"
  181. "my-filename2"
  182. (file-name "../d/my-filename2.json"))
  183. ;; absolute
  184. (test-equal "file-name gets correct name - 02"
  185. "myfilename"
  186. (file-name "/../d/myfilename.txt"))
  187. (test-equal "file-name gets correct name - 03"
  188. "myfilename"
  189. (file-name "/../d/myfilename.json"))
  190. ;; files with multiple extensions, 2 and 3 and 4 extensions
  191. ;; relative
  192. (test-equal "file-name gets correct name - 04"
  193. "myfilename.abc"
  194. (file-name "../d/myfilename.abc.txt"))
  195. (test-equal "file-name gets correct name - 05"
  196. "myfilename.abc.txt"
  197. (file-name "../d/myfilename.abc.txt.blablabla"))
  198. (test-equal "file-name gets correct name - 06"
  199. "myfilename.abc.txt.blablabla"
  200. (file-name "../d/myfilename.abc.txt.blablabla.x-y-z"))
  201. ;; absolute
  202. (test-equal "file-name gets correct name - 07"
  203. "myfilename.abc"
  204. (file-name "/../d/myfilename.abc.txt"))
  205. (test-equal "file-name gets correct name - 08"
  206. "myfilename.abc.txt"
  207. (file-name "/../d/myfilename.abc.txt.blablabla"))
  208. (test-equal "file-name gets correct name - 09"
  209. "myfilename.abc.txt.blablabla"
  210. (file-name "/../d/myfilename.abc.txt.blablabla.x-y-z"))
  211. ;; files with only an extension
  212. ;; relative
  213. (test-equal "file-name gets correct name - 10"
  214. #f
  215. (file-name "../d/.txt"))
  216. ;; absolute
  217. (test-equal "file-name gets correct name - 11"
  218. ".txt"
  219. (file-name "../d/.txt.blaaa"))
  220. ;; files with only a name
  221. ;; relative
  222. (test-equal "file-name gets correct name - 12"
  223. "txt"
  224. (file-name "../d/txt"))
  225. ;; absolute
  226. (test-equal "file-name gets correct name - 13"
  227. "txt-json"
  228. (file-name "/../d/txt-json"))
  229. ;; very simple only files no fsing
  230. ;; relative
  231. (test-equal "file-name gets correct name - 14"
  232. "txt"
  233. (file-name "txt"))
  234. ;; absolute
  235. (test-equal "file-name gets correct name - 15"
  236. "txt-json"
  237. (file-name "txt-json")))
  238. (test-group
  239. "sub-fsingp-test"
  240. (test-assert "sub-fsing? recognizes sub fsing of complex fsing -- 00"
  241. (sub-fsing? "../..//a/b/../c/d././../d/new.txt" "../..//a/b/../c/d././../d/"))
  242. (test-assert "sub-fsing? recognizes sub fsing of complex fsing -- 01"
  243. (sub-fsing? "/a/../a/b/c/e/../d" "/a/b/c/d/"))
  244. (test-assert "sub-fsing? recognizes sub fsing of complex fsing -- 02"
  245. (sub-fsing? "/a/../a/b/c/e/../" "/a/b/c/d/.."))
  246. (test-assert "sub-fsing? recognizes non-sub fsing of complex fsing -- 00"
  247. (sub-fsing? "/a/../a/b/c/e/" "/a/b/c/d/.."))
  248. (test-assert "sub-fsing? recognizes non-sub fsing of complex fsing -- 01"
  249. (sub-fsing? "/a/../a/b/c/" "/a/b/c/d/.."))
  250. (test-assert "sub-fsing? equal fsing is sub-fsing -- 00"
  251. (sub-fsing? "/a/b/c/" "/a/b/c/"))
  252. (test-assert "sub-fsing? equal fsing is sub-fsing -- 01"
  253. (sub-fsing? "/a/../a/b/c/" "/a/b/c/d/.."))
  254. (test-assert "sub-fsing? recognizes non-sub-fsings"
  255. (not (sub-fsing? "/a/b/c/d" "/a/b/d")))
  256. (test-assert "sub-fsing? recognizes non-sub-fsings"
  257. (not (sub-fsing? "/a/b/c/d" "d"))))
  258. (test-group
  259. "parent-fsing"
  260. ;; for files not directories
  261. (test-equal "parent-fsing returns containing directory for files -- 00"
  262. "/a/b/c"
  263. (parent-fsing "/a/b/c/my-file.txt"))
  264. (test-equal "parent-fsing returns containing directory for files -- 01"
  265. "/a/b/c"
  266. (parent-fsing "/a/b/c/my-file"))
  267. (test-equal "parent-fsing returns containing directory for files -- 02"
  268. "/a/b/c"
  269. (parent-fsing "/a/b/c/file"))
  270. (test-equal "parent-fsing returns containing directory for files -- 03"
  271. "a/b/c"
  272. (parent-fsing "a/b/c/my-file.txt"))
  273. (test-equal "parent-fsing returns containing directory for files -- 04"
  274. "a/b/c"
  275. (parent-fsing "a/b/c/my-file"))
  276. (test-equal "parent-fsing returns containing directory for files -- 05"
  277. "a/b/c"
  278. (parent-fsing "a/b/c/file"))
  279. ;; for directories
  280. (test-equal "parent-fsing returns parent directory -- 00"
  281. "/a/b"
  282. (parent-fsing "/a/b/c/"))
  283. (test-equal "parent-fsing returns parent directory -- 01"
  284. "/a/b"
  285. (parent-fsing "/a/b/c")))
  286. ;; Finish the testsuite, and report results.
  287. (test-end "fslib-test")