diff options
| author | ruki <[email protected]> | 2026-02-05 22:56:08 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-02-05 22:56:08 +0800 |
| commit | 4f828e01189fb138b21b82718edb876f0d36049e (patch) | |
| tree | 6aaf0105b3aae50b8db9512092a58343215ad4ea | |
| parent | be074db5aa346a36529c13e8b58db3600a6f1f32 (diff) | |
| parent | 6f6de142f94b438c36308f7ad774a9ca7a2c945a (diff) | |
Merge pull request #7295 from xmake-io/testout
support test output files
| -rw-r--r-- | tests/actions/test/outputs/fail_hello_xmake.txt | 1 | ||||
| -rw-r--r-- | tests/actions/test/outputs/pass_hello_foo.txt | 1 | ||||
| -rw-r--r-- | tests/actions/test/xmake.lua | 8 | ||||
| -rw-r--r-- | xmake/actions/test/main.lua | 29 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 6 |
5 files changed, 42 insertions, 3 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..9162da829 100644 --- a/xmake/actions/test/main.lua +++ b/xmake/actions/test/main.lua @@ -33,6 +33,30 @@ import("actions.build.main", {rootdir = os.programdir(), alias = "build_action"} import("utils.progress") import("private.utils.target", {alias = "target_utils"}) +-- Load expected outputs from files for pass_output_files/fail_output_files. +-- Resolve relative paths from the target scriptdir. +-- https://github.com/xmake-io/xmake/issues/7255 +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 = path.absolute(file, scriptdir) + 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 +89,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 +135,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 diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 0b8e2e6ab..ae730738e 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -1187,7 +1187,11 @@ function os.isexec(filepath) end end elseif os.isfile(filepath) then - return os._access and os._access(filepath, "x") or true + if os._access then + return os._access(filepath, "x") + else + return true + end end return false end |
