|
| 1 | +// mcpplibs.os on this runner: the platform, a published file, a list of directories, and a |
| 2 | +// program started and read (this test, started again). |
| 3 | +import std; |
| 4 | +import mcpplibs.os; |
| 5 | + |
| 6 | +namespace os = mcpplibs::os; |
| 7 | + |
| 8 | +int main(int argc, char** argv) { |
| 9 | + if (argc > 1 && std::string_view(argv[1]) == "--echo") { |
| 10 | + (void)os::stdio::write_output("echoed"); |
| 11 | + return 0; |
| 12 | + } |
| 13 | + int failures = 0; |
| 14 | + auto check = [&](bool ok, std::string_view what) { if (!ok) { ++failures; std::println(std::cerr, "FAIL: {}", what); } }; |
| 15 | + |
| 16 | + check(os::host() == os::PLATFORM, "built for a system, the host is that system"); |
| 17 | + const auto dir = os::dirs::temp() / std::format("os-tests-{}", os::process::self()->id); |
| 18 | + std::filesystem::create_directories(dir); |
| 19 | + const auto file = dir / "published.txt"; |
| 20 | + check(os::fs::publish(file, "first").has_value(), "a free name is published"); |
| 21 | + check(!os::fs::publish(file, "second").has_value(), "a published name is not replaced"); |
| 22 | + check(os::fs::read(file).value_or("") == "first", "and keeps what was published first"); |
| 23 | + |
| 24 | + const char sep = os::conventions().path_list_separator; |
| 25 | + const auto parts = os::env::split_list(std::string("a") + sep + sep + "b"); |
| 26 | + check(parts.size() == 2, "a list of directories splits at the host's separator"); |
| 27 | + |
| 28 | + const auto again = os::env::self_command(); |
| 29 | + if (!again.empty()) { |
| 30 | + os::process::spawn_options o; |
| 31 | + o.program = again.front(); |
| 32 | + o.args.assign(again.begin() + 1, again.end()); |
| 33 | + o.args.push_back("--echo"); |
| 34 | + auto r = os::process::run(o); |
| 35 | + check(r && r->status.success() && r->out == "echoed", "a started program's output is read"); |
| 36 | + } |
| 37 | + std::error_code ec; |
| 38 | + std::filesystem::remove_all(dir, ec); |
| 39 | + if (failures == 0) std::println("os-tests: held"); |
| 40 | + return failures; |
| 41 | +} |
0 commit comments