Skip to content

Commit d60b92b

Browse files
jobs: a run's environment overrides, and a test of a build, a failed build and a cancelled one over the mock mcpp
Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent 3f68035 commit d60b92b

3 files changed

Lines changed: 84 additions & 2 deletions

File tree

‎src/jobs.cppm‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ struct Request {
4141
bool supersede = false; // cancel a running job of this kind
4242
std::vector<std::string> deny;
4343
std::string client = "mcpp-ide";
44+
// Variables set (or, with no value, removed) for this run, over the backend's environment.
45+
std::vector<std::pair<std::string, std::optional<std::string>>> env;
4446
};
4547

4648
struct Sink {
@@ -240,6 +242,7 @@ void Jobs::execute(const std::shared_ptr<Job>& job) {
240242
o.deny = job->request.deny;
241243
o.client = job->request.client;
242244
o.color = job->request.color;
245+
o.extraEnv = job->request.env;
243246
mcpp::Client client;
244247
{
245248
std::lock_guard lock(mutex_);

‎src/mcpp.cppm‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ struct Protocol {
3737
};
3838

3939
// `mcpp --version` and `mcpp --protocol-version`.
40-
std::expected<Protocol, std::string> handshake(const std::filesystem::path& program);
40+
// The environment overrides apply to these two runs as to every run of mcpp the backend makes.
41+
using Environment = std::vector<std::pair<std::string, std::optional<std::string>>>;
42+
std::expected<Protocol, std::string> handshake(const std::filesystem::path& program, const Environment& env = {});
4143

4244
// Compares dotted versions numerically ("2026.10.26.2" > "2026.10.26.1").
4345
int compare_versions(std::string_view a, std::string_view b);
@@ -135,11 +137,12 @@ std::expected<Located, std::string> locate(const std::string& configuredPath) {
135137
"mcpp was not found: set mcpp.path, put mcpp on PATH, or install it (xlings install mcpp)"));
136138
}
137139

138-
std::expected<Protocol, std::string> handshake(const std::filesystem::path& program) {
140+
std::expected<Protocol, std::string> handshake(const std::filesystem::path& program, const Environment& env) {
139141
mcpplibs::os::process::spawn_options o;
140142
o.program = program;
141143
o.args = {"--version"};
142144
o.in = mcpplibs::os::process::stdio::null;
145+
o.env = env;
143146
auto v = mcpplibs::os::process::run(o);
144147
if (!v) return std::unexpected(v.error().message());
145148
if (!v->status.success()) return std::unexpected(std::format("{} --version exited {}", program.string(), v->status.shell_code()));

‎tests/test_jobs.cpp‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// mcppide.jobs over the mock mcpp: a build streams its records and ends `ok`, a failed build ends
2+
// `failed` with its diagnostics, and a cancelled one ends `cancelled` with mcpp's record saying so.
3+
#include "support.hpp"
4+
import std;
5+
import nlohmann.json;
6+
import mcpplibs.os;
7+
import mcppide.mcpp;
8+
import mcppide.jobs;
9+
10+
namespace jobs = mcppide::jobs;
11+
using Json = nlohmann::json;
12+
13+
namespace {
14+
struct Seen {
15+
std::mutex lock;
16+
std::vector<Json> records;
17+
std::map<std::string, Json> finished;
18+
};
19+
20+
} // namespace
21+
22+
int main() {
23+
const auto mock = testsupport::built("mcpp-ide-mock-mcpp");
24+
CHECK(!mock.empty());
25+
// The mock reads its fixtures and its behaviour from the environment of each run.
26+
const mcppide::mcpp::Environment fixtures{{"MCPPIDE_FIXTURES", testsupport::fixtures().generic_string()}};
27+
auto protocol = mcppide::mcpp::handshake(mock, fixtures);
28+
CHECK(protocol.has_value() && protocol->contract);
29+
30+
const auto root = std::filesystem::temp_directory_path() / std::format("mcppide-jobs-{}", mcpplibs::os::process::self()->id);
31+
std::filesystem::create_directories(root);
32+
Seen seen;
33+
jobs::Sink sink;
34+
sink.record = [&](std::string_view, const Json& r) { std::lock_guard g(seen.lock); seen.records.push_back(r); };
35+
sink.finished = [&](std::string_view id, const Json& s) { std::lock_guard g(seen.lock); seen.finished[std::string(id)] = s; };
36+
jobs::Jobs runner(mcppide::mcpp::Client(mock, *protocol), sink);
37+
38+
jobs::Request build{.kind = jobs::Kind::build, .root = root, .args = {"build", "--message-format", "json"}, .mode = jobs::Mode::ndjson};
39+
build.env = fixtures;
40+
const auto ok = runner.submit(build);
41+
runner.drain();
42+
{
43+
std::lock_guard g(seen.lock);
44+
CHECK(seen.finished.contains(ok));
45+
CHECK(seen.finished[ok]["status"] == "ok");
46+
CHECK(!seen.records.empty());
47+
CHECK(seen.finished[ok].contains("envelope"));
48+
}
49+
50+
auto failing = build;
51+
failing.env.push_back({"MOCK_MCPP_FAIL", "build"});
52+
const auto failed = runner.submit(failing);
53+
runner.drain();
54+
{
55+
std::lock_guard g(seen.lock);
56+
CHECK(seen.finished[failed]["status"] == "failed");
57+
}
58+
59+
// A build that takes its time, cancelled while it runs.
60+
auto slowly = build;
61+
slowly.env.push_back({"MOCK_MCPP_SLOW_MS", "20000"});
62+
const auto slow = runner.submit(slowly);
63+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
64+
CHECK(runner.cancel(slow));
65+
runner.drain();
66+
{
67+
std::lock_guard g(seen.lock);
68+
CHECK(seen.finished[slow]["status"] == "cancelled");
69+
}
70+
const auto record = testsupport::read(root / "target" / ".last-result.json");
71+
CHECK(record.find("\"cancelled\"") != std::string::npos);
72+
73+
std::error_code ec;
74+
std::filesystem::remove_all(root, ec);
75+
std::println("test_jobs: ok");
76+
}

0 commit comments

Comments
 (0)