Skip to content

Commit

Permalink
Extract CommandExt to support module
Browse files Browse the repository at this point in the history
commit-id:ad827ee9
  • Loading branch information
mkaput committed Jul 12, 2023
1 parent f4b62ad commit 3c092c6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 76 deletions.
26 changes: 7 additions & 19 deletions scarb/tests/e2e/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@ use std::collections::BTreeMap;

use assert_fs::prelude::*;
use serde_json::json;
use snapbox::cmd::Command;

use scarb_metadata::{Cfg, ManifestMetadataBuilder, Metadata, PackageMetadata};

use crate::support::command::Scarb;
use crate::support::command::{CommandExt, Scarb};
use crate::support::project_builder::ProjectBuilder;

trait CommandExt {
fn stdout_json(self) -> Metadata;
}

impl CommandExt for Command {
fn stdout_json(self) -> Metadata {
let output = self.output().expect("Failed to spawn command");
serde_json::de::from_slice(&output.stdout).expect("Failed to deserialize stdout to JSON")
}
}

fn packages_by_name(meta: Metadata) -> BTreeMap<String, PackageMetadata> {
meta.packages
.into_iter()
Expand Down Expand Up @@ -54,7 +42,7 @@ fn simple() {
.arg("--format-version")
.arg("1")
.current_dir(&t)
.stdout_json();
.stdout_json::<Metadata>();
}

#[test]
Expand All @@ -70,7 +58,7 @@ fn includes_compilation_units() {
.arg("--format-version")
.arg("1")
.current_dir(&t)
.stdout_json();
.stdout_json::<Metadata>();

assert!(!output.compilation_units.is_empty());
let unit = &output.compilation_units[0];
Expand Down Expand Up @@ -171,7 +159,7 @@ fn local_dependencies() {
.arg("--format-version")
.arg("1")
.current_dir(&t)
.stdout_json();
.stdout_json::<Metadata>();
assert_eq!(
packages_and_deps(meta),
BTreeMap::from_iter([
Expand All @@ -197,7 +185,7 @@ fn no_dep() {
.arg("1")
.arg("--no-deps")
.current_dir(&t)
.stdout_json();
.stdout_json::<Metadata>();

assert_eq!(
packages_and_deps(meta),
Expand Down Expand Up @@ -262,7 +250,7 @@ fn manifest_targets_and_metadata() {
.arg("--format-version")
.arg("1")
.current_dir(&t)
.stdout_json();
.stdout_json::<Metadata>();

assert_eq!(
meta.packages
Expand Down Expand Up @@ -322,7 +310,7 @@ fn tool_metadata_is_packaged_contained() {
.arg("--format-version")
.arg("1")
.current_dir(&t)
.stdout_json();
.stdout_json::<Metadata>();
assert_eq!(
packages_by_name(meta)
.into_iter()
Expand Down
58 changes: 21 additions & 37 deletions scarb/tests/e2e/profiles.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
use assert_fs::prelude::*;
use assert_fs::TempDir;
use indoc::indoc;

use scarb_metadata::Metadata;
use snapbox::cmd::Command;

use crate::support::command::Scarb;
use crate::support::command::{CommandExt, Scarb};
use crate::support::fsx::ChildPathEx;
use crate::support::project_builder::ProjectBuilder;

trait CommandExt {
fn stdout_metadata(self) -> Metadata;
}

impl CommandExt for Command {
fn stdout_metadata(self) -> Metadata {
let output = self.output().expect("Failed to spawn command");
assert!(
output.status.success(),
"Command failed: {}",
String::from_utf8_lossy(&output.stderr)
);
serde_json::de::from_slice(&output.stdout).expect("Failed to deserialize stdout to JSON")
}
}

#[test]
fn build_defaults_to_dev() {
let t = TempDir::new().unwrap();
Expand Down Expand Up @@ -62,7 +46,7 @@ fn defaults_to_dev() {
let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

let mut all_profiles = metadata.profiles;
all_profiles.sort();
Expand All @@ -79,7 +63,7 @@ fn can_choose_release() {
let metadata = Scarb::quick_snapbox()
.args(["--release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

let mut all_profiles = metadata.profiles;
all_profiles.sort();
Expand All @@ -96,7 +80,7 @@ fn can_choose_dev() {
let metadata = Scarb::quick_snapbox()
.args(["--dev", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

let mut all_profiles = metadata.profiles;
all_profiles.sort();
Expand Down Expand Up @@ -132,7 +116,7 @@ fn can_choose_release_by_name() {
let metadata = Scarb::quick_snapbox()
.args(["--profile", "release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

let mut all_profiles = metadata.profiles;
all_profiles.sort();
Expand Down Expand Up @@ -168,7 +152,7 @@ fn can_choose_dev_by_name() {
let metadata = Scarb::quick_snapbox()
.args(["--profile", "dev", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

let mut all_profiles = metadata.profiles;
all_profiles.sort();
Expand All @@ -185,7 +169,7 @@ fn can_choose_dev_by_short_name() {
let metadata = Scarb::quick_snapbox()
.args(["-P", "dev", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

let mut all_profiles = metadata.profiles;
all_profiles.sort();
Expand All @@ -207,7 +191,7 @@ fn can_choose_custom_profile() {
let metadata = Scarb::quick_snapbox()
.args(["--profile", "custom", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

let mut all_profiles = metadata.profiles;
all_profiles.sort();
Expand Down Expand Up @@ -244,7 +228,7 @@ fn sierra_replace_ids_defaults_true_in_dev() {
let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "dev".to_string());
assert!(!metadata.compilation_units.is_empty());
Expand All @@ -266,7 +250,7 @@ fn sierra_replace_ids_default_false_in_release() {
let metadata = Scarb::quick_snapbox()
.args(["--profile", "release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "release".to_string());
assert!(!metadata.compilation_units.is_empty());
Expand Down Expand Up @@ -298,7 +282,7 @@ fn compiler_config_set_for_all_profiles() {
let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "dev".to_string());
assert!(!metadata.compilation_units.is_empty());
Expand All @@ -314,7 +298,7 @@ fn compiler_config_set_for_all_profiles() {
let metadata = Scarb::quick_snapbox()
.args(["--release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "release".to_string());
assert!(!metadata.compilation_units.is_empty());
Expand All @@ -336,7 +320,7 @@ fn compiler_config_set_for_all_profiles() {
"1",
])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "some-profile".to_string());
assert!(!metadata.compilation_units.is_empty());
Expand Down Expand Up @@ -364,7 +348,7 @@ fn can_set_replace_ids_in_profile() {
let metadata = Scarb::quick_snapbox()
.args(["--release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "release".to_string());
assert!(!metadata.compilation_units.is_empty());
Expand Down Expand Up @@ -395,7 +379,7 @@ fn profile_precedes_compiler_config() {
let metadata = Scarb::quick_snapbox()
.args(["--release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "release".to_string());
assert!(!metadata.compilation_units.is_empty());
Expand All @@ -411,7 +395,7 @@ fn profile_precedes_compiler_config() {
let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "dev".to_string());
assert!(!metadata.compilation_units.is_empty());
Expand All @@ -438,7 +422,7 @@ fn custom_profiles_inherit_from_dev_by_default() {
let metadata = Scarb::quick_snapbox()
.args(["--profile", "custom", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "custom".to_string());
assert!(!metadata.compilation_units.is_empty());
Expand Down Expand Up @@ -466,7 +450,7 @@ fn custom_profiles_can_inherit_by_name() {
let metadata = Scarb::quick_snapbox()
.args(["--profile", "custom", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "custom".to_string());
assert!(!metadata.compilation_units.is_empty());
Expand Down Expand Up @@ -522,7 +506,7 @@ fn profile_overrides_tool() {
let metadata = Scarb::quick_snapbox()
.args(["metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "dev".to_string());
assert_eq!(metadata.packages.len(), 2);
Expand All @@ -545,7 +529,7 @@ fn profile_overrides_tool() {
let metadata = Scarb::quick_snapbox()
.args(["--release", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_metadata();
.stdout_json::<Metadata>();

assert_eq!(metadata.current_profile, "release".to_string());
assert_eq!(metadata.packages.len(), 2);
Expand Down
23 changes: 3 additions & 20 deletions scarb/tests/e2e/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,13 @@ use std::env;
use assert_fs::prelude::*;
use assert_fs::TempDir;
use indoc::{formatdoc, indoc};
use snapbox::cmd::Command;

use scarb::process::make_executable;

use crate::support::command::Scarb;
use crate::support::command::{CommandExt, Scarb};
use crate::support::filesystem::{path_with_temp_dir, write_script};
use crate::support::project_builder::ProjectBuilder;

trait CommandExt {
fn stdout_json(self) -> BTreeMap<String, String>;
}

impl CommandExt for Command {
fn stdout_json(self) -> BTreeMap<String, String> {
let output = self.output().expect("Failed to spawn command");
assert!(
output.status.success(),
"Command failed: {}",
String::from_utf8_lossy(&output.stderr)
);
serde_json::de::from_slice(&output.stdout).expect("Failed to deserialize stdout to JSON")
}
}

#[test]
fn run_simple_script() {
let t = TempDir::new().unwrap();
Expand Down Expand Up @@ -80,7 +63,7 @@ fn list_scripts() {
"#})
.build(&t);

let output = Scarb::quick_snapbox()
let output: BTreeMap<String, String> = Scarb::quick_snapbox()
.args(["--json", "run"])
.current_dir(&t)
.stdout_json();
Expand All @@ -99,7 +82,7 @@ fn list_empty_scripts() {
"#})
.build(&t);

let output = Scarb::quick_snapbox()
let output: BTreeMap<String, String> = Scarb::quick_snapbox()
.args(["--json", "run"])
.current_dir(&t)
.stdout_json();
Expand Down
17 changes: 17 additions & 0 deletions scarb/tests/e2e/support/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{fs, iter};

use assert_fs::TempDir;
use once_cell::sync::Lazy;
use serde::de::DeserializeOwned;
use snapbox::cmd::cargo_bin;
use snapbox::cmd::Command as SnapboxCommand;

Expand Down Expand Up @@ -110,3 +111,19 @@ impl EnvPath {
}
}
}

pub trait CommandExt {
fn stdout_json<T: DeserializeOwned>(self) -> T;
}

impl CommandExt for SnapboxCommand {
fn stdout_json<T: DeserializeOwned>(self) -> T {
let output = self.output().expect("Failed to spawn command");
assert!(
output.status.success(),
"Command failed: {}",
String::from_utf8_lossy(&output.stderr)
);
serde_json::de::from_slice(&output.stdout).expect("Failed to deserialize stdout to JSON")
}
}

0 comments on commit 3c092c6

Please sign in to comment.