Skip to content

Commit cf01da4

Browse files
authored
Merge pull request #116 from pchickey/pch/wkg_get_check
add `--check` mode to `wkg get`
2 parents 11d6d34 + c78bc84 commit cf01da4

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

crates/wkg/src/main.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ struct GetArgs {
199199
#[arg(long, value_enum, default_value = "auto")]
200200
format: Format,
201201

202+
/// Check that the retrieved package matches the existing file at the
203+
/// output path. Output path will not be modified. Program exits with
204+
/// codes similar to diff(1): exits with 1 if there were differences, and
205+
/// 0 means no differences.
206+
#[arg(long, conflicts_with = "overwrite")]
207+
check: bool,
208+
202209
/// Overwrite any existing output file.
203210
#[arg(long)]
204211
overwrite: bool,
@@ -371,21 +378,38 @@ impl GetArgs {
371378
} else {
372379
self.output
373380
};
374-
ensure!(
375-
self.overwrite || !output_path.exists(),
376-
"{output_path:?} already exists; you can use '--overwrite' to overwrite it"
377-
);
378-
379-
if let Some(wit) = wit {
380-
std::fs::write(&output_path, wit)
381-
.with_context(|| format!("Failed to write WIT to {output_path:?}"))?
381+
382+
if self.check {
383+
let existing = tokio::fs::read(&output_path)
384+
.await
385+
.with_context(|| format!("Failed to read {output_path:?}"))?;
386+
let latest = if let Some(wit) = wit {
387+
wit.into_bytes()
388+
} else {
389+
tokio::fs::read(&tmp_path)
390+
.await
391+
.with_context(|| format!("Failed to read {tmp_path:?}"))?
392+
};
393+
if existing != latest {
394+
anyhow::bail!("Differences between retrieved and {output_path:?}");
395+
}
382396
} else {
383-
tmp_path
384-
.persist(&output_path)
385-
.with_context(|| format!("Failed to persist WASM to {output_path:?}"))?
386-
}
387-
println!("Wrote '{}'", output_path.display());
397+
ensure!(
398+
self.overwrite || !output_path.exists(),
399+
"{output_path:?} already exists; you can use '--overwrite' to overwrite it"
400+
);
388401

402+
if let Some(wit) = wit {
403+
tokio::fs::write(&output_path, wit)
404+
.await
405+
.with_context(|| format!("Failed to write WIT to {output_path:?}"))?
406+
} else {
407+
tmp_path
408+
.persist(&output_path)
409+
.with_context(|| format!("Failed to persist WASM to {output_path:?}"))?
410+
}
411+
println!("Wrote '{}'", output_path.display());
412+
}
389413
Ok(())
390414
}
391415
}

crates/wkg/tests/e2e.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,46 @@ async fn build_and_publish_with_metadata() {
8787
"Name should match"
8888
);
8989
}
90+
91+
#[tokio::test]
92+
pub async fn check() {
93+
let fixture = common::load_fixture("wasi-http").await;
94+
let output = fixture.temp_dir.path().join("out");
95+
96+
let get = fixture
97+
.command()
98+
.arg("get")
99+
.arg("wasi:http")
100+
.arg("--output")
101+
.arg(&output)
102+
.status()
103+
.await
104+
.unwrap();
105+
assert!(get.success());
106+
107+
let check_same = fixture
108+
.command()
109+
.arg("get")
110+
.arg("--check")
111+
.arg("wasi:http")
112+
.arg("--output")
113+
.arg(&output)
114+
.status()
115+
.await
116+
.unwrap();
117+
assert!(check_same.success());
118+
119+
std::fs::write(&output, vec![1, 2, 3, 4]).expect("overwrite output with bogus contents");
120+
121+
let check_diff = fixture
122+
.command()
123+
.arg("get")
124+
.arg("--check")
125+
.arg("wasi:http")
126+
.arg("--output")
127+
.arg(output)
128+
.status()
129+
.await
130+
.unwrap();
131+
assert!(!check_diff.success());
132+
}

0 commit comments

Comments
 (0)