Skip to content

Commit

Permalink
Merge pull request #116 from pchickey/pch/wkg_get_check
Browse files Browse the repository at this point in the history
add `--check` mode to `wkg get`
  • Loading branch information
lann authored Oct 23, 2024
2 parents 11d6d34 + c78bc84 commit cf01da4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 13 deletions.
50 changes: 37 additions & 13 deletions crates/wkg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ struct GetArgs {
#[arg(long, value_enum, default_value = "auto")]
format: Format,

/// Check that the retrieved package matches the existing file at the
/// output path. Output path will not be modified. Program exits with
/// codes similar to diff(1): exits with 1 if there were differences, and
/// 0 means no differences.
#[arg(long, conflicts_with = "overwrite")]
check: bool,

/// Overwrite any existing output file.
#[arg(long)]
overwrite: bool,
Expand Down Expand Up @@ -371,21 +378,38 @@ impl GetArgs {
} else {
self.output
};
ensure!(
self.overwrite || !output_path.exists(),
"{output_path:?} already exists; you can use '--overwrite' to overwrite it"
);

if let Some(wit) = wit {
std::fs::write(&output_path, wit)
.with_context(|| format!("Failed to write WIT to {output_path:?}"))?

if self.check {
let existing = tokio::fs::read(&output_path)
.await
.with_context(|| format!("Failed to read {output_path:?}"))?;
let latest = if let Some(wit) = wit {
wit.into_bytes()
} else {
tokio::fs::read(&tmp_path)
.await
.with_context(|| format!("Failed to read {tmp_path:?}"))?
};
if existing != latest {
anyhow::bail!("Differences between retrieved and {output_path:?}");
}
} else {
tmp_path
.persist(&output_path)
.with_context(|| format!("Failed to persist WASM to {output_path:?}"))?
}
println!("Wrote '{}'", output_path.display());
ensure!(
self.overwrite || !output_path.exists(),
"{output_path:?} already exists; you can use '--overwrite' to overwrite it"
);

if let Some(wit) = wit {
tokio::fs::write(&output_path, wit)
.await
.with_context(|| format!("Failed to write WIT to {output_path:?}"))?
} else {
tmp_path
.persist(&output_path)
.with_context(|| format!("Failed to persist WASM to {output_path:?}"))?
}
println!("Wrote '{}'", output_path.display());
}
Ok(())
}
}
Expand Down
43 changes: 43 additions & 0 deletions crates/wkg/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,46 @@ async fn build_and_publish_with_metadata() {
"Name should match"
);
}

#[tokio::test]
pub async fn check() {
let fixture = common::load_fixture("wasi-http").await;
let output = fixture.temp_dir.path().join("out");

let get = fixture
.command()
.arg("get")
.arg("wasi:http")
.arg("--output")
.arg(&output)
.status()
.await
.unwrap();
assert!(get.success());

let check_same = fixture
.command()
.arg("get")
.arg("--check")
.arg("wasi:http")
.arg("--output")
.arg(&output)
.status()
.await
.unwrap();
assert!(check_same.success());

std::fs::write(&output, vec![1, 2, 3, 4]).expect("overwrite output with bogus contents");

let check_diff = fixture
.command()
.arg("get")
.arg("--check")
.arg("wasi:http")
.arg("--output")
.arg(output)
.status()
.await
.unwrap();
assert!(!check_diff.success());
}

0 comments on commit cf01da4

Please sign in to comment.