From 01e138fc0e2d875d3fe1c7449fdf76cd8e6cca21 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 3 Oct 2024 21:09:18 -0500 Subject: [PATCH 1/5] chore: Update snapbox --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b38b2529b61..1c2640bf08b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3255,9 +3255,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "snapbox" -version = "0.6.17" +version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "840b73eb3148bc3cbc10ebe00ec9bc6d96033e658d022c4adcbf3f35596fd64a" +checksum = "7ba434818a8a9b1b106404288d6bd75a94348aae8fc9a518b211b609a36a54bc" dependencies = [ "anstream", "anstyle", diff --git a/Cargo.toml b/Cargo.toml index c62dc28df49..633a5f7fe52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,7 +93,7 @@ sha2 = "0.10.8" shell-escape = "0.1.5" similar = "2.6.0" supports-hyperlinks = "3.0.0" -snapbox = { version = "0.6.17", features = ["diff", "dir", "term-svg", "regex", "json"] } +snapbox = { version = "0.6.18", features = ["diff", "dir", "term-svg", "regex", "json"] } tar = { version = "0.4.42", default-features = false } tempfile = "3.10.1" thiserror = "1.0.63" From f92e54faf99e40e7a1b84f37f2374fc151f0351b Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 16 Aug 2024 16:58:16 -0500 Subject: [PATCH 2/5] test: Migrate remaining metadata with_json tests to snapbox --- tests/testsuite/metadata.rs | 689 +++++++++++++----------------------- 1 file changed, 255 insertions(+), 434 deletions(-) diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index aec0b6cf6aa..a923c30a84c 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -3511,7 +3511,6 @@ fn deps_with_bin_only() { .run(); } -#[expect(deprecated)] #[cargo_test] fn filter_platform() { // Testing the --filter-platform flag. @@ -3550,292 +3549,6 @@ fn filter_platform() { .file("src/lib.rs", "") .build(); - let alt_dep = r#" - { - "name": "alt-dep", - "version": "0.0.1", - "id": "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1", - "license": null, - "license_file": null, - "description": null, - "source": "registry+https://github.com/rust-lang/crates.io-index", - "dependencies": [], - "targets": [ - { - "kind": [ - "lib" - ], - "crate_types": [ - "lib" - ], - "name": "alt_dep", - "src_path": "[..]/alt-dep-0.0.1/src/lib.rs", - "edition": "2015", - "test": true, - "doc": true, - "doctest": true - } - ], - "features": {}, - "manifest_path": "[..]/alt-dep-0.0.1/Cargo.toml", - "metadata": null, - "publish": null, - "authors": [], - "categories": [], - "default_run": null, - "keywords": [], - "readme": null, - "repository": null, - "rust_version": null, - "homepage": null, - "documentation": null, - "edition": "2015", - "links": null - } - "#; - - let cfg_dep = r#" - { - "name": "cfg-dep", - "version": "0.0.1", - "id": "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1", - "license": null, - "license_file": null, - "description": null, - "source": "registry+https://github.com/rust-lang/crates.io-index", - "dependencies": [], - "targets": [ - { - "kind": [ - "lib" - ], - "crate_types": [ - "lib" - ], - "name": "cfg_dep", - "src_path": "[..]/cfg-dep-0.0.1/src/lib.rs", - "edition": "2015", - "test": true, - "doc": true, - "doctest": true - } - ], - "features": {}, - "manifest_path": "[..]/cfg-dep-0.0.1/Cargo.toml", - "metadata": null, - "publish": null, - "authors": [], - "categories": [], - "default_run": null, - "keywords": [], - "readme": null, - "repository": null, - "rust_version": null, - "homepage": null, - "documentation": null, - "edition": "2015", - "links": null - } - "#; - - let host_dep = r#" - { - "name": "host-dep", - "version": "0.0.1", - "id": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1", - "license": null, - "license_file": null, - "description": null, - "source": "registry+https://github.com/rust-lang/crates.io-index", - "dependencies": [], - "targets": [ - { - "kind": [ - "lib" - ], - "crate_types": [ - "lib" - ], - "name": "host_dep", - "src_path": "[..]/host-dep-0.0.1/src/lib.rs", - "edition": "2015", - "test": true, - "doc": true, - "doctest": true - } - ], - "features": {}, - "manifest_path": "[..]/host-dep-0.0.1/Cargo.toml", - "metadata": null, - "publish": null, - "authors": [], - "categories": [], - "default_run": null, - "keywords": [], - "readme": null, - "repository": null, - "rust_version": null, - "homepage": null, - "documentation": null, - "edition": "2015", - "links": null - } - "#; - - let normal_dep = r#" - { - "name": "normal-dep", - "version": "0.0.1", - "id": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1", - "license": null, - "license_file": null, - "description": null, - "source": "registry+https://github.com/rust-lang/crates.io-index", - "dependencies": [], - "targets": [ - { - "kind": [ - "lib" - ], - "crate_types": [ - "lib" - ], - "name": "normal_dep", - "src_path": "[..]/normal-dep-0.0.1/src/lib.rs", - "edition": "2015", - "test": true, - "doc": true, - "doctest": true - } - ], - "features": {}, - "manifest_path": "[..]/normal-dep-0.0.1/Cargo.toml", - "metadata": null, - "publish": null, - "authors": [], - "categories": [], - "default_run": null, - "keywords": [], - "readme": null, - "repository": null, - "rust_version": null, - "homepage": null, - "documentation": null, - "edition": "2015", - "links": null - } - "#; - - // The dependencies are stored in sorted order by target and then by name. - // Since the testsuite may run on different targets, this needs to be - // sorted before it can be compared. - let mut foo_deps = serde_json::json!([ - { - "name": "normal-dep", - "source": "registry+https://github.com/rust-lang/crates.io-index", - "req": "^0.0.1", - "kind": null, - "rename": null, - "optional": false, - "uses_default_features": true, - "features": [], - "target": null, - "registry": null - }, - { - "name": "cfg-dep", - "source": "registry+https://github.com/rust-lang/crates.io-index", - "req": "^0.0.1", - "kind": null, - "rename": null, - "optional": false, - "uses_default_features": true, - "features": [], - "target": "cfg(foobar)", - "registry": null - }, - { - "name": "alt-dep", - "source": "registry+https://github.com/rust-lang/crates.io-index", - "req": "^0.0.1", - "kind": null, - "rename": null, - "optional": false, - "uses_default_features": true, - "features": [], - "target": alt_target, - "registry": null - }, - { - "name": "host-dep", - "source": "registry+https://github.com/rust-lang/crates.io-index", - "req": "^0.0.1", - "kind": null, - "rename": null, - "optional": false, - "uses_default_features": true, - "features": [], - "target": host_target, - "registry": null - } - ]); - foo_deps.as_array_mut().unwrap().sort_by(|a, b| { - // This really should be `rename`, but not needed here. - // Also, sorting on `name` isn't really necessary since this test - // only has one package per target, but leaving it here to be safe. - let a = (a["target"].as_str(), a["name"].as_str()); - let b = (b["target"].as_str(), b["name"].as_str()); - a.cmp(&b) - }); - - let foo = r#" - { - "name": "foo", - "version": "0.1.0", - "id": "path+file:[..]foo#0.1.0", - "license": null, - "license_file": null, - "description": null, - "source": null, - "dependencies": - $FOO_DEPS, - "targets": [ - { - "kind": [ - "lib" - ], - "crate_types": [ - "lib" - ], - "name": "foo", - "src_path": "[..]/foo/src/lib.rs", - "edition": "2015", - "test": true, - "doc": true, - "doctest": true - } - ], - "features": {}, - "manifest_path": "[..]/foo/Cargo.toml", - "metadata": null, - "publish": null, - "authors": [], - "categories": [], - "default_run": null, - "keywords": [], - "readme": null, - "repository": null, - "rust_version": null, - "homepage": null, - "documentation": null, - "edition": "2015", - "links": null - } - "# - .replace("$ALT_TRIPLE", alt_target) - .replace("$HOST_TRIPLE", host_target) - .replace("$FOO_DEPS", &foo_deps.to_string()); - // We're going to be checking that we don't download excessively, // so we need to ensure that downloads will happen. let clear = || { @@ -3860,38 +3573,72 @@ fn filter_platform() { "#]] .unordered(), ) - .with_json( - &r#" + .with_stdout_data( + str![[r#" { "packages": [ - $ALT_DEP, - $CFG_DEP, - $FOO, - $HOST_DEP, - $NORMAL_DEP - ], - "workspace_members": [ - "path+file:[..]foo#0.1.0" - ], - "workspace_default_members": [ - "path+file:[..]foo#0.1.0" + { + "name": "alt-dep", + "dependencies": [], + "...": "{...}" + }, + { + "name": "cfg-dep", + "dependencies": [], + "...": "{...}" + }, + { + "name": "foo", + "dependencies": [ + { + "name": "normal-dep", + "target": null, + "...": "{...}" + }, + { + "name": "cfg-dep", + "target": "cfg(foobar)", + "...": "{...}" + }, + { + "name": "alt-dep", + "target": "wasm32-unknown-unknown", + "...": "{...}" + }, + { + "name": "host-dep", + "target": "[HOST_TARGET]", + "...": "{...}" + } + ], + "...": "{...}" + }, + { + "name": "host-dep", + "dependencies": [], + "...": "{...}" + }, + { + "name": "normal-dep", + "dependencies": [], + "...": "{...}" + } ], "resolve": { "nodes": [ { - "id": "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1" }, { - "id": "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1" }, { - "id": "path+file:[..]foo#0.1.0", "dependencies": [ "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1", "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1", @@ -3900,76 +3647,69 @@ fn filter_platform() { ], "deps": [ { - "name": "alt_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1", "dep_kinds": [ { "kind": null, - "target": "$ALT_TRIPLE" + "target": "wasm32-unknown-unknown" } - ] + ], + "name": "alt_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1" }, { - "name": "cfg_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1", "dep_kinds": [ { "kind": null, "target": "cfg(foobar)" } - ] + ], + "name": "cfg_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1" }, { - "name": "host_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1", "dep_kinds": [ { "kind": null, - "target": "$HOST_TRIPLE" + "target": "[HOST_TARGET]" } - ] + ], + "name": "host_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1" }, { - "name": "normal_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1", "dep_kinds": [ { "kind": null, "target": null } - ] + ], + "name": "normal_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1" } ], - "features": [] + "features": [], + "id": "path+[ROOTURL]/foo#0.1.0" }, { - "id": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1" }, { - "id": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1" } ], - "root": "path+file:[..]foo#0.1.0" + "root": "path+[ROOTURL]/foo#0.1.0" }, - "target_directory": "[..]/foo/target", - "version": 1, - "workspace_root": "[..]/foo", - "metadata": null + "...": "{...}" } -"# - .replace("$ALT_TRIPLE", alt_target) - .replace("$HOST_TRIPLE", host_target) - .replace("$ALT_DEP", alt_dep) - .replace("$CFG_DEP", cfg_dep) - .replace("$HOST_DEP", host_dep) - .replace("$NORMAL_DEP", normal_dep) - .replace("$FOO", &foo), +"#]] + .is_json() + .unordered(), ) .run(); clear(); @@ -3988,73 +3728,99 @@ fn filter_platform() { "#]] .unordered(), ) - .with_json( - &r#" + .with_stdout_data( + str![[r#" { "packages": [ - $ALT_DEP, - $FOO, - $NORMAL_DEP + { + "name": "alt-dep", + "dependencies": [], + "...": "{...}" + }, + { + "name": "foo", + "dependencies": [ + { + "name": "normal-dep", + "target": null, + "...": "{...}" + }, + { + "name": "cfg-dep", + "target": "cfg(foobar)", + "...": "{...}" + }, + { + "name": "alt-dep", + "target": "wasm32-unknown-unknown", + "...": "{...}" + }, + { + "name": "host-dep", + "target": "[HOST_TARGET]", + "...": "{...}" + } + ], + "...": "{...}" + }, + { + "name": "normal-dep", + "dependencies": [], + "...": "{...}" + } ], - "workspace_members": "{...}", - "workspace_default_members": "{...}", "resolve": { "nodes": [ { - "id": "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1" }, { - "id": "path+file:[..]foo#0.1.0", "dependencies": [ "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1", "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1" ], "deps": [ { - "name": "alt_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1", "dep_kinds": [ { "kind": null, - "target": "$ALT_TRIPLE" + "target": "wasm32-unknown-unknown" } - ] + ], + "name": "alt_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#alt-dep@0.0.1" }, { - "name": "normal_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1", "dep_kinds": [ { "kind": null, "target": null } - ] + ], + "name": "normal_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1" } ], - "features": [] + "features": [], + "id": "path+[ROOTURL]/foo#0.1.0" }, { - "id": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1" } ], - "root": "path+file:[..]foo#0.1.0" + "root": "path+[ROOTURL]/foo#0.1.0" }, - "target_directory": "[..]foo/target", - "version": 1, - "workspace_root": "[..]foo", - "metadata": null + "...": "{...}" } -"# - .replace("$ALT_TRIPLE", alt_target) - .replace("$ALT_DEP", alt_dep) - .replace("$NORMAL_DEP", normal_dep) - .replace("$FOO", &foo), +"#]] + .is_json() + .unordered(), ) .run(); clear(); @@ -4072,73 +3838,99 @@ fn filter_platform() { "#]] .unordered(), ) - .with_json( - &r#" + .with_stdout_data( + str![[r#" { "packages": [ - $FOO, - $HOST_DEP, - $NORMAL_DEP + { + "name": "foo", + "dependencies": [ + { + "name": "normal-dep", + "target": null, + "...": "{...}" + }, + { + "name": "cfg-dep", + "target": "cfg(foobar)", + "...": "{...}" + }, + { + "name": "alt-dep", + "target": "wasm32-unknown-unknown", + "...": "{...}" + }, + { + "name": "host-dep", + "target": "[HOST_TARGET]", + "...": "{...}" + } + ], + "...": "{...}" + }, + { + "name": "host-dep", + "dependencies": [], + "...": "{...}" + }, + { + "name": "normal-dep", + "dependencies": [], + "...": "{...}" + } ], - "workspace_members": "{...}", - "workspace_default_members": "{...}", "resolve": { "nodes": [ { - "id": "path+file:[..]foo#0.1.0", "dependencies": [ "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1", "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1" ], "deps": [ { - "name": "host_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1", "dep_kinds": [ { "kind": null, - "target": "$HOST_TRIPLE" + "target": "[HOST_TARGET]" } - ] + ], + "name": "host_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1" }, { - "name": "normal_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1", "dep_kinds": [ { "kind": null, "target": null } - ] + ], + "name": "normal_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1" } ], - "features": [] + "features": [], + "id": "path+[ROOTURL]/foo#0.1.0" }, { - "id": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1" }, { - "id": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1" } ], - "root": "path+file:[..]foo#0.1.0" + "root": "path+[ROOTURL]/foo#0.1.0" }, - "target_directory": "[..]foo/target", - "version": 1, - "workspace_root": "[..]foo", - "metadata": null + "...": "{...}" } -"# - .replace("$HOST_TRIPLE", host_target) - .replace("$HOST_DEP", host_dep) - .replace("$NORMAL_DEP", normal_dep) - .replace("$FOO", &foo), +"#]] + .is_json() + .unordered(), ) .run(); clear(); @@ -4158,27 +3950,61 @@ fn filter_platform() { "#]] .unordered(), ) - .with_json( - &r#" + .with_stdout_data( + str![[r#" { "packages": [ - $CFG_DEP, - $FOO, - $HOST_DEP, - $NORMAL_DEP + { + "name": "cfg-dep", + "dependencies": [], + "...": "{...}" + }, + { + "name": "foo", + "dependencies": [ + { + "name": "normal-dep", + "target": null, + "...": "{...}" + }, + { + "name": "cfg-dep", + "target": "cfg(foobar)", + "...": "{...}" + }, + { + "name": "alt-dep", + "target": "wasm32-unknown-unknown", + "...": "{...}" + }, + { + "name": "host-dep", + "target": "[HOST_TARGET]", + "...": "{...}" + } + ], + "...": "{...}" + }, + { + "name": "host-dep", + "dependencies": [], + "...": "{...}" + }, + { + "name": "normal-dep", + "dependencies": [], + "...": "{...}" + } ], - "workspace_members": "{...}", - "workspace_default_members": "{...}", "resolve": { "nodes": [ { - "id": "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1" }, { - "id": "path+file:[..]/foo#0.1.0", "dependencies": [ "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1", "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1", @@ -4186,64 +4012,59 @@ fn filter_platform() { ], "deps": [ { - "name": "cfg_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1", "dep_kinds": [ { "kind": null, "target": "cfg(foobar)" } - ] + ], + "name": "cfg_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#cfg-dep@0.0.1" }, { - "name": "host_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1", "dep_kinds": [ { "kind": null, - "target": "$HOST_TRIPLE" + "target": "[HOST_TARGET]" } - ] + ], + "name": "host_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1" }, { - "name": "normal_dep", - "pkg": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1", "dep_kinds": [ { "kind": null, "target": null } - ] + ], + "name": "normal_dep", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1" } ], - "features": [] + "features": [], + "id": "path+[ROOTURL]/foo#0.1.0" }, { - "id": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#host-dep@0.0.1" }, { - "id": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1", "dependencies": [], "deps": [], - "features": [] + "features": [], + "id": "registry+https://github.com/rust-lang/crates.io-index#normal-dep@0.0.1" } ], - "root": "path+file:[..]/foo#0.1.0" + "root": "path+[ROOTURL]/foo#0.1.0" }, - "target_directory": "[..]/foo/target", - "version": 1, - "workspace_root": "[..]/foo", - "metadata": null + "...": "{...}" } -"# - .replace("$HOST_TRIPLE", host_target) - .replace("$CFG_DEP", cfg_dep) - .replace("$HOST_DEP", host_dep) - .replace("$NORMAL_DEP", normal_dep) - .replace("$FOO", &foo), +"#]] + .is_json() + .unordered(), ) .run(); } From c18765a1523748e9e819cd4c280b26d4e47873a4 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 20 Sep 2024 19:36:00 -0500 Subject: [PATCH 3/5] test: Migrate remaining build with_json tests to snapbox --- tests/testsuite/build.rs | 300 +++++++++++++++------------------------ 1 file changed, 118 insertions(+), 182 deletions(-) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 3b81c6e226d..bb13046e50f 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -4131,7 +4131,6 @@ fn panic_abort_compiles_with_panic_abort() { .run(); } -#[expect(deprecated)] #[cargo_test] fn compiler_json_error_format() { let p = project() @@ -4160,148 +4159,98 @@ fn compiler_json_error_format() { let output = |fresh| { r#" - { - "reason":"compiler-artifact", - "package_id":"path+file:///[..]/foo#0.5.0", - "manifest_path": "[..]", - "target":{ - "kind":["custom-build"], - "crate_types":["bin"], - "doc": false, - "doctest": false, - "edition": "2015", - "name":"build-script-build", - "src_path":"[..]build.rs", - "test": false - }, - "profile": { - "debug_assertions": true, - "debuginfo": 0, - "opt_level": "0", - "overflow_checks": true, - "test": false - }, - "executable": null, - "features": [], - "filenames": "{...}", - "fresh": $FRESH - } - - { - "reason":"compiler-message", - "package_id":"path+file:///[..]/bar#0.5.0", - "manifest_path": "[..]", - "target":{ - "kind":["lib"], - "crate_types":["lib"], - "doc": true, - "doctest": true, - "edition": "2015", - "name":"bar", - "src_path":"[..]lib.rs", - "test": true - }, - "message":"{...}" - } - - { - "reason":"compiler-artifact", - "profile": { - "debug_assertions": true, - "debuginfo": 2, - "opt_level": "0", - "overflow_checks": true, - "test": false - }, - "executable": null, - "features": [], - "package_id":"path+file:///[..]/bar#0.5.0", - "manifest_path": "[..]", - "target":{ - "kind":["lib"], - "crate_types":["lib"], - "doc": true, - "doctest": true, - "edition": "2015", - "name":"bar", - "src_path":"[..]lib.rs", - "test": true - }, - "filenames":[ - "[..].rlib", - "[..].rmeta" - ], - "fresh": $FRESH - } - - { - "reason":"build-script-executed", - "package_id":"path+file:///[..]/foo#0.5.0", - "linked_libs":[], - "linked_paths":[], - "env":[], - "cfgs":["xyz"], - "out_dir": "[..]target/debug/build/foo-[..]/out" - } - - { - "reason":"compiler-message", - "package_id":"path+file:///[..]/foo#0.5.0", - "manifest_path": "[..]", - "target":{ - "kind":["bin"], - "crate_types":["bin"], - "doc": true, - "doctest": false, - "edition": "2015", - "name":"foo", - "src_path":"[..]main.rs", - "test": true - }, - "message":"{...}" - } - - { - "reason":"compiler-artifact", - "package_id":"path+file:///[..]/foo#0.5.0", - "manifest_path": "[..]", - "target":{ - "kind":["bin"], - "crate_types":["bin"], - "doc": true, - "doctest": false, - "edition": "2015", - "name":"foo", - "src_path":"[..]main.rs", - "test": true - }, - "profile": { - "debug_assertions": true, - "debuginfo": 2, - "opt_level": "0", - "overflow_checks": true, - "test": false - }, - "executable": "[..]/foo/target/debug/foo[EXE]", - "features": [], - "filenames": "{...}", - "fresh": $FRESH - } - - {"reason": "build-finished", "success": true} - "# +[ + { + "executable": null, + "features": [], + "fresh": $FRESH, + "manifest_path": "[ROOT]/foo/Cargo.toml", + "package_id": "path+[ROOTURL]/foo#0.5.0", + "reason": "compiler-artifact", + "target": { + "kind": ["custom-build"], + "...": "{...}" + }, + "...": "{...}" + }, + { + "manifest_path": "[ROOT]/foo/bar/Cargo.toml", + "package_id": "path+[ROOTURL]/foo/bar#0.5.0", + "reason": "compiler-message", + "target": { + "kind": ["lib"], + "...": "{...}" + }, + "...": "{...}" + }, + { + "executable": null, + "features": [], + "fresh": $FRESH, + "manifest_path": "[ROOT]/foo/bar/Cargo.toml", + "package_id": "path+[ROOTURL]/foo/bar#0.5.0", + "reason": "compiler-artifact", + "target": { + "kind": ["lib"], + "...": "{...}" + }, + "...": "{...}" + }, + { + "cfgs": [ + "xyz" + ], + "env": [], + "linked_libs": [], + "linked_paths": [], + "out_dir": "[ROOT]/foo/target/debug/build/foo-[HASH]/out", + "package_id": "path+[ROOTURL]/foo#0.5.0", + "reason": "build-script-executed" + }, + { + "manifest_path": "[ROOT]/foo/Cargo.toml", + "package_id": "path+[ROOTURL]/foo#0.5.0", + "reason": "compiler-message", + "target": { + "kind": ["bin"], + "...": "{...}" + }, + "...": "{...}" + }, + { + "features": [], + "fresh": $FRESH, + "manifest_path": "[ROOT]/foo/Cargo.toml", + "package_id": "path+[ROOTURL]/foo#0.5.0", + "reason": "compiler-artifact", + "target": { + "kind": ["bin"], + "...": "{...}" + }, + "...": "{...}" + }, + { + "reason": "build-finished", + "success": true + }, + "{...}" +] +"# .replace("$FRESH", fresh) + .is_json() + .against_jsonlines() + .unordered() }; // Use `jobs=1` to ensure that the order of messages is consistent. p.cargo("build -v --message-format=json --jobs=1") - .with_json_contains_unordered(&output("false")) + .with_stdout_data(output("false")) .run(); // With fresh build, we should repeat the artifacts, // and replay the cached compiler warnings. p.cargo("build -v --message-format=json --jobs=1") - .with_json_contains_unordered(&output("true")) + .with_stdout_data(output("true")) .run(); } @@ -4321,7 +4270,6 @@ fn wrong_message_format_option() { .run(); } -#[expect(deprecated)] #[cargo_test] fn message_format_json_forward_stderr() { let p = project() @@ -4330,54 +4278,42 @@ fn message_format_json_forward_stderr() { .build(); p.cargo("rustc --release --bin foo --message-format JSON") - .with_json_contains_unordered( - r#" - { - "reason":"compiler-message", - "package_id":"path+file:///[..]/foo#0.5.0", - "manifest_path": "[..]", - "target":{ - "kind":["bin"], - "crate_types":["bin"], - "doc": true, - "doctest": false, - "edition": "2015", - "name":"foo", - "src_path":"[..]", - "test": true - }, - "message":"{...}" - } - - { - "reason":"compiler-artifact", - "package_id":"path+file:///[..]/foo#0.5.0", - "manifest_path": "[..]", - "target":{ - "kind":["bin"], - "crate_types":["bin"], - "doc": true, - "doctest": false, - "edition": "2015", - "name":"foo", - "src_path":"[..]", - "test": true - }, - "profile":{ - "debug_assertions":false, - "debuginfo":0, - "opt_level":"3", - "overflow_checks": false, - "test":false - }, - "executable": "{...}", - "features":[], - "filenames": "{...}", - "fresh": false - } - - {"reason": "build-finished", "success": true} - "#, + .with_stdout_data( + str![[r#" +[ + { + "manifest_path": "[ROOT]/foo/Cargo.toml", + "message": "{...}", + "package_id": "path+[ROOTURL]/foo#0.5.0", + "reason": "compiler-message", + "target": { + "kind": ["bin"], + "...": "{...}" + }, + "...": "{...}" + }, + { + "features": [], + "fresh": false, + "manifest_path": "[ROOT]/foo/Cargo.toml", + "package_id": "path+[ROOTURL]/foo#0.5.0", + "reason": "compiler-artifact", + "target": { + "kind": ["bin"], + "...": "{...}" + }, + "...": "{...}" + }, + { + "reason": "build-finished", + "success": true + }, + "{...}" +] +"#]] + .is_json() + .against_jsonlines() + .unordered(), ) .run(); } From 01a47f3c4dceae7cfbb6f2bfcf737119bca3c9d8 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 21 Sep 2024 20:06:13 -0500 Subject: [PATCH 4/5] fix(test): Remove unused, deprecated with_json --- crates/cargo-test-support/src/compare.rs | 80 +----------------------- crates/cargo-test-support/src/lib.rs | 64 ------------------- 2 files changed, 1 insertion(+), 143 deletions(-) diff --git a/crates/cargo-test-support/src/compare.rs b/crates/cargo-test-support/src/compare.rs index 5a26aa1f18c..cb40eebc5c1 100644 --- a/crates/cargo-test-support/src/compare.rs +++ b/crates/cargo-test-support/src/compare.rs @@ -44,7 +44,7 @@ use crate::cross_compile::try_alternate; use crate::paths; use crate::{diff, rustc_host}; -use anyhow::{bail, Context, Result}; +use anyhow::{bail, Result}; use serde_json::Value; use std::fmt; use std::path::Path; @@ -654,84 +654,6 @@ pub(crate) fn match_with_without( } } -/// Checks that the given string of JSON objects match the given set of -/// expected JSON objects. -/// -/// See [`crate::Execs::with_json`] for more details. -pub(crate) fn match_json(expected: &str, actual: &str, cwd: Option<&Path>) -> Result<()> { - let (exp_objs, act_objs) = collect_json_objects(expected, actual)?; - if exp_objs.len() != act_objs.len() { - bail!( - "expected {} json lines, got {}, stdout:\n{}", - exp_objs.len(), - act_objs.len(), - actual - ); - } - for (exp_obj, act_obj) in exp_objs.iter().zip(act_objs) { - find_json_mismatch(exp_obj, &act_obj, cwd)?; - } - Ok(()) -} - -/// Checks that the given string of JSON objects match the given set of -/// expected JSON objects, ignoring their order. -/// -/// See [`crate::Execs::with_json_contains_unordered`] for more details and -/// cautions when using. -pub(crate) fn match_json_contains_unordered( - expected: &str, - actual: &str, - cwd: Option<&Path>, -) -> Result<()> { - let (exp_objs, mut act_objs) = collect_json_objects(expected, actual)?; - for exp_obj in exp_objs { - match act_objs - .iter() - .position(|act_obj| find_json_mismatch(&exp_obj, act_obj, cwd).is_ok()) - { - Some(index) => act_objs.remove(index), - None => { - bail!( - "Did not find expected JSON:\n\ - {}\n\ - Remaining available output:\n\ - {}\n", - serde_json::to_string_pretty(&exp_obj).unwrap(), - itertools::join( - act_objs.iter().map(|o| serde_json::to_string(o).unwrap()), - "\n" - ) - ); - } - }; - } - Ok(()) -} - -fn collect_json_objects( - expected: &str, - actual: &str, -) -> Result<(Vec, Vec)> { - let expected_objs: Vec<_> = expected - .split("\n\n") - .map(|expect| { - expect - .parse() - .with_context(|| format!("failed to parse expected JSON object:\n{}", expect)) - }) - .collect::>()?; - let actual_objs: Vec<_> = actual - .lines() - .filter(|line| line.starts_with('{')) - .map(|line| { - line.parse() - .with_context(|| format!("failed to parse JSON object:\n{}", line)) - }) - .collect::>()?; - Ok((expected_objs, actual_objs)) -} - /// Compares JSON object for approximate equality. /// You can use `[..]` wildcard in strings (useful for OS-dependent things such /// as paths). You can use a `"{...}"` string literal as a wildcard for diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index 2f2d43b6a1f..a4cabafd6db 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -655,8 +655,6 @@ pub struct Execs { expect_stdout_unordered: Vec, expect_stderr_unordered: Vec, expect_stderr_with_without: Vec<(Vec, Vec)>, - expect_json: Option, - expect_json_contains_unordered: Option, stream_output: bool, assert: snapbox::Assert, } @@ -824,56 +822,6 @@ impl Execs { self.expect_stderr_with_without.push((with, without)); self } - - /// Verifies the JSON output matches the given JSON. - /// - /// This is typically used when testing cargo commands that emit JSON. - /// Each separate JSON object should be separated by a blank line. - /// Example: - /// - /// ```rust,ignore - /// assert_that( - /// p.cargo("metadata"), - /// execs().with_json(r#" - /// {"example": "abc"} - /// - /// {"example": "def"} - /// "#) - /// ); - /// ``` - /// - /// - Objects should match in the order given. - /// - The order of arrays is ignored. - /// - Strings support patterns described in [`compare`]. - /// - Use `"{...}"` to match any object. - #[deprecated( - note = "replaced with `Execs::with_stdout_data(expected.is_json().against_jsonlines())`" - )] - pub fn with_json(&mut self, expected: &str) -> &mut Self { - self.expect_json = Some(expected.to_string()); - self - } - - /// Verifies JSON output contains the given objects (in any order) somewhere - /// in its output. - /// - /// CAUTION: Be very careful when using this. Make sure every object is - /// unique (not a subset of one another). Also avoid using objects that - /// could possibly match multiple output lines unless you're very sure of - /// what you are doing. - /// - /// See `with_json` for more detail. - #[deprecated] - pub fn with_json_contains_unordered(&mut self, expected: &str) -> &mut Self { - match &mut self.expect_json_contains_unordered { - None => self.expect_json_contains_unordered = Some(expected.to_string()), - Some(e) => { - e.push_str("\n\n"); - e.push_str(expected); - } - } - self - } } /// # Configure the process @@ -1050,8 +998,6 @@ impl Execs { && self.expect_stdout_unordered.is_empty() && self.expect_stderr_unordered.is_empty() && self.expect_stderr_with_without.is_empty() - && self.expect_json.is_none() - && self.expect_json_contains_unordered.is_none() { panic!( "`with_status()` is used, but no output is checked.\n\ @@ -1175,14 +1121,6 @@ impl Execs { for (with, without) in self.expect_stderr_with_without.iter() { compare::match_with_without(stderr, with, without, cwd)?; } - - if let Some(ref expect_json) = self.expect_json { - compare::match_json(expect_json, stdout, cwd)?; - } - - if let Some(ref expected) = self.expect_json_contains_unordered { - compare::match_json_contains_unordered(expected, stdout, cwd)?; - } Ok(()) } } @@ -1212,8 +1150,6 @@ pub fn execs() -> Execs { expect_stdout_unordered: Vec::new(), expect_stderr_unordered: Vec::new(), expect_stderr_with_without: Vec::new(), - expect_json: None, - expect_json_contains_unordered: None, stream_output: false, assert: compare::assert_e2e(), } From c3f19a8ec1641614aa9edbd05a88e957d43df371 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 21 Sep 2024 20:12:36 -0500 Subject: [PATCH 5/5] refactor(test): Migrate validate_upload to snapbox for json comparisons --- crates/cargo-test-support/src/compare.rs | 76 ------------------------ crates/cargo-test-support/src/publish.rs | 9 +-- 2 files changed, 3 insertions(+), 82 deletions(-) diff --git a/crates/cargo-test-support/src/compare.rs b/crates/cargo-test-support/src/compare.rs index cb40eebc5c1..5b5d4b6ce85 100644 --- a/crates/cargo-test-support/src/compare.rs +++ b/crates/cargo-test-support/src/compare.rs @@ -45,7 +45,6 @@ use crate::cross_compile::try_alternate; use crate::paths; use crate::{diff, rustc_host}; use anyhow::{bail, Result}; -use serde_json::Value; use std::fmt; use std::path::Path; use std::str; @@ -654,81 +653,6 @@ pub(crate) fn match_with_without( } } -/// Compares JSON object for approximate equality. -/// You can use `[..]` wildcard in strings (useful for OS-dependent things such -/// as paths). You can use a `"{...}"` string literal as a wildcard for -/// arbitrary nested JSON (useful for parts of object emitted by other programs -/// (e.g., rustc) rather than Cargo itself). -pub(crate) fn find_json_mismatch( - expected: &Value, - actual: &Value, - cwd: Option<&Path>, -) -> Result<()> { - match find_json_mismatch_r(expected, actual, cwd) { - Some((expected_part, actual_part)) => bail!( - "JSON mismatch\nExpected:\n{}\nWas:\n{}\nExpected part:\n{}\nActual part:\n{}\n", - serde_json::to_string_pretty(expected).unwrap(), - serde_json::to_string_pretty(&actual).unwrap(), - serde_json::to_string_pretty(expected_part).unwrap(), - serde_json::to_string_pretty(actual_part).unwrap(), - ), - None => Ok(()), - } -} - -fn find_json_mismatch_r<'a>( - expected: &'a Value, - actual: &'a Value, - cwd: Option<&Path>, -) -> Option<(&'a Value, &'a Value)> { - use serde_json::Value::*; - match (expected, actual) { - (&Number(ref l), &Number(ref r)) if l == r => None, - (&Bool(l), &Bool(r)) if l == r => None, - (&String(ref l), _) if l == "{...}" => None, - (&String(ref l), &String(ref r)) => { - if match_exact(l, r, "", "", cwd).is_err() { - Some((expected, actual)) - } else { - None - } - } - (&Array(ref l), &Array(ref r)) => { - if l.len() != r.len() { - return Some((expected, actual)); - } - - l.iter() - .zip(r.iter()) - .filter_map(|(l, r)| find_json_mismatch_r(l, r, cwd)) - .next() - } - (&Object(ref l), &Object(ref r)) => { - let mut expected_entries = l.iter(); - let mut actual_entries = r.iter(); - - loop { - match (expected_entries.next(), actual_entries.next()) { - (None, None) => return None, - (Some((expected_key, expected_value)), Some((actual_key, actual_value))) - if expected_key == actual_key => - { - if let mismatch @ Some(_) = - find_json_mismatch_r(expected_value, actual_value, cwd) - { - return mismatch; - } - } - _ => return Some((expected, actual)), - } - } - } - (&Null, &Null) => None, - // Magic string literal `"{...}"` acts as wildcard for any sub-JSON. - _ => Some((expected, actual)), - } -} - /// A single line string that supports `[..]` wildcard matching. pub(crate) struct WildStr<'a> { has_meta: bool, diff --git a/crates/cargo-test-support/src/publish.rs b/crates/cargo-test-support/src/publish.rs index a673c466b94..b47c73b3808 100644 --- a/crates/cargo-test-support/src/publish.rs +++ b/crates/cargo-test-support/src/publish.rs @@ -57,9 +57,10 @@ //! ); //! ``` -use crate::compare::{assert_match_exact, find_json_mismatch}; +use crate::compare::assert_match_exact; use crate::registry::{self, alt_api_path, FeatureMap}; use flate2::read::GzDecoder; +use snapbox::prelude::*; use std::collections::{HashMap, HashSet}; use std::fs; use std::fs::File; @@ -133,12 +134,8 @@ fn _validate_upload( let json_sz = read_le_u32(&mut f).expect("read json length"); let mut json_bytes = vec![0; json_sz as usize]; f.read_exact(&mut json_bytes).expect("read JSON data"); - let actual_json = serde_json::from_slice(&json_bytes).expect("uploaded JSON should be valid"); - let expected_json = serde_json::from_str(expected_json).expect("expected JSON does not parse"); - if let Err(e) = find_json_mismatch(&expected_json, &actual_json, None) { - panic!("{}", e); - } + snapbox::assert_data_eq!(json_bytes, expected_json.is_json()); // 32-bit little-endian integer of length of crate file. let crate_sz = read_le_u32(&mut f).expect("read crate length");