summaryrefslogtreecommitdiff
path: root/tests/runner.lua
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-06-08 13:34:02 +0800
committerOpportunityLiu <[email protected]>2019-06-08 13:34:02 +0800
commit1af88102687e493cab40e62b0a2b8b7d42417ae6 (patch)
tree272a80b424e68024a9ecda138159864d9659fe5a /tests/runner.lua
parente165a8e43abed4eb2eca64c3b5adefb57cd65600 (diff)
improve test lib
Diffstat (limited to 'tests/runner.lua')
-rw-r--r--tests/runner.lua74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/runner.lua b/tests/runner.lua
new file mode 100644
index 000000000..6ec74a35c
--- /dev/null
+++ b/tests/runner.lua
@@ -0,0 +1,74 @@
+import("core.base.option")
+import("test_utils.test_assert")
+import("test_utils.print_error")
+import("test_utils.build", { alias = "test_build" })
+
+function main(script)
+
+ if os.isdir(script) then
+ script = path.join(script, "test.lua")
+ end
+ script = path.absolute(script)
+ assert(path.filename(script) == "test.lua")
+ assert(os.isfile(script))
+
+ -- disable statistics
+ os.setenv("XMAKE_STATS", "false")
+
+ -- init test context
+ local context =
+ {
+ filename = script
+ }
+ context = test_assert(context)
+ function context:build(argv)
+ test_build(argv)
+ end
+
+ local root = path.directory(script)
+
+ local verbose = option.get("verbose") or option.get("diagnosis")
+
+ -- trace
+ cprint(">> testing %s ...", path.relative(root))
+
+ -- enter script directory
+ local old_dir = os.cd(root)
+
+ -- get test functions
+ local data = import("test", { root_dir = root })
+
+ if data.main then
+ -- ignore everthing when we found a main function
+ data = { test_main = data.main }
+ end
+
+ -- run test
+ local succeed_count = 0
+ for k, v in pairs(data) do
+ if k:startswith("test") and type(v) == "function" then
+ if verbose then print(">> running %s ...", k) end
+ context.func = v
+ context.funcname = k
+ try{
+ function ()
+ v(context)
+ end,
+ catch{
+ function (error)
+ if not error:find("aborting because of ") then
+ print_error(error, v, "unhandled error")
+ else
+ raise(error)
+ end
+ end
+ }
+ }
+ succeed_count = succeed_count + 1
+ end
+ end
+ if verbose then print(">> finined %d test method(s) ...", succeed_count) end
+
+ -- leave script directory
+ os.cd(old_dir)
+end