Skip to content

Commit 524db31

Browse files
authored
Merge pull request #402 from OpenCoven/test/goal-fixture-runner-current-main
test(commands): share bounded goal fixture runner
2 parents 5a2eda2 + 9e62c05 commit 524db31

4 files changed

Lines changed: 84 additions & 159 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::process::{Command, Stdio};
2+
use std::time::{Duration, Instant};
3+
4+
pub fn run_cases(worker: &str, cases: &[&str]) {
5+
let mut failures = Vec::new();
6+
for case in cases {
7+
let temp = tempfile::tempdir().unwrap();
8+
let home = temp.path().join("home");
9+
let project = temp.path().join("project");
10+
std::fs::create_dir(&home).unwrap();
11+
std::fs::create_dir(&project).unwrap();
12+
let log = temp.path().join("stderr");
13+
let mut child = Command::new(std::env::current_exe().unwrap())
14+
.args(["--exact", worker, "--ignored", "--nocapture"])
15+
.env_clear()
16+
.env("HOME", &home)
17+
.env("USERPROFILE", &home)
18+
.env("COVEN_CODE_HOME", &home)
19+
.env("COVEN_HOME", temp.path().join("coven"))
20+
.env("COVEN_CAVE_HOME", temp.path().join("cave"))
21+
.env("GOAL_FIXTURE_PROJECT", &project)
22+
.env("GOAL_FIXTURE_CASE", case)
23+
.env("GOAL_FIXTURE_DONE", temp.path().join("done"))
24+
.stdin(Stdio::null())
25+
.stdout(Stdio::null())
26+
.stderr(std::fs::File::create(&log).unwrap())
27+
.spawn()
28+
.unwrap();
29+
// The bound only catches a hung worker. Every goal write is a synced
30+
// SQLite commit, and the valid read case makes over 160 of them, which
31+
// took 9-11 s on a macOS host whose disk other builds were using.
32+
let deadline = Instant::now() + Duration::from_secs(60);
33+
let outcome = loop {
34+
match child.try_wait() {
35+
Ok(Some(status)) => break status,
36+
Ok(None) if Instant::now() < deadline => {
37+
std::thread::sleep(Duration::from_millis(10))
38+
}
39+
result => {
40+
let _ = child.kill();
41+
let _ = child.wait();
42+
panic!("{case}: child wait failed: {result:?}");
43+
}
44+
}
45+
};
46+
if outcome.success() {
47+
assert_eq!(std::fs::read(temp.path().join("done")).unwrap(), b"checked");
48+
} else {
49+
failures.push(format!("{case}: {}", std::fs::read_to_string(log).unwrap()));
50+
}
51+
}
52+
assert!(failures.is_empty(), "{}", failures.join("\n"));
53+
}

‎src-rust/crates/commands/tests/goal_budget.rs‎

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,30 @@
1+
#[path = "common/goal.rs"]
2+
mod goal_fixture;
3+
14
use claurst_commands::{CommandContext, CommandResult, GoalCommand, SlashCommand};
25
use claurst_core::{config::Config, cost::CostTracker, GoalStore};
3-
use std::process::{Command, Stdio};
4-
use std::time::{Duration, Instant};
6+
use goal_fixture::run_cases;
57

