From 0c18d5d09c8a445c70ba80a804d75f5f3ce88ecc Mon Sep 17 00:00:00 2001 From: JoshuaBatty Date: Tue, 25 Jun 2024 11:35:07 +1000 Subject: [PATCH] use indoc for tests --- Cargo.toml | 1 + component/Cargo.toml | 1 + component/src/lib.rs | 19 +- src/download.rs | 26 +-- src/toolchain_override.rs | 62 ++++--- tests/component.rs | 44 +++-- tests/show.rs | 360 +++++++++++++++++++------------------- 7 files changed, 258 insertions(+), 255 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c95590cf0..305550b47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ component = { path = "component" } dirs = "4" flate2 = "1" indicatif = "0.17.7" +indoc = "2.0" semver = { version = "1", features = ["serde"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/component/Cargo.toml b/component/Cargo.toml index 7112fea5f..5907c0676 100644 --- a/component/Cargo.toml +++ b/component/Cargo.toml @@ -8,5 +8,6 @@ publish = false [dependencies] anyhow = "1" +indoc = "2.0" serde = { version = "1.0", features = ["derive"] } toml_edit = { version = "0.13", features = ["serde", "easy"] } diff --git a/component/src/lib.rs b/component/src/lib.rs index f04f4e3b8..c12f3dfe6 100644 --- a/component/src/lib.rs +++ b/component/src/lib.rs @@ -203,17 +203,18 @@ impl Components { #[cfg(test)] mod tests { use super::*; + use indoc::indoc; #[test] fn test_toml() -> Result<()> { - const TOML: &str = r#" -[component.forc-fmt] -name = "forc-fmt" -is_plugin = true -tarball_prefix = "forc-binaries" -executables = ["forc-fmt"] -repository_name = "sway" -targets = ["linux_amd64", "linux_arm64", "darwin_amd64", "darwin_arm64"] -"#; + const TOML: &str = indoc! {r#" + [component.forc-fmt] + name = "forc-fmt" + is_plugin = true + tarball_prefix = "forc-binaries" + executables = ["forc-fmt"] + repository_name = "sway" + targets = ["linux_amd64", "linux_arm64", "darwin_amd64", "darwin_arm64"] + "#}; let components = Components::from_toml(TOML)?; diff --git a/src/download.rs b/src/download.rs index d0c9aadb2..4d562311d 100644 --- a/src/download.rs +++ b/src/download.rs @@ -430,6 +430,7 @@ pub fn fetch_fuels_version(cfg: &DownloadCfg) -> Result { mod tests { use super::*; use dirs::home_dir; + use indoc::indoc; use std::io::{self, Result}; use tempfile; @@ -465,26 +466,25 @@ mod tests { #[test] fn test_fuels_version_from_toml() { - let toml = r#" -[package] -name = "forc" + let toml = indoc! {r#" + [package] + name = "forc" -[dependencies] -fuels = "0.1" -"#; + [dependencies] + fuels = "0.1" + "#}; assert_eq!( "0.1", fuels_version_from_toml(toml_edit::Document::from_str(toml).unwrap()).unwrap() ); - let toml = r#" -[package] -name = "forc" - -[dependencies] -fuels = { version = "0.1", features = ["some-feature"] } -"#; + let toml = indoc! {r#" + [package] + name = "forc" + [dependencies] + fuels = { version = "0.1", features = ["some-feature"] } + "#}; assert_eq!( "0.1", fuels_version_from_toml(toml_edit::Document::from_str(toml).unwrap()).unwrap() diff --git a/src/toolchain_override.rs b/src/toolchain_override.rs index 2cae3c7c9..663648cce 100644 --- a/src/toolchain_override.rs +++ b/src/toolchain_override.rs @@ -212,33 +212,32 @@ impl OverrideCfg { #[cfg(test)] mod tests { - use crate::channel::{BETA_1, BETA_2, BETA_3, NIGHTLY}; - use super::*; + use crate::channel::{BETA_1, BETA_2, BETA_3, NIGHTLY}; + use indoc::indoc; #[test] fn parse_toolchain_override_latest_with_date() { - const TOML: &str = r#"[toolchain] -channel = "latest-2023-01-09" -"#; + const TOML: &str = indoc! {r#" + [toolchain] + channel = "latest-2023-01-09" + "#}; let cfg = OverrideCfg::from_toml(TOML).unwrap(); - assert_eq!(cfg.toolchain.channel.to_string(), "latest-2023-01-09"); - assert!(cfg.components.is_none()); assert_eq!(TOML, cfg.to_string_pretty().unwrap()); } #[test] fn parse_toolchain_override_nightly_with_date() { - const TOML: &str = r#"[toolchain] -channel = "nightly-2023-01-09" + const TOML: &str = indoc! {r#" + [toolchain] + channel = "nightly-2023-01-09" -[components] -forc = "0.33.0" -"#; + [components] + forc = "0.33.0" + "#}; let cfg = OverrideCfg::from_toml(TOML).unwrap(); - assert_eq!(cfg.toolchain.channel.to_string(), "nightly-2023-01-09"); assert_eq!( cfg.components.as_ref().unwrap().get("forc").unwrap(), @@ -249,12 +248,14 @@ forc = "0.33.0" #[test] fn parse_toolchain_override_channel_without_date_error() { - const LATEST: &str = r#"[toolchain] -channel = "latest" -"#; - const NIGHTLY: &str = r#"[toolchain] -channel = "nightly" -"#; + const LATEST: &str = indoc! {r#" + [toolchain] + channel = "latest" + "#}; + const NIGHTLY: &str = indoc! {r#" + [toolchain] + channel = "nightly" + "#}; let result = OverrideCfg::from_toml(LATEST); assert!(result.is_err()); @@ -275,16 +276,19 @@ channel = "nightly" #[test] fn parse_toolchain_override_invalid_tomls() { const EMPTY_STR: &str = ""; - const EMPTY_TOOLCHAIN: &str = r#"[toolchain] -"#; - const INVALID_CHANNEL: &str = r#"[toolchain] -channel = "invalid-channel" -"#; - const EMPTY_COMPONENTS: &str = r#"[toolchain] -channel = "beta-2" - -[components] -"#; + const EMPTY_TOOLCHAIN: &str = indoc! {r#" + [toolchain] + "#}; + const INVALID_CHANNEL: &str = indoc! {r#" + [toolchain] + channel = "invalid-channel" + "#}; + const EMPTY_COMPONENTS: &str = indoc! {r#" + [toolchain] + channel = "beta-2" + + [components] + "#}; for toml in [ EMPTY_STR, diff --git a/tests/component.rs b/tests/component.rs index c5213c606..f76ea831d 100644 --- a/tests/component.rs +++ b/tests/component.rs @@ -1,6 +1,6 @@ use anyhow::Result; use fuelup::{fmt::format_toolchain_with_target, target_triple::TargetTriple}; - +use indoc::formatdoc; pub mod testcfg; use testcfg::{FuelupState, ALL_BINS, DATE}; @@ -49,12 +49,12 @@ fn fuelup_component_add_disallowed() -> Result<()> { testcfg::setup(FuelupState::LatestToolchainInstalled, &|cfg| { let output = cfg.fuelup(&["component", "add", "forc@0.19.1"]); - let expected_stdout = format!( + let expected_stdout = formatdoc!( r#"Installing specific components is reserved for custom toolchains. -You are currently using '{latest}'. + You are currently using '{latest}'. -You may create a custom toolchain using 'fuelup toolchain new '. -"# + You may create a custom toolchain using 'fuelup toolchain new '. + "# ); assert_eq!(output.stdout, expected_stdout); @@ -64,12 +64,12 @@ You may create a custom toolchain using 'fuelup toolchain new '. testcfg::setup(FuelupState::NightlyInstalled, &|cfg| { let output = cfg.fuelup(&["component", "add", "forc@.19.1"]); - let expected_stdout = format!( + let expected_stdout = formatdoc!( r#"Installing specific components is reserved for custom toolchains. -You are currently using '{nightly}'. + You are currently using '{nightly}'. -You may create a custom toolchain using 'fuelup toolchain new '. -"# + You may create a custom toolchain using 'fuelup toolchain new '. + "# ); assert_eq!(output.stdout, expected_stdout); @@ -79,12 +79,12 @@ You may create a custom toolchain using 'fuelup toolchain new '. testcfg::setup(FuelupState::NightlyDateInstalled, &|cfg| { let output = cfg.fuelup(&["component", "add", "forc@.19.1"]); - let expected_stdout = format!( + let expected_stdout = formatdoc!( r#"Installing specific components is reserved for custom toolchains. -You are currently using '{nightly_date}'. + You are currently using '{nightly_date}'. -You may create a custom toolchain using 'fuelup toolchain new '. -"# + You may create a custom toolchain using 'fuelup toolchain new '. + "# ); assert_eq!(output.stdout, expected_stdout); @@ -104,13 +104,12 @@ fn fuelup_component_remove_disallowed() -> Result<()> { expect_files_exist(&latest_toolchain_bin_dir, ALL_BINS); let output = cfg.fuelup(&["component", "remove", "forc"]); - - let expected_stdout = format!( + let expected_stdout = formatdoc!( r#"Removing specific components is reserved for custom toolchains. -You are currently using '{latest}'. + You are currently using '{latest}'. -You may create a custom toolchain using 'fuelup toolchain new '. -"#, + You may create a custom toolchain using 'fuelup toolchain new '. + "# ); assert_eq!(output.stdout, expected_stdout); expect_files_exist(&latest_toolchain_bin_dir, ALL_BINS); @@ -121,13 +120,12 @@ You may create a custom toolchain using 'fuelup toolchain new '. expect_files_exist(&latest_toolchain_bin_dir, ALL_BINS); let output = cfg.fuelup(&["component", "remove", "forc"]); - - let expected_stdout = format!( + let expected_stdout = formatdoc!( r#"Removing specific components is reserved for custom toolchains. -You are currently using '{nightly_date}'. + You are currently using '{nightly_date}'. -You may create a custom toolchain using 'fuelup toolchain new '. -"# + You may create a custom toolchain using 'fuelup toolchain new '. + "# ); assert_eq!(output.stdout, expected_stdout); expect_files_exist(&latest_toolchain_bin_dir, ALL_BINS); diff --git a/tests/show.rs b/tests/show.rs index 3f409e8ca..e287cca7b 100644 --- a/tests/show.rs +++ b/tests/show.rs @@ -4,6 +4,7 @@ use fuelup::{ target_triple::TargetTriple, toolchain_override::{self, OverrideCfg, ToolchainCfg, ToolchainOverride}, }; +use indoc::formatdoc; use std::str::FromStr; pub mod testcfg; @@ -18,36 +19,35 @@ fn fuelup_show_latest() -> Result<()> { let target = TargetTriple::from_host().unwrap(); let fuelup_home = cfg.fuelup_dir(); let fuelup_home_str = fuelup_home.to_string_lossy(); - let expected_stdout = format!( + let expected_stdout = formatdoc! { r#"Default host: {target} -fuelup home: {fuelup_home_str} + fuelup home: {fuelup_home_str} -Installed toolchains --------------------- -latest-{target} (default) -nightly-{target} -nightly-2022-08-30-{target} - -active toolchain ----------------- -latest-{target} (default) - forc : 0.1.0 - - forc-client - - forc-deploy : 0.1.0 - - forc-run : 0.1.0 - - forc-crypto : 0.1.0 - - forc-debug : 0.1.0 - - forc-doc : 0.1.0 - - forc-explore : 0.1.0 - - forc-fmt : 0.1.0 - - forc-lsp : 0.1.0 - - forc-tx : 0.1.0 - - forc-wallet : 0.1.0 - fuel-core : 0.1.0 - fuel-core-keygen : not found -"# - ); + Installed toolchains + -------------------- + latest-{target} (default) + nightly-{target} + nightly-2022-08-30-{target} + active toolchain + ---------------- + latest-{target} (default) + forc : 0.1.0 + - forc-client + - forc-deploy : 0.1.0 + - forc-run : 0.1.0 + - forc-crypto : 0.1.0 + - forc-debug : 0.1.0 + - forc-doc : 0.1.0 + - forc-explore : 0.1.0 + - forc-fmt : 0.1.0 + - forc-lsp : 0.1.0 + - forc-tx : 0.1.0 + - forc-wallet : 0.1.0 + fuel-core : 0.1.0 + fuel-core-keygen : not found + "# + }; assert_eq!(stdout.to_string(), expected_stdout); })?; Ok(()) @@ -62,35 +62,35 @@ fn fuelup_show_and_switch() -> Result<()> { let target = TargetTriple::from_host().unwrap(); let fuelup_home = cfg.fuelup_dir(); let fuelup_home_str = fuelup_home.to_string_lossy(); - let expected_stdout = format!( + let expected_stdout = formatdoc! { r#"Default host: {target} -fuelup home: {fuelup_home_str} + fuelup home: {fuelup_home_str} -Installed toolchains --------------------- -latest-{target} (default) -nightly-{target} -nightly-2022-08-30-{target} + Installed toolchains + -------------------- + latest-{target} (default) + nightly-{target} + nightly-2022-08-30-{target} -active toolchain ----------------- -latest-{target} (default) - forc : 0.1.0 - - forc-client - - forc-deploy : 0.1.0 - - forc-run : 0.1.0 - - forc-crypto : 0.1.0 - - forc-debug : 0.1.0 - - forc-doc : 0.1.0 - - forc-explore : 0.1.0 - - forc-fmt : 0.1.0 - - forc-lsp : 0.1.0 - - forc-tx : 0.1.0 - - forc-wallet : 0.1.0 - fuel-core : 0.1.0 - fuel-core-keygen : not found -"# - ); + active toolchain + ---------------- + latest-{target} (default) + forc : 0.1.0 + - forc-client + - forc-deploy : 0.1.0 + - forc-run : 0.1.0 + - forc-crypto : 0.1.0 + - forc-debug : 0.1.0 + - forc-doc : 0.1.0 + - forc-explore : 0.1.0 + - forc-fmt : 0.1.0 + - forc-lsp : 0.1.0 + - forc-tx : 0.1.0 + - forc-wallet : 0.1.0 + fuel-core : 0.1.0 + fuel-core-keygen : not found + "# + }; assert_eq!(stdout, expected_stdout); cfg.fuelup(&["default", "nightly"]); @@ -98,35 +98,35 @@ latest-{target} (default) stdout = String::from_utf8_lossy(&stripped); let fuelup_home = cfg.fuelup_dir(); let fuelup_home_str = fuelup_home.to_string_lossy(); - let expected_stdout = format!( + let expected_stdout = formatdoc! { r#"Default host: {target} -fuelup home: {fuelup_home_str} + fuelup home: {fuelup_home_str} -Installed toolchains --------------------- -latest-{target} -nightly-{target} (default) -nightly-2022-08-30-{target} + Installed toolchains + -------------------- + latest-{target} + nightly-{target} (default) + nightly-2022-08-30-{target} -active toolchain ----------------- -nightly-{target} (default) - forc : 0.2.0 - - forc-client - - forc-deploy : 0.2.0 - - forc-run : 0.2.0 - - forc-crypto : 0.2.0 - - forc-debug : 0.2.0 - - forc-doc : 0.2.0 - - forc-explore : 0.2.0 - - forc-fmt : 0.2.0 - - forc-lsp : 0.2.0 - - forc-tx : 0.2.0 - - forc-wallet : 0.2.0 - fuel-core : 0.2.0 - fuel-core-keygen : not found -"# - ); + active toolchain + ---------------- + nightly-{target} (default) + forc : 0.2.0 + - forc-client + - forc-deploy : 0.2.0 + - forc-run : 0.2.0 + - forc-crypto : 0.2.0 + - forc-debug : 0.2.0 + - forc-doc : 0.2.0 + - forc-explore : 0.2.0 + - forc-fmt : 0.2.0 + - forc-lsp : 0.2.0 + - forc-tx : 0.2.0 + - forc-wallet : 0.2.0 + fuel-core : 0.2.0 + fuel-core-keygen : not found + "# + }; assert_eq!(stdout, expected_stdout); })?; @@ -142,34 +142,33 @@ fn fuelup_show_custom() -> Result<()> { let target = TargetTriple::from_host().unwrap(); let fuelup_home = cfg.fuelup_dir(); let fuelup_home_str = fuelup_home.to_string_lossy(); - let expected_stdout = format!( + let expected_stdout = formatdoc! { r#"Default host: {target} -fuelup home: {fuelup_home_str} - -Installed toolchains --------------------- -my_toolchain (default) + fuelup home: {fuelup_home_str} -active toolchain ----------------- -my_toolchain (default) - forc : not found - - forc-client - - forc-deploy : not found - - forc-run : not found - - forc-crypto : not found - - forc-debug : not found - - forc-doc : not found - - forc-explore : not found - - forc-fmt : not found - - forc-lsp : not found - - forc-tx : not found - - forc-wallet : not found - fuel-core : not found - fuel-core-keygen : not found -"# - ); + Installed toolchains + -------------------- + my_toolchain (default) + active toolchain + ---------------- + my_toolchain (default) + forc : not found + - forc-client + - forc-deploy : not found + - forc-run : not found + - forc-crypto : not found + - forc-debug : not found + - forc-doc : not found + - forc-explore : not found + - forc-fmt : not found + - forc-lsp : not found + - forc-tx : not found + - forc-wallet : not found + fuel-core : not found + fuel-core-keygen : not found + "# + }; assert_eq!(stdout, expected_stdout); })?; Ok(()) @@ -183,34 +182,33 @@ fn fuelup_show_override() -> Result<()> { let target = TargetTriple::from_host().unwrap(); let fuelup_home = cfg.fuelup_dir(); let fuelup_home_str = fuelup_home.to_string_lossy(); - let expected_stdout = format!( + let expected_stdout = formatdoc! { r#"Default host: {target} -fuelup home: {fuelup_home_str} + fuelup home: {fuelup_home_str} -Installed toolchains --------------------- -latest-{target} (default) + Installed toolchains + -------------------- + latest-{target} (default) -active toolchain ----------------- -beta-1-{target} (override), path: {} - forc : not found - - forc-client - - forc-deploy : not found - - forc-run : not found - - forc-crypto : not found - - forc-debug : not found - - forc-doc : not found - - forc-explore : not found - - forc-fmt : not found - - forc-lsp : not found - - forc-tx : not found - - forc-wallet : not found - fuel-core : not found - fuel-core-keygen : not found -"#, - cfg.home.join(FUEL_TOOLCHAIN_TOML_FILE).display() - ); + active toolchain + ---------------- + beta-1-{target} (override), path: {} + forc : not found + - forc-client + - forc-deploy : not found + - forc-run : not found + - forc-crypto : not found + - forc-debug : not found + - forc-doc : not found + - forc-explore : not found + - forc-fmt : not found + - forc-lsp : not found + - forc-tx : not found + - forc-wallet : not found + fuel-core : not found + fuel-core-keygen : not found + "#, cfg.home.join(FUEL_TOOLCHAIN_TOML_FILE).display() + }; assert_eq!(stdout, expected_stdout); })?; Ok(()) @@ -225,35 +223,35 @@ fn fuelup_show_latest_then_override() -> Result<()> { let target = TargetTriple::from_host().unwrap(); let fuelup_home = cfg.fuelup_dir(); let fuelup_home_str = fuelup_home.to_string_lossy(); - let expected_stdout = format!( + let expected_stdout = formatdoc! { r#"Default host: {target} -fuelup home: {fuelup_home_str} + fuelup home: {fuelup_home_str} -Installed toolchains --------------------- -latest-{target} (default) -nightly-{target} -nightly-2022-08-30-{target} + Installed toolchains + -------------------- + latest-{target} (default) + nightly-{target} + nightly-2022-08-30-{target} -active toolchain ----------------- -latest-{target} (default) - forc : 0.1.0 - - forc-client - - forc-deploy : 0.1.0 - - forc-run : 0.1.0 - - forc-crypto : 0.1.0 - - forc-debug : 0.1.0 - - forc-doc : 0.1.0 - - forc-explore : 0.1.0 - - forc-fmt : 0.1.0 - - forc-lsp : 0.1.0 - - forc-tx : 0.1.0 - - forc-wallet : 0.1.0 - fuel-core : 0.1.0 - fuel-core-keygen : not found -"#, - ); + active toolchain + ---------------- + latest-{target} (default) + forc : 0.1.0 + - forc-client + - forc-deploy : 0.1.0 + - forc-run : 0.1.0 + - forc-crypto : 0.1.0 + - forc-debug : 0.1.0 + - forc-doc : 0.1.0 + - forc-explore : 0.1.0 + - forc-fmt : 0.1.0 + - forc-lsp : 0.1.0 + - forc-tx : 0.1.0 + - forc-wallet : 0.1.0 + fuel-core : 0.1.0 + fuel-core-keygen : not found + "# + }; assert_eq!(stdout, expected_stdout); let override_path = cfg.home.join(FUEL_TOOLCHAIN_TOML_FILE); @@ -274,35 +272,35 @@ latest-{target} (default) stripped = strip_ansi_escapes::strip(cfg.fuelup(&["show"]).stdout); stdout = String::from_utf8_lossy(&stripped); - let expected_stdout = format!( + let expected_stdout = formatdoc! { r#"Default host: {target} -fuelup home: {fuelup_home_str} + fuelup home: {fuelup_home_str} -Installed toolchains --------------------- -latest-{target} (default) -nightly-{target} -nightly-2022-08-30-{target} (override) + Installed toolchains + -------------------- + latest-{target} (default) + nightly-{target} + nightly-2022-08-30-{target} (override) -active toolchain ----------------- -nightly-2022-08-30-{target} (override), path: {ovveride_path_str} - forc : 0.2.0 - - forc-client - - forc-deploy : 0.2.0 - - forc-run : 0.2.0 - - forc-crypto : 0.2.0 - - forc-debug : 0.2.0 - - forc-doc : 0.2.0 - - forc-explore : 0.2.0 - - forc-fmt : 0.2.0 - - forc-lsp : 0.2.0 - - forc-tx : 0.2.0 - - forc-wallet : 0.2.0 - fuel-core : 0.2.0 - fuel-core-keygen : not found -"#, - ); + active toolchain + ---------------- + nightly-2022-08-30-{target} (override), path: {ovveride_path_str} + forc : 0.2.0 + - forc-client + - forc-deploy : 0.2.0 + - forc-run : 0.2.0 + - forc-crypto : 0.2.0 + - forc-debug : 0.2.0 + - forc-doc : 0.2.0 + - forc-explore : 0.2.0 + - forc-fmt : 0.2.0 + - forc-lsp : 0.2.0 + - forc-tx : 0.2.0 + - forc-wallet : 0.2.0 + fuel-core : 0.2.0 + fuel-core-keygen : not found + "# + }; assert_eq!(stdout, expected_stdout); })?; Ok(())