summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-02-06 00:09:03 +0800
committerruki <[email protected]>2026-02-06 00:09:03 +0800
commit75296c694d98cd1dbd624f6e38f69d167d91a3c7 (patch)
tree61b670384fa5b3bb0d16c3ff3b3c98fc97ec55ad
parentbe074db5aa346a36529c13e8b58db3600a6f1f32 (diff)
support test output files
-rw-r--r--tests/actions/test/outputs/fail_hello_xmake.txt1
-rw-r--r--tests/actions/test/outputs/pass_hello_foo.txt1
-rw-r--r--tests/actions/test/xmake.lua8
-rw-r--r--xmake/actions/test/main.lua32
4 files changed, 40 insertions, 2 deletions
diff --git a/tests/actions/test/outputs/fail_hello_xmake.txt b/tests/actions/test/outputs/fail_hello_xmake.txt
new file mode 100644
index 000000000..32650dceb
--- /dev/null
+++ b/tests/actions/test/outputs/fail_hello_xmake.txt
@@ -0,0 +1 @@
+hello xmake
diff --git a/tests/actions/test/outputs/pass_hello_foo.txt b/tests/actions/test/outputs/pass_hello_foo.txt
new file mode 100644
index 000000000..dfbb21580
--- /dev/null
+++ b/tests/actions/test/outputs/pass_hello_foo.txt
@@ -0,0 +1 @@
+hello foo
diff --git a/tests/actions/test/xmake.lua b/tests/actions/test/xmake.lua
index d8de81f9a..eb1cef888 100644
--- a/tests/actions/test/xmake.lua
+++ b/tests/actions/test/xmake.lua
@@ -13,6 +13,13 @@ for _, file in ipairs(os.files("src/test_*.cpp")) do
add_tests("fail_output", {fail_outputs = {"hello2 .*", "hello xmake"}})
end
+target("test_output_files")
+ set_kind("binary")
+ set_default(false)
+ add_files("src/test_1.cpp")
+ add_tests("pass_output_file", {trim_output = true, runargs = "foo", pass_output_files = "outputs/pass_hello_foo.txt"})
+ add_tests("fail_output_file", {trim_output = true, should_fail = true, fail_output_files = "outputs/fail_hello_xmake.txt"})
+
target("test_10")
set_kind("binary")
set_default(false)
@@ -48,4 +55,3 @@ target("test_timeout")
set_default(false)
add_files("src/run_timeout.cpp")
add_tests("run_timeout", {run_timeout = 1000})
-
diff --git a/xmake/actions/test/main.lua b/xmake/actions/test/main.lua
index 85866814a..5e0f761cd 100644
--- a/xmake/actions/test/main.lua
+++ b/xmake/actions/test/main.lua
@@ -33,6 +33,33 @@ import("actions.build.main", {rootdir = os.programdir(), alias = "build_action"}
import("utils.progress")
import("private.utils.target", {alias = "target_utils"})
+function _load_output_files(target, files, opt)
+ files = table.wrap(files)
+ if #files == 0 then
+ return {}
+ end
+ local scriptdir = target and target:scriptdir() or nil
+ local outputs = {}
+ for _, file in ipairs(files) do
+ local filepath = file
+ if filepath and not path.is_absolute(filepath) then
+ if scriptdir then
+ filepath = path.join(scriptdir, filepath)
+ end
+ end
+ filepath = path.absolute(filepath)
+ if not os.isfile(filepath) then
+ raise("file not found: %s", filepath)
+ end
+ local data = io.readfile(filepath)
+ if opt.trim_output and data then
+ data = data:trim()
+ end
+ table.insert(outputs, data)
+ end
+ return outputs
+end
+
-- test target
function _do_test_target(target, opt)
opt = opt or {}
@@ -65,7 +92,8 @@ function _do_test_target(target, opt)
if opt.realtime_output then
outfile = nil
errfile = nil
- assert(not opt.pass_outputs and not opt.fail_outputs, "add_tests(%s): we cannot check pass_outputs/fail_outputs in real output mode", opt.name)
+ assert(not opt.pass_outputs and not opt.fail_outputs and not opt.pass_output_files and not opt.fail_output_files,
+ "add_tests(%s): we cannot check pass_outputs/fail_outputs in real output mode", opt.name)
else
os.tryrm(outfile)
os.tryrm(errfile)
@@ -110,6 +138,8 @@ function _do_test_target(target, opt)
if ok == 0 then
local pass_outputs = table.wrap(opt.pass_outputs)
local fail_outputs = table.wrap(opt.fail_outputs)
+ table.join2(pass_outputs, _load_output_files(target, opt.pass_output_files, opt))
+ table.join2(fail_outputs, _load_output_files(target, opt.fail_output_files, opt))
for _, pass_output in ipairs(pass_outputs) do
if opt.plain then
if pass_output == outdata then