68
#[test]
79
fn invalid_explicit_goal_budgets_preserve_existing_goal() {
8-
run_cases(&[
9-
"overflow_k",
10-
"overflow_m",
11-
"overflow_integer",
12-
"invalid",
13-
"negative",
14-
"over_storage",
15-
]);
10+
run_cases(
11+
"goal_budget_worker",
12+
&[
13+
"overflow_k",
14+
"overflow_m",
15+
"overflow_integer",
16+
"invalid",
17+
"negative",
18+
"over_storage",
19+
],
20+
);
1621
}
1722
#[test]
1823
fn valid_goal_budgets_preserve_existing_semantics() {
19-
run_cases(&["zero", "plain", "k", "m", "none", "max_storage"]);
20-
}
21-
22-
fn run_cases(cases: &[&str]) {
23-
let mut failures = Vec::new();
24-
for case in cases {
25-
let temp = tempfile::tempdir().unwrap();
26-
let home = temp.path().join("home");
27-
let project = temp.path().join("project");
28-
std::fs::create_dir(&home).unwrap();
29-
std::fs::create_dir(&project).unwrap();
30-
let log = temp.path().join("stderr");
31-
let mut child = Command::new(std::env::current_exe().unwrap())
32-
.args(["--exact", "goal_budget_worker", "--ignored", "--nocapture"])
33-
.env_clear()
34-
.env("COVEN_CODE_HOME", &home)
35-
.env("GOAL_FIXTURE_PROJECT", &project)
36-
.env("GOAL_FIXTURE_CASE", case)
37-
.env("GOAL_FIXTURE_DONE", temp.path().join("done"))
38-
.stdin(Stdio::null())
39-
.stdout(Stdio::null())
40-
.stderr(std::fs::File::create(&log).unwrap())
41-
.spawn()
42-
.unwrap();
43-
let deadline = Instant::now() + Duration::from_secs(10);
44-
let outcome = loop {
45-
match child.try_wait() {
46-
Ok(Some(status)) => break status,
47-
Ok(None) if Instant::now() < deadline => {
48-
std::thread::sleep(Duration::from_millis(10))
49-
}
50-
result => {
51-
let _ = child.kill();
52-
let _ = child.wait();
53-
panic!("{case}: child wait failed: {result:?}");
54-
}
55-
}
56-
};
57-
if outcome.success() {
58-
assert_eq!(std::fs::read(temp.path().join("done")).unwrap(), b"checked");
59-
} else {
60-
failures.push(format!("{case}: {}", std::fs::read_to_string(log).unwrap()));
61-
}
62-
}
63-
assert!(failures.is_empty(), "{}", failures.join("\n"));
24+
run_cases(
25+
"goal_budget_worker",
26+
&["zero", "plain", "k", "m", "none", "max_storage"],
27+
);
6428
}
6529

6630
#[test]

‎src-rust/crates/commands/tests/goal_clear.rs‎

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,18 @@
1+
#[path = "common/goal.rs"]
2+
mod goal_fixture;
3+
14
use claurst_commands::{CommandContext, CommandResult, CovenCommand, GoalCommand, SlashCommand};
25
use claurst_core::{config::Config, cost::CostTracker, Goal, GoalStore};
3-
use std::process::{Command, Stdio};
4-
use std::time::{Duration, Instant};
6+
use goal_fixture::run_cases;
57

68
#[test]
79
fn failed_goal_deletion_is_reported_without_mutating_goals() {
8-
run_cases(&["direct_failure", "coven_failure"]);
10+
run_cases("goal_clear_worker", &["direct_failure", "coven_failure"]);
911
}
1012

1113
#[test]
1214
fn successful_and_absent_goal_deletion_keep_existing_behavior() {
13-
run_cases(&["direct_success", "coven_success"]);
14-
}
15-
16-
fn run_cases(cases: &[&str]) {
17-
let mut failures = Vec::new();
18-
for case in cases {
19-
let temp = tempfile::tempdir().unwrap();
20-
let home = temp.path().join("home");
21-
let project = temp.path().join("project");
22-
std::fs::create_dir(&home).unwrap();
23-
std::fs::create_dir(&project).unwrap();
24-
let log = temp.path().join("stderr");
25-
let mut child = Command::new(std::env::current_exe().unwrap())
26-
.args(["--exact", "goal_clear_worker", "--ignored", "--nocapture"])
27-
.env_clear()
28-
.env("HOME", &home)
29-
.env("USERPROFILE", &home)
30-
.env("COVEN_CODE_HOME", &home)
31-
.env("COVEN_HOME", temp.path().join("coven"))
32-
.env("COVEN_CAVE_HOME", temp.path().join("cave"))
33-
.env("GOAL_FIXTURE_PROJECT", &project)
34-
.env("GOAL_FIXTURE_CASE", case)
35-
.env("GOAL_FIXTURE_DONE", temp.path().join("done"))
36-
.stdin(Stdio::null())
37-
.stdout(Stdio::null())
38-
.stderr(std::fs::File::create(&log).unwrap())
39-
.spawn()
40-
.unwrap();
41-
let deadline = Instant::now() + Duration::from_secs(10);
42-
let outcome = loop {
43-
match child.try_wait() {
44-
Ok(Some(status)) => break status,
45-
Ok(None) if Instant::now() < deadline => {
46-
std::thread::sleep(Duration::from_millis(10))
47-
}
48-
result => {
49-
let _ = child.kill();
50-
let _ = child.wait();
51-
panic!("{case}: child wait failed: {result:?}");
52-
}
53-
}
54-
};
55-
if outcome.success() {
56-
assert_eq!(std::fs::read(temp.path().join("done")).unwrap(), b"checked");
57-
} else {
58-
failures.push(format!("{case}: {}", std::fs::read_to_string(log).unwrap()));
59-
}
60-
}
61-
assert!(failures.is_empty(), "{}", failures.join("\n"));
15+
run_cases("goal_clear_worker", &["direct_success", "coven_success"]);
6216
}
6317

