Skip to content

Commit

Permalink
use indoc for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaBatty committed Jun 25, 2024
1 parent 70ddcec commit 0c18d5d
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 255 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions component/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
19 changes: 10 additions & 9 deletions component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
26 changes: 13 additions & 13 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ pub fn fetch_fuels_version(cfg: &DownloadCfg) -> Result<String> {
mod tests {
use super::*;
use dirs::home_dir;
use indoc::indoc;
use std::io::{self, Result};
use tempfile;

Expand Down Expand Up @@ -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()
Expand Down
62 changes: 33 additions & 29 deletions src/toolchain_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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());
Expand All @@ -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,
Expand Down
44 changes: 21 additions & 23 deletions tests/component.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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 <toolchain>'.
"#
You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.
"#
);
assert_eq!(output.stdout, expected_stdout);

Expand All @@ -64,12 +64,12 @@ You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.

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 <toolchain>'.
"#
You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.
"#
);
assert_eq!(output.stdout, expected_stdout);

Expand All @@ -79,12 +79,12 @@ You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.

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 <toolchain>'.
"#
You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.
"#
);
assert_eq!(output.stdout, expected_stdout);

Expand All @@ -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 <toolchain>'.
"#,
You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.
"#
);
assert_eq!(output.stdout, expected_stdout);
expect_files_exist(&latest_toolchain_bin_dir, ALL_BINS);
Expand All @@ -121,13 +120,12 @@ You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.

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 <toolchain>'.
"#
You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.
"#
);
assert_eq!(output.stdout, expected_stdout);
expect_files_exist(&latest_toolchain_bin_dir, ALL_BINS);
Expand Down
Loading

0 comments on commit 0c18d5d

Please sign in to comment.