From 15380d2f363a28670efdeaf2fe6e8ccaf74393f1 Mon Sep 17 00:00:00 2001 From: maciektr Date: Tue, 1 Aug 2023 11:55:42 +0200 Subject: [PATCH] Parse NDJSON output in tests with stdout_json --- scarb/tests/metadata.rs | 14 +++-- scarb/tests/profiles.rs | 79 +++++++++++++++++++------ utils/scarb-test-support/src/command.rs | 10 +++- 3 files changed, 80 insertions(+), 23 deletions(-) diff --git a/scarb/tests/metadata.rs b/scarb/tests/metadata.rs index 2d1ab787c..4e74d7e41 100644 --- a/scarb/tests/metadata.rs +++ b/scarb/tests/metadata.rs @@ -38,6 +38,7 @@ fn simple() { .build(&t); Scarb::quick_snapbox() + .arg("--json") .arg("metadata") .arg("--format-version") .arg("1") @@ -54,6 +55,7 @@ fn includes_compilation_units() { .build(&t); let output = Scarb::quick_snapbox() + .arg("--json") .arg("metadata") .arg("--format-version") .arg("1") @@ -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") @@ -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") @@ -246,6 +250,7 @@ fn manifest_targets_and_metadata() { .unwrap(); let meta = Scarb::quick_snapbox() + .arg("--json") .arg("metadata") .arg("--format-version") .arg("1") @@ -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") @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); diff --git a/scarb/tests/profiles.rs b/scarb/tests/profiles.rs index e375f8892..75b359837 100644 --- a/scarb/tests/profiles.rs +++ b/scarb/tests/profiles.rs @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -312,6 +340,7 @@ fn compiler_config_set_for_all_profiles() { let metadata = Scarb::quick_snapbox() .args([ + "--json", "--profile", "some-profile", "metadata", @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); @@ -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::(); diff --git a/utils/scarb-test-support/src/command.rs b/utils/scarb-test-support/src/command.rs index 818256c29..a40eddfdd 100644 --- a/utils/scarb-test-support/src/command.rs +++ b/utils/scarb-test-support/src/command.rs @@ -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}; @@ -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::(&line) { + Ok(t) => return t, + Err(_) => continue, + } + } + panic!("Failed to deserialize stdout to JSON"); } }