Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

Say no folder, or no rust-anything.json, for a missing crate in every crate command (#1361)

Abir Kolinc1cf84f118a0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6cf28ea0..c7c97554 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,14 @@
## Unreleased
+- Every command that takes a crate folder (`diff`, `gate`, `record`, `build`, `fuzz`, `bench`,
+ `report`, `shadow`, `enable`, `disable`, `refresh` and `package`) says ``no folder <path> (run
+ `rust-anything bridge` first)`` for a folder that is not there and ``no rust-anything.json in
+ <path> (run `rust-anything bridge` first)`` for one with no manifest, on one line with no
+ operating system error. A manifest that exists but cannot be read keeps its error. Before, they
+ said ``reading <path>/rust-anything.json (run `rust-anything bridge` first): No such file or
+ directory (os error 2)`` or ``resolving <path> (...): No such file or directory (os error 2)``
+ (#1355 by @kolinabir).
- PHP: `shuffle()`, `str_shuffle()`, `array_rand()`, `random_bytes()` and `lcg_value()` count as
randomness, as `rand()` does. Before, a function that picked or shuffled with them was bridged as
pure, and each recorded case was one draw no port could repeat (#1340 by @Alkhanizi-hashem).
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index cccb0d0f..052282e2 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -128,8 +128,8 @@ paths in what you commit. CI runs it on every file.
node, python or go, with "no built library ... (run `rust-anything build <crate>` first)".
- `tests/diff_missing_folder.rs` and `tests/missing_crate.rs`: commands run on a crate folder
that is missing (e.g. `diff` not suggesting `record` when the crate folder has no manifest,
- and `disable`, `report` and `shadow` on a non-existent crate folder naming the folder and
- suggesting `bridge` first).
+ and every crate command on a non-existent or empty crate folder naming it and suggesting
+ `bridge` first).
- `tests/error_messages.rs`: recorded exception messages and the comparison verdict.
- `tests/go_shadow.rs`: the Go shadow wrapper running a port once per call.
- `tests/held_out.rs`: fresh gate inputs and ports that only match their recordings.
diff --git a/src/bridge.rs b/src/bridge.rs
index 36118d20..373ac5fc 100644
--- a/src/bridge.rs
+++ b/src/bridge.rs
@@ -752,15 +752,30 @@ fn keep_php_classes_together(planned: &mut [(ExportedFn, Option<String>)]) {
impl Manifest {
pub fn load(crate_dir: &Path) -> Result<Self> {
let path = crate_dir.join(MANIFEST);
- let text = std::fs::read_to_string(&path).with_context(|| {
- format!(
- "reading {} (run `rust-anything bridge` first)",
- path.display()
- )
- })?;
+ let text = match std::fs::read_to_string(&path) {
+ Ok(text) => text,
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
+ return Err(Self::missing(crate_dir));
+ }
+ Err(e) => {
+ return Err(anyhow::Error::new(e).context(format!(
+ "reading {} (run `rust-anything bridge` first)",
+ path.display()
+ )));
+ }
+ };
Self::parse(&text, crate_dir)
}
+ fn missing(crate_dir: &Path) -> anyhow::Error {
+ let dir = crate_dir.display();
+ if crate_dir.is_dir() {
+ anyhow::anyhow!("no {MANIFEST} in {dir} (run `rust-anything bridge` first)")
+ } else {
+ anyhow::anyhow!("no folder {dir} (run `rust-anything bridge` first)")
+ }
+ }
+
/// The manifest written in `text`, as found in `crate_dir`: `load` without the reading, for a
/// caller that opens the file itself.
pub fn parse(text: &str, crate_dir: &Path) -> Result<Self> {
diff --git a/src/card.rs b/src/card.rs
index eea8343a..f1480bb7 100644
--- a/src/card.rs
+++ b/src/card.rs
@@ -106,13 +106,11 @@ pub struct Options<'a> {
pub fn report(opts: &Options) -> Result<Card> {
anyhow::ensure!(opts.min_ms > 0, "`--min-ms` must be at least 1");
- let crate_dir = opts.crate_dir.canonical().with_context(|| {
- format!(
- "resolving {} (run `rust-anything bridge` first)",
- opts.crate_dir.display()
- )
- })?;
- let manifest = Manifest::load(&crate_dir)?;
+ let manifest = Manifest::load(opts.crate_dir)?;
+ let crate_dir = opts
+ .crate_dir
+ .canonical()
+ .with_context(|| format!("resolving {}", opts.crate_dir.display()))?;
let port = crate::state::port_version(&crate_dir);
let _lock = crate::fuzz::RecordingLock::take_recovered(&crate_dir)?;
// A card says what the recorded cases showed, so the recording has to be the one `record`
diff --git a/src/enable.rs b/src/enable.rs
index 787a1ae7..c9fe1173 100644
--- a/src/enable.rs
+++ b/src/enable.rs
@@ -139,13 +139,10 @@ pub fn enable_only(
only: Option<&[String]>,
extra_skipped: &[String],
) -> Result<Switched> {
- let crate_dir = crate_dir.canonical().with_context(|| {
- format!(
- "resolving {} (run `rust-anything bridge` first)",
- crate_dir.display()
- )
- })?;
- let manifest = Manifest::load(&crate_dir)?;
+ let manifest = Manifest::load(crate_dir)?;
+ let crate_dir = crate_dir
+ .canonical()
+ .with_context(|| format!("resolving {}", crate_dir.display()))?;
supported_in(&crate_dir, &manifest)?;
let selected: Option<Vec<&str>> = match only {
Some(names) => {
@@ -518,13 +515,10 @@ pub fn select_matching(
}
pub fn disable(crate_dir: &Path) -> Result<Switched> {
- let crate_dir = crate_dir.canonical().with_context(|| {
- format!(
- "resolving {} (run `rust-anything bridge` first)",
- crate_dir.display()
- )
- })?;
- let manifest = Manifest::load(&crate_dir)?;
+ let manifest = Manifest::load(crate_dir)?;
+ let crate_dir = crate_dir
+ .canonical()
+ .with_context(|| format!("resolving {}", crate_dir.display()))?;
let mut out = Switched {
schema: crate::state::schema("disable"),
at: crate::state::now(),
diff --git a/src/main.rs b/src/main.rs
index 0a649343..cfc18c6f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1672,16 +1672,8 @@ fn run() -> Result<ExitCode> {
if let Some(t) = float_tolerance {
rust_anything::tolerance::check_bounded(t, allow_large_tolerance)?;
}
- // The same missing-folder hint the other commands give before they
- // read the manifest. Otherwise a missing crate reaches the harness
- // and reports "run record first", which cannot work yet.
- let manifest_path = crate_dir.join(rust_anything::bridge::MANIFEST);
- if !manifest_path.exists() {
- anyhow::bail!(
- "reading {} (run `rust-anything bridge` first)",
- manifest_path.display()
- );
- }
+ // Before the harness, which would otherwise say "run record first" for a crate that is not there.
+ rust_anything::bridge::Manifest::load(&crate_dir)?;
let opts = harness::DiffOptions {
crate_dir: &crate_dir,
module: module.as_deref(),
diff --git a/src/shadow.rs b/src/shadow.rs
index cb332f3c..d9cb193a 100644
--- a/src/shadow.rs
+++ b/src/shadow.rs
@@ -53,13 +53,10 @@ pub(crate) fn manual_switch_advice(crate_dir: &Path, manifest: &Manifest) -> Opt
}
pub fn shadow(crate_dir: &Path, out: Option<&Path>) -> Result<ShadowOutcome> {
- let crate_dir = crate_dir.canonical().with_context(|| {
- format!(
- "resolving {} (run `rust-anything bridge` first)",
- crate_dir.display()
- )
- })?;
- let manifest = Manifest::load(&crate_dir)?;
+ let manifest = Manifest::load(crate_dir)?;
+ let crate_dir = crate_dir
+ .canonical()
+ .with_context(|| format!("resolving {}", crate_dir.display()))?;
if manifest.language == crate::lang::Lang::Python {
return shadow_python(&crate_dir, &manifest, out);
}
diff --git a/tests/missing_crate.rs b/tests/missing_crate.rs
index 7581bfe8..1e6de1a5 100644
--- a/tests/missing_crate.rs
+++ b/tests/missing_crate.rs
@@ -163,3 +163,47 @@ fn app_spec_map_and_obligations_name_a_missing_folder_once() {
assert_eq!(err.trim(), format!("error: no folder {path}"), "{args:?}");
}
}
+
+#[test]
+fn crate_commands_name_a_missing_or_empty_folder_once() {
+ // A folder that is not there, and one with no manifest: one line each, no operating system
+ // error, and the `bridge` hint that `--json` keys `not_bridged` on.
+ let missing_dir = missing("crate-commands");
+ let empty_dir = missing("crate-commands-empty");
+ let _ = std::fs::remove_dir_all(&empty_dir);
+ std::fs::create_dir_all(&empty_dir).unwrap();
+ for (dir, line) in [
+ (&missing_dir, "no folder"),
+ (&empty_dir, "no rust-anything.json in"),
+ ] {
+ let path = dir.display().to_string();
+ let expected = format!("error: {line} {path} (run `rust-anything bridge` first)");
+ for command in [
+ "diff", "gate", "record", "build", "fuzz", "bench", "report", "shadow", "enable",
+ "disable", "refresh", "package",
+ ] {
+ let out = common::cli().args([command, &path]).output().unwrap();
+ assert_eq!(out.status.code(), Some(2), "{command} {path}");
+ let err = String::from_utf8_lossy(&out.stderr);
+ assert_eq!(err.trim(), expected, "{command} {path}");
+ }
+ }
+ let _ = std::fs::remove_dir_all(&empty_dir);
+}
+
+#[test]
+fn a_manifest_that_cannot_be_read_keeps_its_os_error() {
+ let dir = missing("unreadable-manifest");
+ let _ = std::fs::remove_dir_all(&dir);
+ // A directory where the file should be: it exists, so it is not "missing".
+ std::fs::create_dir_all(dir.join("rust-anything.json")).unwrap();
+ let out = common::cli()
+ .args(["gate", &dir.display().to_string()])
+ .output()
+ .unwrap();
+ let err = String::from_utf8_lossy(&out.stderr);
+ let _ = std::fs::remove_dir_all(&dir);
+ assert_eq!(out.status.code(), Some(2), "{err}");
+ assert!(err.contains("reading"), "{err}");
+ assert!(!err.contains("no rust-anything.json"), "{err}");
+}