test_path.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. --
  2. -- tests/base/test_path.lua
  3. -- Automated test suite for the action list.
  4. -- Copyright (c) 2008-2013 Jason Perkins and the Premake project
  5. --
  6. local suite = test.declare("path")
  7. --
  8. -- path.getabsolute() tests
  9. --
  10. function suite.getabsolute_worksWithMissingSubdirs()
  11. local expected = os.getcwd() .. "/a/b/c"
  12. test.isequal(expected, path.getabsolute("a/b/c"))
  13. end
  14. function suite.getabsolute_removesDotDots_onWindows()
  15. test.isequal("c:/ProjectB/bin", path.getabsolute("c:/ProjectA/../ProjectB/bin"))
  16. end
  17. function suite.getabsolute_removesDotDots_OnPosix()
  18. test.isequal("/ProjectB/bin", path.getabsolute("/ProjectA/../ProjectB/bin"))
  19. end
  20. function suite.getabsolute_limitsDotDots_onWindows()
  21. test.isequal("c:/ProjectB/bin", path.getabsolute("c:/ProjectA/../../ProjectB/bin"))
  22. end
  23. function suite.getabsolute_limitsDotDots_OnPosix()
  24. test.isequal("/ProjectB/bin", path.getabsolute("/ProjectA/../../ProjectB/bin"))
  25. end
  26. function suite.getabsolute_removesDot()
  27. test.isequal("/ProjectA/ProjectB/bin", path.getabsolute("/ProjectA/./ProjectB/bin"))
  28. end
  29. function suite.getabsolute_removesTrailingSlash()
  30. test.isequal("/a/b/c", path.getabsolute("/a/b/c/"))
  31. end
  32. function suite.getabsolute_onLeadingEnvVar()
  33. test.isequal("$(HOME)/user", path.getabsolute("$(HOME)/user"))
  34. end
  35. function suite.getabsolute_onLeadingEnvVar_dosStyle()
  36. test.isequal("%HOME%/user", path.getabsolute("%HOME%/user"))
  37. end
  38. function suite.getabsolute_onServerPath()
  39. test.isequal("//Server/Volume", path.getabsolute("//Server/Volume"))
  40. end
  41. function suite.getabsolute_onMultipleEnvVar()
  42. test.isequal("$(HOME)/$(USER)", path.getabsolute("$(HOME)/$(USER)"))
  43. end
  44. function suite.getabsolute_onTrailingEnvVar()
  45. test.isequal("/home/$(USER)", path.getabsolute("/home/$(USER)"))
  46. end
  47. function suite.getabsolute_onLeadingEnvVarQuoted()
  48. test.isequal('"$(HOME)/user"', path.getabsolute('"$(HOME)/user"'))
  49. end
  50. function suite.getabsolute_normalizesPaths()
  51. test.isequal("c:/ProjectB/bin", path.getabsolute("c:\\ProjectB\\bin"))
  52. end
  53. function suite.getabsolute_acceptsTables()
  54. test.isequal({ "/a/b", "/c/d" }, path.getabsolute({ "/a/b", "/c/d" }))
  55. end
  56. function suite.getabsolute_withRelativeTo()
  57. local relto = path.getdirectory(os.getcwd())
  58. local expected = relto .. "/a/b/c"
  59. test.isequal(expected, path.getabsolute("a/b/c", relto))
  60. end
  61. function suite.getabsolute_withRelativeTo_withTrailingSlashes()
  62. local relto = path.getdirectory(os.getcwd())
  63. local expected = relto .. "/a/b/c"
  64. test.isequal(expected, path.getabsolute("a/b/c", relto .. "/"))
  65. end
  66. function suite.getabsolute_acceptsTables_withRelativeTo()
  67. local relto = path.getdirectory(os.getcwd())
  68. test.isequal({ relto .. "/a/b", relto .. "/c/d" }, path.getabsolute({ "a/b", "c/d" }, relto))
  69. end
  70. function suite.getabsolute_leavesDotDot_onShellVar()
  71. test.isequal("$ORIGIN/../libs", path.getabsolute("$ORIGIN/../libs"))
  72. end
  73. function suite.getabsolute_leavesDotDot2_onShellVar()
  74. test.isequal("$ORIGIN/../../libs", path.getabsolute("$ORIGIN/../../libs"))
  75. end
  76. --
  77. -- path.deferred_join() tests
  78. --
  79. function suite.deferred_join_OnMaybeAbsolutePath()
  80. test.isequal("p1\a%{foo}", path.deferredjoin("p1", "%{foo}"))
  81. end
  82. function suite.deferred_join_OnValidParts()
  83. test.isequal("p1/p2", path.deferredjoin("p1", "p2"))
  84. end
  85. function suite.deferred_join_OnAbsoluteath()
  86. test.isequal("/p2", path.deferredjoin("p1", "/p2"))
  87. end
  88. --
  89. -- path.has_deferred_join() tests
  90. --
  91. function suite.has_deferred_join_true()
  92. test.istrue(path.hasdeferredjoin("p1\a%{foo}"))
  93. end
  94. function suite.has_deferred_join_false()
  95. test.isfalse(path.hasdeferredjoin("p1/p2"))
  96. end
  97. function suite.has_deferred_join_true_OnPipe()
  98. test.istrue(path.hasdeferredjoin("c1 p1\a%{foo} | c2"))
  99. end
  100. function suite.has_deferred_join_false_OnPipe()
  101. test.isfalse(path.hasdeferredjoin("c1 p1/p2 | c2"))
  102. end
  103. function suite.has_deferred_join_true_OnOr()
  104. test.istrue(path.hasdeferredjoin("c1 p1\a%{foo} || c2"))
  105. end
  106. function suite.has_deferred_join_false_OnOr()
  107. test.isfalse(path.hasdeferredjoin("c1 p1/p2 || c2"))
  108. end
  109. --
  110. -- path.resolvedeferredjoin() tests
  111. --
  112. function suite.resolve_deferred_join_OnNoDelimiter()
  113. test.isequal("p1", path.resolvedeferredjoin("p1"))
  114. end
  115. function suite.resolve_deferred_join_OnValidParts()
  116. test.isequal("p1/p2", path.resolvedeferredjoin("p1\ap2"))
  117. end
  118. function suite.resolve_deferred_join_OnAbsoluteWindowsPath()
  119. test.isequal("C:/p2", path.resolvedeferredjoin("p1\aC:/p2"))
  120. end
  121. function suite.resolve_deferred_join_OnCurrentDirectory()
  122. test.isequal("p2", path.resolvedeferredjoin(".\ap2"))
  123. end
  124. function suite.resolve_deferred_join_OnBackToBasePath()
  125. test.isequal("", path.resolvedeferredjoin("p1/p2/\a../../"))
  126. end
  127. function suite.resolve_deferred_join_OnBackToBasePathWithoutFinalSlash()
  128. test.isequal("", path.resolvedeferredjoin("p1/p2/\a../.."))
  129. end
  130. function suite.resolve_deferred_join_OnBothUpTwoFolders()
  131. test.isequal("../../../../foo", path.resolvedeferredjoin("../../\a../../foo"))
  132. end
  133. function suite.resolve_deferred_join_OnUptwoFolders()
  134. test.isequal("p1/foo", path.resolvedeferredjoin("p1/p2/p3\a../../foo"))
  135. end
  136. function suite.resolve_deferred_join_OnUptoBase()
  137. test.isequal("foo", path.resolvedeferredjoin("p1/p2/p3\a../../../foo"))
  138. end
  139. function suite.resolve_deferred_join_ignoreLeadingDots()
  140. test.isequal("p1/p2/foo", path.resolvedeferredjoin("p1/p2\a././foo"))
  141. end
  142. function suite.resolve_deferred_join_OnUptoParentOfBase()
  143. test.isequal("../../p1", path.resolvedeferredjoin("p1/p2/p3/p4/p5/p6/p7/\a../../../../../../../../../p1"))
  144. end
  145. function suite.resolve_deferred_join_onMoreThanTwoParts()
  146. test.isequal("p1/p2/p3", path.resolvedeferredjoin("p1\ap2\ap3"))
  147. end
  148. function suite.resolve_deferred_join_removesExtraInternalSlashes()
  149. test.isequal("p1/p2", path.resolvedeferredjoin("p1/\ap2"))
  150. end
  151. function suite.resolve_deferred_join_removesTrailingSlash()
  152. test.isequal("p1/p2", path.resolvedeferredjoin("p1\ap2/"))
  153. end
  154. function suite.resolve_deferred_join_ignoresEmptyParts()
  155. test.isequal("p2", path.resolvedeferredjoin("\ap2\a"))
  156. end
  157. function suite.resolve_deferred_join_canJoinBareSlash()
  158. test.isequal("/Users", path.resolvedeferredjoin("/\aUsers"))
  159. end
  160. function suite.resolve_deferred_join_keepsLeadingEnvVar()
  161. test.isequal("$(ProjectDir)/../../Bin", path.resolvedeferredjoin("$(ProjectDir)\a../../Bin"))
  162. end
  163. function suite.resolve_deferred_join_keepsInternalEnvVar()
  164. test.isequal("$(ProjectDir)/$(TargetName)/../../Bin", path.resolvedeferredjoin("$(ProjectDir)/$(TargetName)\a../../Bin"))
  165. end
  166. function suite.resolve_deferred_join_keepsComplexInternalEnvVar()
  167. test.isequal("$(ProjectDir)/myobj_$(Arch)/../../Bin", path.resolvedeferredjoin("$(ProjectDir)/myobj_$(Arch)\a../../Bin"))
  168. end
  169. function suite.resolve_deferred_join_keepsRecursivePattern()
  170. test.isequal("p1/**.lproj/../p2", path.resolvedeferredjoin("p1/**.lproj\a../p2"))
  171. end
  172. function suite.resolve_deferred_join_keepsVSMacros()
  173. test.isequal("p1/%(Filename).ext", path.resolvedeferredjoin("p1\a%(Filename).ext"))
  174. end
  175. function suite.resolve_deferred_join_noCombineSingleDot()
  176. test.isequal("p1/./../p2", path.resolvedeferredjoin("p1/.\a../p2"))
  177. end
  178. function suite.resolve_deferred_join_absolute_second_part()
  179. test.isequal("$ORIGIN", path.resolvedeferredjoin("foo/bar\a$ORIGIN"))
  180. end
  181. function suite.resolve_deferred_join_absolute_second_part1()
  182. test.isequal("$(FOO)/bar", path.resolvedeferredjoin("foo/bar\a$(FOO)/bar"))
  183. end
  184. function suite.resolve_deferred_join_absolute_second_part2()
  185. test.isequal("%ROOT%/foo", path.resolvedeferredjoin("foo/bar\a%ROOT%/foo"))
  186. end
  187. function suite.resolve_deferred_join_token_in_second_part()
  188. test.isequal("foo/bar/%{test}/foo", path.resolvedeferredjoin("foo/bar\a%{test}/foo"))
  189. end
  190. function suite.resolve_deferred_join_ignoresPipe()
  191. test.isequal("c1 p1/p2 | c2", path.resolvedeferredjoin("c1 p1/p2 | c2"))
  192. end
  193. function suite.resolve_deferred_join_OnPipe()
  194. test.isequal("c1 p1/p2 | c2", path.resolvedeferredjoin("c1 p1\ap2 | c2"))
  195. end
  196. function suite.resolve_deferred_join_ignoresOr()
  197. test.isequal("c1 p1/p2 || c2", path.resolvedeferredjoin("c1 p1/p2 || c2"))
  198. end
  199. function suite.resolve_deferred_join_OnOr()
  200. test.isequal("c1 p1/p2 || c2", path.resolvedeferredjoin("c1 p1\ap2 || c2"))
  201. end
  202. --
  203. -- path.getbasename() tests
  204. --
  205. function suite.getbasename_ReturnsCorrectName_OnDirAndExtension()
  206. test.isequal("filename", path.getbasename("folder/filename.ext"))
  207. end
  208. --
  209. -- path.getdirectory() tests
  210. --
  211. function suite.getdirectory_ReturnsEmptyString_OnNoDirectory()
  212. test.isequal(".", path.getdirectory("filename.ext"))
  213. end
  214. function suite.getdirectory_ReturnsDirectory_OnSingleLevelPath()
  215. test.isequal("dir0", path.getdirectory("dir0/filename.ext"))
  216. end
  217. function suite.getdirectory_ReturnsDirectory_OnMultiLeveLPath()
  218. test.isequal("dir0/dir1/dir2", path.getdirectory("dir0/dir1/dir2/filename.ext"))
  219. end
  220. function suite.getdirectory_ReturnsRootPath_OnRootPathOnly()
  221. test.isequal("/", path.getdirectory("/filename.ext"))
  222. end
  223. --
  224. -- path.getdrive() tests
  225. --
  226. function suite.getdrive_ReturnsNil_OnNotWindows()
  227. test.isnil(path.getdrive("/hello"))
  228. end
  229. function suite.getdrive_ReturnsLetter_OnWindowsAbsolute()
  230. test.isequal("x", path.getdrive("x:/hello"))
  231. end
  232. --
  233. -- path.getextension() tests
  234. --
  235. function suite.getextension_ReturnsEmptyString_OnNoExtension()
  236. test.isequal("", path.getextension("filename"))
  237. end
  238. function suite.getextension_ReturnsEmptyString_OnPathWithDotAndNoExtension()
  239. test.isequal("", path.getextension("/.premake/premake"))
  240. end
  241. function suite.getextension_ReturnsExtension()
  242. test.isequal(".txt", path.getextension("filename.txt"))
  243. end
  244. function suite.getextension_ReturnsExtension_OnPathWithDot()
  245. test.isequal(".lua", path.getextension("/.premake/premake.lua"))
  246. end
  247. function suite.getextension_OnMultipleDots()
  248. test.isequal(".txt", path.getextension("filename.mod.txt"))
  249. end
  250. function suite.getextension_OnLeadingNumeric()
  251. test.isequal(".7z", path.getextension("filename.7z"))
  252. end
  253. function suite.getextension_OnUnderscore()
  254. test.isequal(".a_c", path.getextension("filename.a_c"))
  255. end
  256. function suite.getextension_OnHyphen()
  257. test.isequal(".a-c", path.getextension("filename.a-c"))
  258. end
  259. --
  260. -- path.getrelative() tests
  261. --
  262. function suite.getrelative_ReturnsDot_OnMatchingPaths()
  263. test.isequal(".", path.getrelative("/a/b/c", "/a/b/c"))
  264. end
  265. function suite.getrelative_ReturnsDoubleDot_OnChildToParent()
  266. test.isequal("..", path.getrelative("/a/b/c", "/a/b"))
  267. end
  268. function suite.getrelative_ReturnsDoubleDot_OnSiblingToSibling()
  269. test.isequal("../d", path.getrelative("/a/b/c", "/a/b/d"))
  270. end
  271. function suite.getrelative_ReturnsChildPath_OnParentToChild()
  272. test.isequal("d", path.getrelative("/a/b/c", "/a/b/c/d"))
  273. end
  274. function suite.getrelative_ReturnsChildPath_OnWindowsAbsolute()
  275. test.isequal("obj/debug", path.getrelative("C:/Code/Premake4", "C:/Code/Premake4/obj/debug"))
  276. end
  277. function suite.getrelative_ReturnsChildPath_OnServerPath()
  278. test.isequal("../Volume", path.getrelative("//Server/Shared", "//Server/Volume"))
  279. end
  280. function suite.getrelative_ReturnsAbsPath_OnDifferentDriveLetters()
  281. test.isequal("D:/Files", path.getrelative("C:/Code/Premake4", "D:/Files"))
  282. end
  283. function suite.getrelative_ReturnsAbsPath_OnDollarMacro()
  284. test.isequal("$(SDK_HOME)/include", path.getrelative("C:/Code/Premake4", "$(SDK_HOME)/include"))
  285. end
  286. function suite.getrelative_ReturnsAbsPath_OnRootedPath()
  287. test.isequal("/opt/include", path.getrelative("/home/me/src/project", "/opt/include"))
  288. end
  289. function suite.getrelative_ReturnsAbsPath_OnServerPath()
  290. test.isequal("//Server/Volume", path.getrelative("C:/Files", "//Server/Volume"))
  291. end
  292. function suite.getrelative_ReturnsAbsPath_OnDifferentServers()
  293. test.isequal("//Server/Volume", path.getrelative("//Computer/Users", "//Server/Volume"))
  294. end
  295. function suite.getrelative_ignoresExtraSlashes2()
  296. test.isequal("..", path.getrelative("/a//b/c","/a/b"))
  297. end
  298. function suite.getrelative_ignoresExtraSlashes3()
  299. test.isequal("..", path.getrelative("/a///b/c","/a/b"))
  300. end
  301. function suite.getrelative_ignoresTrailingSlashes()
  302. test.isequal("c", path.getrelative("/a/b/","/a/b/c"))
  303. end
  304. function suite.getrelative_returnsAbsPath_onContactWithFileSysRoot()
  305. test.isequal("C:/Boost/Include", path.getrelative("C:/Code/MyApp", "C:/Boost/Include"))
  306. end
  307. --
  308. -- path.isabsolute() tests
  309. --
  310. function suite.isabsolute_ReturnsTrue_OnAbsolutePosixPath()
  311. test.istrue(path.isabsolute("/a/b/c"))
  312. end
  313. function suite.isabsolute_ReturnsTrue_OnAbsoluteWindowsPathWithDrive()
  314. test.istrue(path.isabsolute("C:/a/b/c"))
  315. end
  316. function suite.isabsolute_ReturnsFalse_OnRelativePath()
  317. test.isfalse(path.isabsolute("a/b/c"))
  318. end
  319. function suite.isabsolute_ReturnsTrue_OnDollarToken()
  320. test.istrue(path.isabsolute("$(SDK_HOME)/include"))
  321. end
  322. function suite.isabsolute_ReturnsTrue_OnDotInDollarToken()
  323. test.istrue(path.isabsolute("$(configuration.libs)/include"))
  324. end
  325. function suite.isabsolute_ReturnsTrue_OnJustADollarSign()
  326. test.istrue(path.isabsolute("$foo/include"))
  327. end
  328. function suite.isabsolute_ReturnsFalse_OnIncompleteDollarToken()
  329. test.isfalse(path.isabsolute("$(foo/include"))
  330. end
  331. function suite.isabsolute_ReturnsTrue_OnEnvVar()
  332. test.istrue(path.isabsolute("%FOO%/include"))
  333. end
  334. function suite.isabsolute_ReturnsFalse_OnEmptyEnvVar()
  335. test.isfalse(path.isabsolute("%%/include"))
  336. end
  337. function suite.isabsolute_ReturnsFalse_OnToken()
  338. test.isfalse(path.isabsolute("%{foo}/include"))
  339. end
  340. --
  341. -- path.join() tests
  342. --
  343. function suite.join_OnValidParts()
  344. test.isequal("p1/p2", path.join("p1", "p2"))
  345. end
  346. function suite.join_OnAbsoluteUnixPath()
  347. test.isequal("/p2", path.join("p1", "/p2"))
  348. end
  349. function suite.join_OnAbsoluteWindowsPath()
  350. test.isequal("C:/p2", path.join("p1", "C:/p2"))
  351. end
  352. function suite.join_OnCurrentDirectory()
  353. test.isequal("p2", path.join(".", "p2"))
  354. end
  355. function suite.join_OnBackToBasePath()
  356. test.isequal("", path.join("p1/p2/", "../../"))
  357. end
  358. function suite.join_OnBackToBasePathWithoutFinalSlash()
  359. test.isequal("", path.join("p1/p2/", "../.."))
  360. end
  361. function suite.join_OnBothUpTwoFolders()
  362. test.isequal("../../../../foo", path.join("../../", "../../foo"))
  363. end
  364. function suite.join_OnUptwoFolders()
  365. test.isequal("p1/foo", path.join("p1/p2/p3", "../../foo"))
  366. end
  367. function suite.join_OnUptoBase()
  368. test.isequal("foo", path.join("p1/p2/p3", "../../../foo"))
  369. end
  370. function suite.join_ignoreLeadingDots()
  371. test.isequal("p1/p2/foo", path.join("p1/p2", "././foo"))
  372. end
  373. function suite.join_OnUptoParentOfBase()
  374. test.isequal("../../p1", path.join("p1/p2/p3/p4/p5/p6/p7/", "../../../../../../../../../p1"))
  375. end
  376. function suite.join_OnNilSecondPart()
  377. test.isequal("p1", path.join("p1", nil))
  378. end
  379. function suite.join_onMoreThanTwoParts()
  380. test.isequal("p1/p2/p3", path.join("p1", "p2", "p3"))
  381. end
  382. function suite.join_removesExtraInternalSlashes()
  383. test.isequal("p1/p2", path.join("p1/", "p2"))
  384. end
  385. function suite.join_removesTrailingSlash()
  386. test.isequal("p1/p2", path.join("p1", "p2/"))
  387. end
  388. function suite.join_ignoresNilParts()
  389. test.isequal("p2", path.join(nil, "p2", nil))
  390. end
  391. function suite.join_ignoresEmptyParts()
  392. test.isequal("p2", path.join("", "p2", ""))
  393. end
  394. function suite.join_canJoinBareSlash()
  395. test.isequal("/Users", path.join("/", "Users"))
  396. end
  397. function suite.join_keepsLeadingEnvVar()
  398. test.isequal("$(ProjectDir)/../../Bin", path.join("$(ProjectDir)", "../../Bin"))
  399. end
  400. function suite.join_keepsInternalEnvVar()
  401. test.isequal("$(ProjectDir)/$(TargetName)/../../Bin", path.join("$(ProjectDir)/$(TargetName)", "../../Bin"))
  402. end
  403. function suite.join_keepsComplexInternalEnvVar()
  404. test.isequal("$(ProjectDir)/myobj_$(Arch)/../../Bin", path.join("$(ProjectDir)/myobj_$(Arch)", "../../Bin"))
  405. end
  406. function suite.join_keepsRecursivePattern()
  407. test.isequal("p1/**.lproj/../p2", path.join("p1/**.lproj", "../p2"))
  408. end
  409. function suite.join_noCombineSingleDot()
  410. test.isequal("p1/./../p2", path.join("p1/.", "../p2"))
  411. end
  412. function suite.join_absolute_second_part()
  413. test.isequal("$ORIGIN", path.join("foo/bar", "$ORIGIN"))
  414. end
  415. function suite.join_absolute_second_part1()
  416. test.isequal("$(FOO)/bar", path.join("foo/bar", "$(FOO)/bar"))
  417. end
  418. function suite.join_absolute_second_part2()
  419. test.isequal("%ROOT%/foo", path.join("foo/bar", "%ROOT%/foo"))
  420. end
  421. function suite.join_token_in_second_part()
  422. test.isequal("foo/bar/%{test}/foo", path.join("foo/bar", "%{test}/foo"))
  423. end
  424. --
  425. -- path.rebase() tests
  426. --
  427. function suite.rebase_WithEndingSlashOnPath()
  428. local cwd = os.getcwd()
  429. test.isequal("src", path.rebase("../src/", cwd, path.getdirectory(cwd)))
  430. end
  431. --
  432. -- path.replaceextension() tests
  433. --
  434. function suite.getabsolute_replaceExtension()
  435. test.isequal("/AB.foo", path.replaceextension("/AB.exe","foo"))
  436. end
  437. function suite.getabsolute_replaceExtensionWithDot()
  438. test.isequal("/AB.foo", path.replaceextension("/AB.exe",".foo"))
  439. end
  440. function suite.getabsolute_replaceExtensionWithDotMultipleDots()
  441. test.isequal("/nunit.framework.foo", path.replaceextension("/nunit.framework.dll",".foo"))
  442. end
  443. function suite.getabsolute_replaceExtensionCompletePath()
  444. test.isequal("/nunit/framework/main.foo", path.replaceextension("/nunit/framework/main.cpp",".foo"))
  445. end
  446. function suite.getabsolute_replaceExtensionWithoutExtension()
  447. test.isequal("/nunit/framework/main.foo", path.replaceextension("/nunit/framework/main",".foo"))
  448. end
  449. function suite.getabsolute_replaceExtensionWithEmptyString()
  450. test.isequal("foo", path.replaceextension("foo.lua",""))
  451. end
  452. --
  453. -- path.translate() tests
  454. --
  455. function suite.translate_ReturnsTranslatedPath_OnValidPath()
  456. test.isequal("dir/dir/file", path.translate("dir\\dir\\file", "/"))
  457. end
  458. function suite.translate_returnsCorrectSeparator_onMixedPath()
  459. local actual = path.translate("dir\\dir/file", "/")
  460. test.isequal("dir/dir/file", actual)
  461. end
  462. function suite.translate_ReturnsTargetOSSeparator_Windows()
  463. _OPTIONS["os"] = "windows"
  464. test.isequal("dir\\dir\\file", path.translate("dir/dir\\file"))
  465. end
  466. function suite.translate_ReturnsTargetOSSeparator_Linux()
  467. _OPTIONS["os"] = "linux"
  468. test.isequal("dir/dir/file", path.translate("dir/dir\\file"))
  469. end
  470. --
  471. -- path.wildcards tests
  472. --
  473. function suite.wildcards_MatchesTrailingStar()
  474. local p = path.wildcards("**/xcode/*")
  475. test.isequal(".*/xcode/[^/]*", p)
  476. end
  477. function suite.wildcards_MatchPlusSign()
  478. local patt = path.wildcards("file+name.*")
  479. local name = "file+name.c"
  480. test.isequal(name, name:match(patt))
  481. end
  482. function suite.wildcards_escapeSpecialChars()
  483. test.isequal("%.%-", path.wildcards(".-"))
  484. end
  485. function suite.wildcards_escapeStar()
  486. test.isequal("vs[^/]*", path.wildcards("vs*"))
  487. end
  488. function suite.wildcards_escapeStarStar()
  489. test.isequal("Images/.*%.bmp", path.wildcards("Images/**.bmp"))
  490. end
  491. --
  492. -- path.normalize tests
  493. --
  494. function suite.normalize_Test1()
  495. local p = path.normalize("d:/game/../test")
  496. test.isequal("d:/test", p)
  497. end
  498. function suite.normalize_Test2()
  499. local p = path.normalize("d:/game/../../test")
  500. test.isequal("d:/../test", p)
  501. end
  502. function suite.normalize_Test3()
  503. local p = path.normalize("../../test")
  504. test.isequal("../../test", p)
  505. end
  506. function suite.normalize_Test4()
  507. local p = path.normalize("../../../test/*.h")
  508. test.isequal("../../../test/*.h", p)
  509. end
  510. function suite.normalize_Test5()
  511. test.isequal("test", path.normalize("./test"))
  512. test.isequal("d:/", path.normalize("d:/"))
  513. test.isequal("d:/", path.normalize("d:/./"))
  514. local p = path.normalize("d:/game/..")
  515. test.isequal("d:/", p)
  516. end
  517. function suite.normalize_trailingDots1()
  518. local p = path.normalize("../game/test/..")
  519. test.isequal("../game", p)
  520. end
  521. function suite.normalize_trailingDots2()
  522. local p = path.normalize("../game/..")
  523. test.isequal("..", p)
  524. end
  525. function suite.normalize_singleDot()
  526. local p = path.normalize("../../p1/p2/p3/p4/./a.pb.cc")
  527. test.isequal("../../p1/p2/p3/p4/a.pb.cc", p)
  528. end
  529. function suite.normalize_trailingSingleDot()
  530. local p = path.normalize("../../p1/p2/p3/p4/./.")
  531. test.isequal("../../p1/p2/p3/p4", p)
  532. end
  533. function suite.normalize()
  534. test.isequal("d:/ProjectB/bin", path.normalize("d:/ProjectA/../ProjectB/bin"))
  535. test.isequal("/ProjectB/bin", path.normalize("/ProjectA/../ProjectB/bin"))
  536. end
  537. function suite.normalize_leadingWhitespaces()
  538. test.isequal("d:/game", path.normalize("\t\n d:/game"))
  539. end
  540. function suite.normalize_multPath()
  541. test.isequal("../a/b ../c/d", path.normalize("../a/b ../c/d"))
  542. test.isequal("d:/test ../a/b", path.normalize("d:/game/../test ../a/b"))
  543. test.isequal("d:/game/test ../a/b", path.normalize("d:/game/./test ../a/b"))
  544. test.isequal("d:/test ../a/b", path.normalize(" d:/game/../test ../a/b"))
  545. test.isequal("d:/game ../a/b", path.normalize(" d:/game ../a/./b"))
  546. test.isequal("d:/game ../a/b", path.normalize("d:/game/ ../a/b"))
  547. test.isequal("d:/game", path.normalize("d:/game/ "))
  548. end
  549. function suite.normalize_legitimateDots()
  550. test.isequal("d:/test/test..test", path.normalize("d:/test/test..test"))
  551. test.isequal("d:/test..test/test", path.normalize("d:/test..test/test"))
  552. test.isequal("d:/test/.test", path.normalize("d:/test/.test"))
  553. test.isequal("d:/.test", path.normalize("d:/test/../.test"))
  554. test.isequal("d:/test", path.normalize("d:/test/.test/.."))
  555. test.isequal("d:/test/..test", path.normalize("d:/test/..test"))
  556. test.isequal("d:/..test", path.normalize("d:/test/../..test"))
  557. test.isequal("d:/test", path.normalize("d:/test/..test/.."))
  558. test.isequal("d:/test/.test", path.normalize("d:/test/..test/../.test"))
  559. test.isequal("d:/test/..test/.test", path.normalize("d:/test/..test/test/../.test"))
  560. test.isequal("d:/test", path.normalize("d:/test/..test/../.test/.."))
  561. end
  562. function suite.normalize_serverpath()
  563. test.isequal("//myawesomeserver/test", path.normalize("//myawesomeserver/test/"))
  564. test.isequal("//myawesomeserver/test", path.normalize("///myawesomeserver/test/"))
  565. end
  566. function suite.normalize_quotedpath()
  567. test.isequal("\"../../test/test/\"", path.normalize("\"../../test/test/\""))
  568. test.isequal("\"../../test/\"", path.normalize("\"../../test/../test/\""))
  569. end
  570. function suite.normalize_withTokens()
  571. -- Premake tokens
  572. test.isequal("%{wks.location}../../test", path.normalize("%{wks.location}../../test"))
  573. -- Visual Studio var
  574. test.isequal("$(SolutionDir)../../test", path.normalize("$(SolutionDir)../../test"))
  575. -- Windows env var
  576. test.isequal("%APPDATA%../../test", path.normalize("%APPDATA%../../test"))
  577. -- Unix env var
  578. test.isequal("${HOME}../../test", path.normalize("${HOME}../../test"))
  579. -- Middle
  580. test.isequal("../../${MYVAR}/../test", path.normalize("../../${MYVAR}/../test"))
  581. -- End
  582. test.isequal("../../test/${MYVAR}", path.normalize("../../test/${MYVAR}"))
  583. end
  584. function suite.normalize_quotedpath_withTokens()
  585. -- Premake tokens
  586. test.isequal("\"%{wks.location}../../test\"", path.normalize("\"%{wks.location}../../test\""))
  587. -- Visual Studio var
  588. test.isequal("\"$(SolutionDir)../../test\"", path.normalize("\"$(SolutionDir)../../test\""))
  589. -- Windows env var
  590. test.isequal("\"%APPDATA%../../test\"", path.normalize("\"%APPDATA%../../test\""))
  591. -- Unix env var
  592. test.isequal("\"${HOME}../../test\"", path.normalize("\"${HOME}../../test\""))
  593. -- Middle
  594. test.isequal("\"../../${MYVAR}/../test\"", path.normalize("\"../../${MYVAR}/../test\""))
  595. -- End
  596. test.isequal("\"../../test/${MYVAR}\"", path.normalize("\"../../test/${MYVAR}\""))
  597. end