Luna/tests/run-tests.cpp
apio eab44307f0
All checks were successful
Build and test / build (push) Successful in 2m9s
run-tests: Avoid starting a shell to spawn test processes
2024-03-14 12:37:59 +01:00

47 lines
1.3 KiB
C++

#include <luna/String.h>
#include <os/ArgumentParser.h>
#include <os/Directory.h>
#include <os/File.h>
#include <os/Process.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/wait.h>
Result<int> luna_main(int argc, char** argv)
{
StringView test_dir;
os::ArgumentParser parser;
parser.add_description("Run the system test suite.");
parser.add_system_program_info("run-tests"_sv);
parser.add_positional_argument(test_dir, "test-dir", "/bin/tests"_sv);
parser.parse(argc, argv);
auto dir = TRY(os::Directory::open(test_dir));
auto files = TRY(dir->list_names(os::Directory::Filter::Hidden));
for (const auto& program : files)
{
auto command = TRY(String::format("%s/%s"_sv, test_dir.chars(), program.chars()));
StringView args[] = { command.view() };
auto pid = TRY(os::Process::spawn(args[0], Slice<StringView> { args, 1 }, false));
int status;
TRY(os::Process::wait(pid, &status));
if (WEXITSTATUS(status) != 0)
{
os::Process::kill(1, SIGQUIT); // Tell init to report a failed test run.
return WEXITSTATUS(status);
}
}
os::println("All tests passed.");
os::Process::kill(1, SIGTERM); // Tell init to report a successful test run.
return 0;
}