self-test.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ---
  2. -- self-test/self-test.lua
  3. --
  4. -- An automated test framework for Premake and its add-on modules.
  5. --
  6. -- Author Jason Perkins
  7. -- Copyright (c) 2008-2016 Jason Perkins and the Premake project.
  8. ---
  9. local p = premake
  10. p.modules.self_test = {}
  11. local m = p.modules.self_test
  12. m._VERSION = p._VERSION
  13. newaction {
  14. trigger = "self-test",
  15. shortname = "Test Premake",
  16. description = "Run Premake's own local unit test suites",
  17. execute = function()
  18. m.executeSelfTest()
  19. end
  20. }
  21. newoption {
  22. trigger = "test-only",
  23. value = "suite[.test]",
  24. description = "For self-test action; run specific suite or test"
  25. }
  26. function m.executeSelfTest()
  27. m.detectDuplicateTests = true
  28. m.loadTestsFromManifests()
  29. m.detectDuplicateTests = false
  30. local tests = {}
  31. local isAction = true
  32. for i, arg in ipairs(_ARGS) do
  33. local _tests, err = m.getTestsWithIdentifier(arg)
  34. if err then
  35. error(err, 0)
  36. end
  37. tests = table.join(tests, _tests)
  38. end
  39. if #tests == 0 or _OPTIONS["test-only"] ~= nil then
  40. local _tests, err = m.getTestsWithIdentifier(_OPTIONS["test-only"])
  41. if err then
  42. error(err, 0)
  43. end
  44. tests = table.join(tests, _tests)
  45. end
  46. local passed, failed = m.runTest(tests)
  47. if failed > 0 then
  48. printf("\n %d FAILED TEST%s", failed, iif(failed > 1, "S", ""))
  49. os.exit(5)
  50. end
  51. end
  52. function m.loadTestsFromManifests()
  53. local mask = path.join(_MAIN_SCRIPT_DIR, "**/tests/_tests.lua")
  54. local manifests = os.matchfiles(mask)
  55. -- TODO: "**" should also match "." but doesn't currently
  56. local top = path.join(_MAIN_SCRIPT_DIR, "tests/_tests.lua")
  57. if os.isfile(top) then
  58. table.insert(manifests, 1, top)
  59. end
  60. for i = 1, #manifests do
  61. local manifest = manifests[i]
  62. _TESTS_DIR = path.getdirectory(manifest)
  63. local files = dofile(manifest)
  64. for i = 1, #files do
  65. local filename = path.join(_TESTS_DIR, files[i])
  66. dofile(filename)
  67. end
  68. end
  69. end
  70. dofile("test_assertions.lua")
  71. dofile("test_declare.lua")
  72. dofile("test_helpers.lua")
  73. dofile("test_runner.lua")
  74. return m