Skip to content

Commit

Permalink
Parse NDJSON output in tests with stdout_json
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr committed Aug 1, 2023
1 parent f19b467 commit 15380d2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 23 deletions.
14 changes: 10 additions & 4 deletions scarb/tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn simple() {
.build(&t);

Scarb::quick_snapbox()
.arg("--json")
.arg("metadata")
.arg("--format-version")
.arg("1")
Expand All @@ -54,6 +55,7 @@ fn includes_compilation_units() {
.build(&t);

let output = Scarb::quick_snapbox()
.arg("--json")
.arg("metadata")
.arg("--format-version")
.arg("1")
Expand Down Expand Up @@ -155,6 +157,7 @@ fn local_dependencies() {
let t = assert_fs::TempDir::new().unwrap();
create_local_dependencies_setup(&t);
let meta = Scarb::quick_snapbox()
.arg("--json")
.arg("metadata")
.arg("--format-version")
.arg("1")
Expand All @@ -180,6 +183,7 @@ fn no_dep() {
let t = assert_fs::TempDir::new().unwrap();
create_local_dependencies_setup(&t);
let meta = Scarb::quick_snapbox()
.arg("--json")
.arg("metadata")
.arg("--format-version")
.arg("1")
Expand Down Expand Up @@ -246,6 +250,7 @@ fn manifest_targets_and_metadata() {
.unwrap();

let meta = Scarb::quick_snapbox()
.arg("--json")
.arg("metadata")
.arg("--format-version")
.arg("1")
Expand Down Expand Up @@ -306,6 +311,7 @@ fn tool_metadata_is_packaged_contained() {
)
.unwrap();
let meta = Scarb::quick_snapbox()
.arg("--json")
.arg("metadata")
.arg("--format-version")
.arg("1")
Expand Down Expand Up @@ -367,7 +373,7 @@ fn workspace_simple() {
.build(&t);

let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version=1"])
.args(["--json", "metadata", "--format-version=1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand Down Expand Up @@ -405,7 +411,7 @@ fn workspace_with_root() {
.build(&t);

let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version=1"])
.args(["--json", "metadata", "--format-version=1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand Down Expand Up @@ -447,7 +453,7 @@ fn workspace_as_dep() {
.build(&first_t);

let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version=1"])
.args(["--json", "metadata", "--format-version=1"])
.current_dir(&first_t)
.stdout_json::<Metadata>();

Expand Down Expand Up @@ -481,7 +487,7 @@ fn workspace_as_dep() {
.build(&second_t);

let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version=1"])
.args(["--json", "metadata", "--format-version=1"])
.current_dir(&second_t)
.stdout_json::<Metadata>();

Expand Down
79 changes: 61 additions & 18 deletions scarb/tests/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn defaults_to_dev() {
ProjectBuilder::start().name("hello").build(&t);

let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version", "1"])
.args(["--json", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand All @@ -60,7 +60,7 @@ fn can_choose_release() {
ProjectBuilder::start().name("hello").build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--release", "metadata", "--format-version", "1"])
.args(["--json", "--release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand All @@ -77,7 +77,7 @@ fn can_choose_dev() {
ProjectBuilder::start().name("hello").build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--dev", "metadata", "--format-version", "1"])
.args(["--json", "--dev", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand Down Expand Up @@ -113,7 +113,14 @@ fn can_choose_release_by_name() {
ProjectBuilder::start().name("hello").build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--profile", "release", "metadata", "--format-version", "1"])
.args([
"--json",
"--profile",
"release",
"metadata",
"--format-version",
"1",
])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand Down Expand Up @@ -149,7 +156,14 @@ fn can_choose_dev_by_name() {
ProjectBuilder::start().name("hello").build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--profile", "dev", "metadata", "--format-version", "1"])
.args([
"--json",
"--profile",
"dev",
"metadata",
"--format-version",
"1",
])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand All @@ -166,7 +180,7 @@ fn can_choose_dev_by_short_name() {
ProjectBuilder::start().name("hello").build(&t);

let metadata = Scarb::quick_snapbox()
.args(["-P", "dev", "metadata", "--format-version", "1"])
.args(["--json", "-P", "dev", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand All @@ -188,7 +202,14 @@ fn can_choose_custom_profile() {
.build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--profile", "custom", "metadata", "--format-version", "1"])
.args([
"--json",
"--profile",
"custom",
"metadata",
"--format-version",
"1",
])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand Down Expand Up @@ -225,7 +246,7 @@ fn sierra_replace_ids_defaults_true_in_dev() {
ProjectBuilder::start().name("hello").build(&t);

let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version", "1"])
.args(["--json", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand All @@ -247,7 +268,14 @@ fn sierra_replace_ids_default_false_in_release() {
ProjectBuilder::start().name("hello").build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--profile", "release", "metadata", "--format-version", "1"])
.args([
"--json",
"--profile",
"release",
"metadata",
"--format-version",
"1",
])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand Down Expand Up @@ -279,7 +307,7 @@ fn compiler_config_set_for_all_profiles() {
.build(&t);

let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version", "1"])
.args(["--json", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand All @@ -295,7 +323,7 @@ fn compiler_config_set_for_all_profiles() {
}

let metadata = Scarb::quick_snapbox()
.args(["--release", "metadata", "--format-version", "1"])
.args(["--json", "--release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand All @@ -312,6 +340,7 @@ fn compiler_config_set_for_all_profiles() {

let metadata = Scarb::quick_snapbox()
.args([
"--json",
"--profile",
"some-profile",
"metadata",
Expand Down Expand Up @@ -345,7 +374,7 @@ fn can_set_replace_ids_in_profile() {
.build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--release", "metadata", "--format-version", "1"])
.args(["--json", "--release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand Down Expand Up @@ -376,7 +405,7 @@ fn profile_precedes_compiler_config() {
.build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--release", "metadata", "--format-version", "1"])
.args(["--json", "--release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand All @@ -392,7 +421,7 @@ fn profile_precedes_compiler_config() {
}

let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version", "1"])
.args(["--json", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand All @@ -419,7 +448,14 @@ fn custom_profiles_inherit_from_dev_by_default() {
.build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--profile", "custom", "metadata", "--format-version", "1"])
.args([
"--json",
"--profile",
"custom",
"metadata",
"--format-version",
"1",
])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand Down Expand Up @@ -447,7 +483,14 @@ fn custom_profiles_can_inherit_by_name() {
.build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--profile", "custom", "metadata", "--format-version", "1"])
.args([
"--json",
"--profile",
"custom",
"metadata",
"--format-version",
"1",
])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand Down Expand Up @@ -503,7 +546,7 @@ fn profile_overrides_tool() {
"#})
.build(&t);
let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version", "1"])
.args(["--json", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand All @@ -526,7 +569,7 @@ fn profile_overrides_tool() {
);

let metadata = Scarb::quick_snapbox()
.args(["--release", "metadata", "--format-version", "1"])
.args(["--json", "--release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

Expand Down
10 changes: 9 additions & 1 deletion utils/scarb-test-support/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ffi::OsString;
use std::io::BufRead;
use std::path::{Path, PathBuf};
use std::process::Command as StdCommand;
use std::{fs, iter};
Expand Down Expand Up @@ -128,6 +129,13 @@ impl CommandExt for SnapboxCommand {
"Command failed: {}",
String::from_utf8_lossy(&output.stderr)
);
serde_json::de::from_slice(&output.stdout).expect("Failed to deserialize stdout to JSON")
for line in BufRead::split(output.stdout.as_slice(), b'\n') {
let line = line.expect("Failed to read line from stdout");
match serde_json::de::from_slice::<T>(&line) {
Ok(t) => return t,
Err(_) => continue,
}
}
panic!("Failed to deserialize stdout to JSON");
}
}

0 comments on commit 15380d2

Please sign in to comment.