test_os.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. ---
  2. -- tests/base/test_os.lua
  3. -- Automated test suite for the new OS functions.
  4. -- Copyright (c) 2008-2017 Jason Perkins and the Premake project
  5. ---
  6. local suite = test.declare("base_os")
  7. local cwd
  8. function suite.setup()
  9. cwd = os.getcwd()
  10. os.chdir(_TESTS_DIR)
  11. end
  12. function suite.teardown()
  13. os.chdir(cwd)
  14. end
  15. --
  16. -- os.findlib() tests
  17. --
  18. function suite.findlib_FindSystemLib()
  19. if os.istarget("macosx") then
  20. -- macOS no longer stores system libraries on filesystem; see
  21. -- https://developer.apple.com/documentation/macos-release-notes/macos-big-sur-11_0_1-release-notes
  22. elseif os.istarget("windows") then
  23. test.istrue(os.findlib("user32"))
  24. elseif os.istarget("haiku") then
  25. test.istrue(os.findlib("root"))
  26. else
  27. test.istrue(os.findlib("m"))
  28. end
  29. end
  30. function suite.findlib_FailsOnBadLibName()
  31. test.isfalse(os.findlib("NoSuchLibraryAsThisOneHere"))
  32. end
  33. function suite.findheader_stdheaders()
  34. if not os.istarget("windows") and not os.istarget("macosx") then
  35. test.istrue(os.findheader("stdlib.h"))
  36. end
  37. end
  38. function suite.findheader_failure()
  39. test.isfalse(os.findheader("Knights/who/say/Ni.hpp"))
  40. end
  41. --
  42. -- os.isfile() tests
  43. --
  44. function suite.isfile_ReturnsTrue_OnExistingFile()
  45. test.istrue(os.isfile("_tests.lua"))
  46. end
  47. function suite.isfile_ReturnsFalse_OnNonexistantFile()
  48. test.isfalse(os.isfile("no_such_file.lua"))
  49. end
  50. --
  51. -- os.matchdirs() tests
  52. --
  53. function suite.matchdirs_skipsDottedDirs()
  54. local result = os.matchdirs("*")
  55. test.isfalse(table.contains(result, ".."))
  56. end
  57. --
  58. -- os.matchfiles() tests
  59. --
  60. function suite.matchfiles_OnNonRecursive()
  61. local result = os.matchfiles("*.lua")
  62. test.istrue(table.contains(result, "_tests.lua"))
  63. test.isfalse(table.contains(result, "folder/ok.lua"))
  64. end
  65. function suite.matchfiles_Recursive()
  66. local result = os.matchfiles("**.lua")
  67. test.istrue(table.contains(result, "folder/ok.lua"))
  68. end
  69. function suite.matchfiles_SkipsDotDirs_OnRecursive()
  70. local result = os.matchfiles("**.lua")
  71. test.isfalse(table.contains(result, ".svn/text-base/testfx.lua.svn-base"))
  72. end
  73. function suite.matchfiles_OnSubfolderMatch()
  74. local result = os.matchfiles("**/subfolder/*")
  75. test.istrue(table.contains(result, "folder/subfolder/hello.txt"))
  76. test.isfalse(table.contains(result, "premake4.lua"))
  77. end
  78. function suite.matchfiles_OnDotSlashPrefix()
  79. local result = os.matchfiles("./**.lua")
  80. test.istrue(table.contains(result, "folder/ok.lua"))
  81. end
  82. function suite.matchfiles_OnImplicitEndOfString()
  83. local result = os.matchfiles("folder/*.lua")
  84. test.istrue(table.contains(result, "folder/ok.lua"))
  85. test.isfalse(table.contains(result, "folder/ok.lua.2"))
  86. end
  87. function suite.matchfiles_OnLeadingDotSlashWithPath()
  88. local result = os.matchfiles("./folder/*.lua")
  89. test.istrue(table.contains(result, "folder/ok.lua"))
  90. end
  91. function suite.matchfiles_OnDottedFile()
  92. local result = os.matchfiles("base/.*")
  93. test.istrue(table.contains(result, "base/.testDotFile"))
  94. end
  95. function suite.matchfiles_onComboSearch()
  96. local result = os.matchfiles("folder/**/*.txt")
  97. test.istrue(table.contains(result, "folder/subfolder/hello.txt"))
  98. end
  99. function suite.matchfiles_onSymbolicLink()
  100. if os.istarget("macosx")
  101. or os.istarget("linux")
  102. or os.istarget("solaris")
  103. or os.istarget("bsd")
  104. then
  105. os.execute("cd folder && ln -s subfolder symlinkfolder && cd ..")
  106. local result = os.matchfiles("folder/**/*.txt")
  107. os.execute("rm folder/symlinkfolder")
  108. premake.modules.self_test.print(table.tostring(result))
  109. test.istrue(table.contains(result, "folder/symlinkfolder/hello.txt"))
  110. end
  111. end
  112. --
  113. -- os.pathsearch() tests
  114. --
  115. function suite.pathsearch_ReturnsNil_OnNotFound()
  116. test.istrue(os.pathsearch("nosuchfile", "aaa;bbb;ccc") == nil)
  117. end
  118. function suite.pathsearch_ReturnsPath_OnFound()
  119. test.isequal(_TESTS_DIR, os.pathsearch("_tests.lua", _TESTS_DIR))
  120. end
  121. function suite.pathsearch_FindsFile_OnComplexPath()
  122. test.isequal(_TESTS_DIR, os.pathsearch("_tests.lua", "aaa;" .. _TESTS_DIR .. ";bbb"))
  123. end
  124. function suite.pathsearch_NilPathsAllowed()
  125. test.isequal(_TESTS_DIR, os.pathsearch("_tests.lua", nil, _TESTS_DIR, nil))
  126. end
  127. --
  128. -- os.outputof() tests
  129. --
  130. -- Check if outputof returns the command exit code
  131. -- in addition of the command output
  132. function suite.outputof_commandExitCode()
  133. if os.istarget("macosx")
  134. or os.istarget("linux")
  135. or os.istarget("solaris")
  136. or os.istarget("bsd")
  137. then
  138. -- Assumes 'true' and 'false' commands exist
  139. -- which should be the case on all *nix platforms
  140. for cmd, exitcode in pairs ({
  141. ["true"] = 0,
  142. ["false"] = 1
  143. })
  144. do
  145. local o, e = os.outputof(cmd)
  146. test.isequal(e, exitcode)
  147. end
  148. end
  149. end
  150. -- Check outputof content
  151. function suite.outputof_streams_output()
  152. if (os.istarget("macosx")
  153. or os.istarget("linux")
  154. or os.istarget("solaris")
  155. or os.istarget("bsd"))
  156. and os.isdir (_TESTS_DIR)
  157. then
  158. local ob, e = os.outputof ("ls " .. _TESTS_DIR .. "/base")
  159. local oo, e = os.outputof ("ls " .. _TESTS_DIR .. "/base", "output")
  160. test.isequal (oo, ob)
  161. local s, e = string.find (oo, "test_os.lua")
  162. test.istrue(s ~= nil)
  163. local o, e = os.outputof ("ls " .. cwd .. "/base", "error")
  164. test.istrue(o == nil or #o == 0)
  165. end
  166. end
  167. --
  168. -- os.translateCommand() tests
  169. --
  170. function suite.translateCommand_onNoToken()
  171. test.isequal("cp a b", os.translateCommands("cp a b"))
  172. end
  173. function suite.translateCommand_callsProcessor()
  174. os.commandTokens.test = {
  175. copy = function(value) return "test " .. value end
  176. }
  177. test.isequal("test a b", os.translateCommands("{COPY} a b", "test"))
  178. end
  179. function suite.translateCommand_callsProcessor_multipleTokens()
  180. os.commandTokens.test = {
  181. copy = function(value) return "test " .. value end
  182. }
  183. test.isequal("test a b; test c d; test e f;", os.translateCommands("{COPY} a b; {COPY} c d; {COPY} e f;", "test"))
  184. end
  185. --
  186. -- os.translateCommand() windows COPY tests
  187. --
  188. function suite.translateCommand_windowsCopyNoDst()
  189. test.isequal('IF EXIST a\\ (xcopy /Q /E /Y /I a > nul) ELSE (xcopy /Q /Y /I a > nul)', os.translateCommands('{COPY} a', "windows"))
  190. end
  191. function suite.translateCommand_windowsCopyNoDst_ExtraSpace()
  192. test.isequal('IF EXIST a\\ (xcopy /Q /E /Y /I a > nul) ELSE (xcopy /Q /Y /I a > nul)', os.translateCommands('{COPY} a ', "windows"))
  193. end
  194. function suite.translateCommand_windowsCopyNoQuotes()
  195. test.isequal('IF EXIST a\\ (xcopy /Q /E /Y /I a b > nul) ELSE (xcopy /Q /Y /I a b > nul)', os.translateCommands('{COPY} a b', "windows"))
  196. end
  197. function suite.translateCommand_windowsCopyNoQuotes_ExtraSpace()
  198. test.isequal('IF EXIST a\\ (xcopy /Q /E /Y /I a b > nul) ELSE (xcopy /Q /Y /I a b > nul)', os.translateCommands('{COPY} a b ', "windows"))
  199. end
  200. function suite.translateCommand_windowsCopyQuotes()
  201. test.isequal('IF EXIST "a a"\\ (xcopy /Q /E /Y /I "a a" "b" > nul) ELSE (xcopy /Q /Y /I "a a" "b" > nul)', os.translateCommands('{COPY} "a a" "b"', "windows"))
  202. end
  203. function suite.translateCommand_windowsCopyQuotes_ExtraSpace()
  204. test.isequal('IF EXIST "a a"\\ (xcopy /Q /E /Y /I "a a" "b" > nul) ELSE (xcopy /Q /Y /I "a a" "b" > nul)', os.translateCommands('{COPY} "a a" "b" ', "windows"))
  205. end
  206. function suite.translateCommand_windowsCopyNoQuotesDst()
  207. test.isequal('IF EXIST "a a"\\ (xcopy /Q /E /Y /I "a a" b > nul) ELSE (xcopy /Q /Y /I "a a" b > nul)', os.translateCommands('{COPY} "a a" b', "windows"))
  208. end
  209. function suite.translateCommand_windowsCopyNoQuotesDst_ExtraSpace()
  210. test.isequal('IF EXIST "a a"\\ (xcopy /Q /E /Y /I "a a" b > nul) ELSE (xcopy /Q /Y /I "a a" b > nul)', os.translateCommands('{COPY} "a a" b ', "windows"))
  211. end
  212. function suite.translateCommand_windowsCopyNoQuotesSrc()
  213. test.isequal('IF EXIST a\\ (xcopy /Q /E /Y /I a "b" > nul) ELSE (xcopy /Q /Y /I a "b" > nul)', os.translateCommands('{COPY} a "b"', "windows"))
  214. end
  215. function suite.translateCommand_windowsCopyNoQuotesSrc_ExtraSpace()
  216. test.isequal('IF EXIST a\\ (xcopy /Q /E /Y /I a "b" > nul) ELSE (xcopy /Q /Y /I a "b" > nul)', os.translateCommands('{COPY} a "b" ', "windows"))
  217. end
  218. --
  219. -- os.getWindowsRegistry windows tests
  220. --
  221. function suite.getreg_nonExistentValue()
  222. if os.ishost("windows") then
  223. local x = os.getWindowsRegistry("HKCU:Should\\Not\\Exist\\At\\All")
  224. test.isequal(nil, x)
  225. end
  226. end
  227. function suite.getreg_nonExistentDefaultValue()
  228. if os.ishost("windows") then
  229. local x = os.getWindowsRegistry("HKCU:Should\\Not\\Exist\\At\\All\\")
  230. test.isequal(nil, x)
  231. end
  232. end
  233. function suite.getreg_noSeparators()
  234. if os.ishost("windows") then
  235. local x = os.getWindowsRegistry("HKCU:ShouldNotExistAtAll")
  236. test.isequal(nil, x)
  237. end
  238. end
  239. function suite.getreg_namedValue()
  240. if os.ishost("windows") then
  241. local x = os.getWindowsRegistry("HKCU:Environment\\TEMP")
  242. test.istrue(x ~= nil)
  243. end
  244. end
  245. function suite.getreg_namedValueOptSeparator()
  246. if os.ishost("windows") then
  247. local x = os.getWindowsRegistry("HKCU:\\Environment\\TEMP")
  248. test.istrue(x ~= nil)
  249. end
  250. end
  251. function suite.getreg_defaultValue()
  252. if os.ishost("windows") then
  253. local x = os.getWindowsRegistry("HKLM:SYSTEM\\CurrentControlSet\\Control\\SafeBoot\\Minimal\\AppInfo\\")
  254. test.isequal("Service", x)
  255. end
  256. end
  257. --
  258. -- os.listWindowsRegistry windows tests
  259. --
  260. function suite.listreg_nonExistentKey()
  261. if os.ishost("windows") then
  262. local x = os.listWindowsRegistry("HKCU:Should\\Not\\Exist\\At\\All")
  263. test.isequal(nil, x)
  264. end
  265. end
  266. function suite.listreg_nonExistentKeyTrailingBackslash()
  267. if os.ishost("windows") then
  268. local x = os.listWindowsRegistry("HKCU:Should\\Not\\Exist\\At\\All\\")
  269. test.isequal(nil, x)
  270. end
  271. end
  272. function suite.listreg_noSeparators()
  273. if os.ishost("windows") then
  274. local x = os.listWindowsRegistry("HKCU:ShouldNotExistAtAll")
  275. test.isequal(nil, x)
  276. end
  277. end
  278. function suite.listreg_noSeparatorExistingPath()
  279. if os.ishost("windows") then
  280. local x = os.listWindowsRegistry("HKCU:Environment")
  281. test.istrue(x ~= nil and x["TEMP"] ~= nil)
  282. end
  283. end
  284. function suite.listreg_optSeparators()
  285. if os.ishost("windows") then
  286. local x = os.listWindowsRegistry("HKCU:\\Environment\\")
  287. test.istrue(x ~= nil and x["TEMP"] ~= nil)
  288. end
  289. end
  290. function suite.listreg_keyDefaultValueAndStringValueFormat()
  291. if os.ishost("windows") then
  292. local x = os.listWindowsRegistry("HKLM:SYSTEM\\CurrentControlSet\\Control\\SafeBoot\\Minimal\\AppInfo")
  293. test.isequal(x[""]["value"], "Service")
  294. test.isequal(x[""]["type"], "REG_SZ")
  295. end
  296. end
  297. function suite.listreg_numericValueFormat()
  298. if os.ishost("windows") then
  299. local x = os.listWindowsRegistry("HKCU:Console")
  300. test.isequal(type(x["FullScreen"]["value"]), "number")
  301. test.isequal(x["FullScreen"]["type"], "REG_DWORD")
  302. end
  303. end
  304. function suite.listreg_subkeyFormat()
  305. if os.ishost("windows") then
  306. local x = os.listWindowsRegistry("HKLM:")
  307. test.isequal(type(x["SOFTWARE"]), "table")
  308. test.isequal(next(x["SOFTWARE"]), nil)
  309. end
  310. end
  311. --
  312. -- os.getversion tests.
  313. --
  314. function suite.getversion()
  315. local version = os.getversion();
  316. test.istrue(version ~= nil)
  317. end
  318. --
  319. -- os.translateCommandsAndPaths.
  320. --
  321. function suite.translateCommandsAndPaths()
  322. test.isequal('cmdtool "../foo/path1"', os.translateCommandsAndPaths("cmdtool %[path1]", '../foo', '.', 'osx'))
  323. end
  324. function suite.translateCommandsAndPaths_PreserveSlash()
  325. test.isequal('cmdtool "../foo/path1/"', os.translateCommandsAndPaths("cmdtool %[path1/]", '../foo', '.', 'osx'))
  326. end
  327. function suite.translateCommandsAndPaths_MultipleTokens()
  328. test.isequal('cmdtool "../foo/path1" "../foo/path2/"', os.translateCommandsAndPaths("cmdtool %[path1] %[path2/]", '../foo', '.', 'osx'))
  329. end
  330. --
  331. -- Helpers
  332. --
  333. local tmpname = function()
  334. local p = os.tmpname()
  335. os.remove(p) -- just needed on POSIX
  336. return p
  337. end
  338. local tmpfile = function()
  339. local p = tmpname()
  340. if os.ishost("windows") then
  341. os.execute("type nul >" .. p)
  342. else
  343. os.execute("touch " .. p)
  344. end
  345. return p
  346. end
  347. local tmpdir = function()
  348. local p = tmpname()
  349. os.mkdir(p)
  350. return p
  351. end
  352. --
  353. -- os.remove() tests.
  354. --
  355. function suite.remove_ReturnsError_OnNonExistingPath()
  356. local ok, err, exitcode = os.remove(tmpname())
  357. test.isnil(ok)
  358. test.isequal("string", type(err))
  359. test.isequal("number", type(exitcode))
  360. test.istrue(0 ~= exitcode)
  361. end
  362. function suite.remove_ReturnsError_OnDirectory()
  363. local ok, err, exitcode = os.remove(tmpdir())
  364. test.isnil(ok)
  365. test.isequal("string", type(err))
  366. test.isequal("number", type(exitcode))
  367. test.istrue(0 ~= exitcode)
  368. end
  369. function suite.remove_ReturnsTrue_OnFile()
  370. local ok, err, exitcode = os.remove(tmpfile())
  371. test.isequal(true, ok)
  372. test.isnil(err)
  373. test.isnil(exitcode)
  374. end
  375. --
  376. -- os.rmdir() tests.
  377. --
  378. function suite.rmdir_ReturnsError_OnNonExistingPath()
  379. local ok, err = os.rmdir(tmpname())
  380. test.isnil(ok)
  381. test.isequal("string", type(err))
  382. end
  383. function suite.rmdir_ReturnsError_OnFile()
  384. local ok, err = os.rmdir(tmpfile())
  385. test.isnil(ok)
  386. test.isequal("string", type(err))
  387. end
  388. function suite.rmdir_ReturnsTrue_OnDirectory()
  389. local ok, err = os.rmdir(tmpdir())
  390. test.isequal(true, ok)
  391. test.isnil(err)
  392. end