|
| 1 | +// A stand-in for mcpp in the tests: answers mcpp's control contract from the |
| 2 | +// golden fixtures (protocol/fixtures, captured from a real mcpp by |
| 3 | +// tools/capture-fixtures.sh). `@ROOT@` in a fixture becomes the directory the |
| 4 | +// command runs in, `@HOME@` the home. |
| 5 | +// |
| 6 | +// MCPPIDE_FIXTURES the fixtures directory (required) |
| 7 | +// MOCK_MCPP_FAIL "build": the build fails with the compiler's diagnostic; |
| 8 | +// "manifest": it fails on the manifest |
| 9 | +// MOCK_MCPP_SLOW_MS the build waits this long before it ends, so a test can |
| 10 | +// cancel it; SIGTERM then leaves the record `cancelled` |
| 11 | +// MOCK_MCPP_LOG every argv is appended here, one line each |
1 | 12 | import std; |
2 | | -int main() { std::println("mock"); } |
| 13 | +import mcpplibs.os; |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +std::string env(const char* name) { |
| 18 | + const char* v = std::getenv(name); |
| 19 | + return v ? std::string(v) : std::string{}; |
| 20 | +} |
| 21 | + |
| 22 | +std::string slurp(const std::filesystem::path& p) { |
| 23 | + std::ifstream in(p, std::ios::binary); |
| 24 | + return std::string(std::istreambuf_iterator<char>(in), {}); |
| 25 | +} |
| 26 | + |
| 27 | +std::string replace_all(std::string s, std::string_view from, std::string_view to) { |
| 28 | + for (auto pos = s.find(from); pos != std::string::npos; pos = s.find(from, pos + to.size())) s.replace(pos, from.size(), to); |
| 29 | + return s; |
| 30 | +} |
| 31 | + |
| 32 | +std::string fixture(std::string_view name) { |
| 33 | + const auto dir = env("MCPPIDE_FIXTURES"); |
| 34 | + auto text = slurp(std::filesystem::path(dir) / name); |
| 35 | + std::error_code ec; |
| 36 | + text = replace_all(std::move(text), "@ROOT@", std::filesystem::current_path(ec).generic_string()); |
| 37 | + text = replace_all(std::move(text), "@HOME@", env("HOME")); |
| 38 | + return text; |
| 39 | +} |
| 40 | + |
| 41 | +void write_record(std::string_view status) { |
| 42 | + std::error_code ec; |
| 43 | + const auto root = std::filesystem::current_path(ec); |
| 44 | + const auto out = root / "target" / "mock-config"; |
| 45 | + std::filesystem::create_directories(out, ec); |
| 46 | + auto record = fixture("result-ok.json"); |
| 47 | + record = replace_all(record, "\"status\": \"ok\"", std::format("\"status\": \"{}\"", status)); |
| 48 | + std::ofstream(out / "result.json", std::ios::binary) << record; |
| 49 | + std::ofstream(root / "target" / ".last-result.json", std::ios::binary) |
| 50 | + << std::format(R"({{"schemaVersion":1,"kind":"mcpp.last-result","kindVersion":1,"data":{{"result":"{}","status":"{}","finishedAt":null}}}})", |
| 51 | + (out / "result.json").generic_string(), status); |
| 52 | +} |
| 53 | + |
| 54 | +// As mcpp does: a build asked to end leaves its record `cancelled`. |
| 55 | +void on_term() { |
| 56 | + write_record("cancelled"); |
| 57 | + std::quick_exit(143); |
| 58 | +} |
| 59 | + |
| 60 | +int build(bool message) { |
| 61 | + const auto fail = env("MOCK_MCPP_FAIL"); |
| 62 | + std::string stream = fixture(fail == "build" ? "build-failed.ndjson" |
| 63 | + : fail == "manifest" ? "build-manifest-error.ndjson" : "build-ok.ndjson"); |
| 64 | + std::println(std::cerr, " Compiling app v0.1.0 (.)"); |
| 65 | + if (const auto slow = env("MOCK_MCPP_SLOW_MS"); !slow.empty()) { |
| 66 | + write_record("running"); |
| 67 | + // A request to end (SIGTERM, a console control event) is taken over and answered as mcpp |
| 68 | + // answers it: the record says `cancelled` (mcpplibs.os.interrupt). |
| 69 | + if (mcpplibs::os::interrupt::wait(std::chrono::milliseconds(std::stoi(slow)))) on_term(); |
| 70 | + } |
| 71 | + if (message) std::cout << stream << std::flush; |
| 72 | + if (fail.empty()) { write_record("ok"); std::println(std::cerr, " Finished dev [unoptimized + debuginfo] in 0.01s"); return 0; } |
| 73 | + if (fail == "build") write_record("failed"); |
| 74 | + return 1; |
| 75 | +} |
| 76 | + |
| 77 | +} // namespace |
| 78 | + |
| 79 | +int main(int argc, char** argv) { |
| 80 | + std::vector<std::string> args(argv + 1, argv + argc); |
| 81 | + if (const auto log = env("MOCK_MCPP_LOG"); !log.empty()) { |
| 82 | + std::ofstream out(log, std::ios::app); |
| 83 | + for (const auto& a : args) out << a << ' '; |
| 84 | + out << '\n'; |
| 85 | + } |
| 86 | + auto has = [&](std::string_view a) { return std::ranges::find(args, a) != args.end(); }; |
| 87 | + auto first = [&](std::size_t i) { return i < args.size() ? args[i] : std::string{}; }; |
| 88 | + if (args.empty()) return 2; |
| 89 | + if (first(0) == "--version") { std::cout << fixture("version.txt"); return 0; } |
| 90 | + if (first(0) == "--protocol-version") { std::cout << fixture("protocol-version.json"); return 0; } |
| 91 | + if (first(0) == "build") return build(has("--message-format")); |
| 92 | + if (first(0) == "test") { std::cout << fixture("test.ndjson"); return 0; } |
| 93 | + if (first(0) == "run" && has("--print-launch")) { std::cout << fixture("launch.json"); return 0; } |
| 94 | + if (first(0) == "search") { std::cout << fixture("search.json"); return 0; } |
| 95 | + if (first(0) == "index" && first(1) == "list") { std::cout << fixture("index-list.json"); return 0; } |
| 96 | + if (first(0) == "pack" && has("--list-formats")) { std::cout << fixture("pack-formats.json"); return 0; } |
| 97 | + if (first(0) == "toolchain" && first(1) == "list") { std::cout << fixture("toolchain-list.json"); return 0; } |
| 98 | + if (first(0) == "cache" && first(1) == "list") { std::cout << fixture("cache-list.json"); return 0; } |
| 99 | + if (first(0) == "emit") { |
| 100 | + if (env("MOCK_MCPP_FAIL") == "manifest") { |
| 101 | + std::cout << replace_all(fixture("build-manifest-error.ndjson"), "\"mcpp.build\"", "\"mcpp.build-database\""); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + std::cout << fixture("build-database.json"); |
| 105 | + return 0; |
| 106 | + } |
| 107 | + if (first(0) == "self" && first(1) == "env") { std::cout << fixture("self-env.json"); return 0; } |
| 108 | + // Everything else is a command a person reads: say what ran. |
| 109 | + std::string line; |
| 110 | + for (const auto& a : args) { if (!line.empty()) line += ' '; line += a; } |
| 111 | + std::println(std::cerr, "mock mcpp: {}", line); |
| 112 | + return 0; |
| 113 | +} |
0 commit comments