6418
fn assert_goal_unchanged(before: &Goal, after: &Goal) {

‎src-rust/crates/commands/tests/goal_reads.rs‎

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,18 @@
1+
#[path = "common/goal.rs"]
2+
mod goal_fixture;
3+
14
use claurst_commands::{CommandContext, CommandResult, CovenCommand, GoalCommand, SlashCommand};
25
use claurst_core::{config::Config, cost::CostTracker, GoalError, GoalStatus, GoalStore};
3-
use std::process::{Command, Stdio};
4-
use std::time::{Duration, Instant};
6+
use goal_fixture::run_cases;
57

68
#[test]
79
fn corrupt_goal_reads_are_errors_without_mutation() {
8-
run_cases(&["status", "counter"]);
10+
run_cases("goal_read_worker", &["status", "counter"]);
911
}
1012

1113
#[test]
1214
fn missing_and_valid_goal_reads_keep_existing_behavior() {
13-
run_cases(&["missing", "valid"]);
14-
}
15-
16-
fn run_cases(cases: &[&str]) {
17-
let mut failures = Vec::new();
18-
for case in cases {
19-
let temp = tempfile::tempdir().unwrap();
20-
let home = temp.path().join("home");
21-
let project = temp.path().join("project");
22-
std::fs::create_dir(&home).unwrap();
23-
std::fs::create_dir(&project).unwrap();
24-
let log = temp.path().join("stderr");
25-
let mut child = Command::new(std::env::current_exe().unwrap())
26-
.args(["--exact", "goal_read_worker", "--ignored", "--nocapture"])
27-
.env_clear()
28-
.env("HOME", &home)
29-
.env("USERPROFILE", &home)
30-
.env("COVEN_CODE_HOME", &home)
31-
.env("COVEN_HOME", temp.path().join("coven"))
32-
.env("COVEN_CAVE_HOME", temp.path().join("cave"))
33-
.env("GOAL_FIXTURE_PROJECT", &project)
34-
.env("GOAL_FIXTURE_CASE", case)
35-
.env("GOAL_FIXTURE_DONE", temp.path().join("done"))
36-
.stdin(Stdio::null())
37-
.stdout(Stdio::null())
38-
.stderr(std::fs::File::create(&log).unwrap())
39-
.spawn()
40-
.unwrap();
41-
let deadline = Instant::now() + Duration::from_secs(10);
42-
let outcome = loop {
43-
match child.try_wait() {
44-
Ok(Some(status)) => break status,
45-
Ok(None) if Instant::now() < deadline => {
46-
std::thread::sleep(Duration::from_millis(10))
47-
}
48-
result => {
49-
let _ = child.kill();
50-
let _ = child.wait();
51-
panic!("{case}: child wait failed: {result:?}");
52-
}
53-
}
54-
};
55-
if outcome.success() {
56-
assert_eq!(std::fs::read(temp.path().join("done")).unwrap(), b"checked");
57-
} else {
58-
failures.push(format!("{case}: {}", std::fs::read_to_string(log).unwrap()));
59-
}
60-
}
61-
assert!(failures.is_empty(), "{}", failures.join("\n"));
15+
run_cases("goal_read_worker", &["missing", "valid"]);
6216
}
6317

6418
#[derive(Debug, Clone, PartialEq, Eq)]

0 commit comments

Comments
 (0)