Skip to content

Commit 77d613e

Browse files
committed
add --check mode to wkg get
which compares the retrieved contents to an existing file, and exits with 0 if no difference or 1 if there is a difference.
1 parent 11d6d34 commit 77d613e

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

crates/wkg/src/main.rs

Lines changed: 37 additions & 12 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 got package matches the existing file at the output
203+
/// path. Output path will not be modified. Program exits with codes
204+
/// simmilar to diff(1): exits with 1 if there were differences, and 0
205+
/// means no differences.
206+
#[arg(long)]
207+
check: bool,
208+
202209
/// Overwrite any existing output file.
203210
#[arg(long)]
204211
overwrite: bool,
@@ -269,6 +276,11 @@ enum Format {
269276

270277
impl GetArgs {
271278
pub async fn run(self) -> anyhow::Result<()> {
279+
ensure!(
280+
!(self.overwrite && self.check),
281+
"Not allowed to specify both --check and --overwrite"
282+
);
283+
272284
let PackageSpec { package, version } = self.package_spec;
273285
let mut config = self.common.load_config().await?;
274286
if let Some(registry) = self.registry_args.registry.clone() {
@@ -371,21 +383,34 @@ impl GetArgs {
371383
} else {
372384
self.output
373385
};
374-
ensure!(
375-
self.overwrite || !output_path.exists(),
376-
"{output_path:?} already exists; you can use '--overwrite' to overwrite it"
377-
);
378386

379-
if let Some(wit) = wit {
380-
std::fs::write(&output_path, wit)
381-
.with_context(|| format!("Failed to write WIT to {output_path:?}"))?
387+
if self.check {
388+
let existing = std::fs::read(&output_path)
389+
.with_context(|| format!("Failed to read {output_path:?}"))?;
390+
let latest = if let Some(wit) = wit {
391+
wit.into_bytes()
392+
} else {
393+
std::fs::read(&tmp_path).with_context(|| format!("Failed to read {tmp_path:?}"))?
394+
};
395+
if existing != latest {
396+
anyhow::bail!("Differences between retrieved and {output_path:?}");
397+
}
382398
} 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());
399+
ensure!(
400+
self.overwrite || !output_path.exists(),
401+
"{output_path:?} already exists; you can use '--overwrite' to overwrite it"
402+
);
388403

404+
if let Some(wit) = wit {
405+
std::fs::write(&output_path, wit)
406+
.with_context(|| format!("Failed to write WIT to {output_path:?}"))?
407+
} else {
408+
tmp_path
409+
.persist(&output_path)
410+
.with_context(|| format!("Failed to persist WASM to {output_path:?}"))?
411+
}
412+
println!("Wrote '{}'", output_path.display());
413+
}
389414
Ok(())
390415
}
391416
}

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)