Skip to content

Commit

Permalink
test: increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexD10S committed Sep 20, 2024
1 parent b00503e commit cca59fb
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 94 deletions.
134 changes: 123 additions & 11 deletions crates/pop-cli/src/commands/build/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ async fn guide_user_to_generate_spec(
let default_bootnode = match chain_type {
ChainType::Development => true,
_ => cli
.confirm("Would you like to use local host as a bootnode ?".to_string())
.confirm("Would you like to use local host as a bootnode?".to_string())
.interact()?,
};

Expand All @@ -441,20 +441,20 @@ async fn guide_user_to_generate_spec(

// Prompt for genesis state
let genesis_state = cli
.confirm("Should the genesis state file be generated ?".to_string())
.confirm("Should the genesis state file be generated?".to_string())
.initial_value(true)
.interact()?;

// Prompt for genesis code
let genesis_code = cli
.confirm("Should the genesis code file be generated ?".to_string())
.confirm("Should the genesis code file be generated?".to_string())
.initial_value(true)
.interact()?;

// Only check user to check their profile selection if a live spec is being built on debug mode.
let profile = if !args.release && matches!(chain_type, ChainType::Live) {
cli.confirm(
"Using Debug profile to build a Live specification. Should Release be used instead ?",
"Using Debug profile to build a Live specification. Should Release be used instead?",
)
.initial_value(true)
.interact()?
Expand Down Expand Up @@ -483,11 +483,11 @@ mod tests {
use std::path::PathBuf;

#[tokio::test]
async fn guide_user_to_generate_spec_works() -> anyhow::Result<()> {
async fn guide_user_to_generate_spec_development_works() -> anyhow::Result<()> {
let mut cli = MockCli::new()
.expect_intro("Generate your chain spec")
.expect_confirm("Should the genesis code file be generated ?", true)
.expect_confirm("Should the genesis state file be generated ?", true)
.expect_confirm("Should the genesis code file be generated?", true)
.expect_confirm("Should the genesis state file be generated?", true)
.expect_input(
"Enter the protocol ID that will identify your network:",
"protocol".into(),
Expand Down Expand Up @@ -565,8 +565,121 @@ mod tests {
assert!(user_prompt.genesis_state);
assert!(user_prompt.genesis_code);

cli.verify()?;
Ok(())
cli.verify()
}

#[tokio::test]
async fn guide_user_to_generate_spec_live_works() -> anyhow::Result<()> {
let mut cli = MockCli::new()
.expect_intro("Generate your chain spec")
.expect_confirm("Using Debug profile to build a Live specification. Should Release be used instead?", true) // Because args.release is false
.expect_confirm("Should the genesis code file be generated?", true)
.expect_confirm("Should the genesis state file be generated?", true)
.expect_input(
"Enter the protocol ID that will identify your network:",
"protocol".into(),
)
.expect_select::<RelayChain>(
"Choose the relay chain your chain will be connecting to: ",
Some(false),
true,
Some(vec![
(
RelayChain::Paseo.get_message().unwrap().into(),
RelayChain::Paseo.get_detailed_message().unwrap().into(),
),
(
RelayChain::Westend.get_message().unwrap().into(),
RelayChain::Westend.get_detailed_message().unwrap().into(),
),
(
RelayChain::Kusama.get_message().unwrap().into(),
RelayChain::Kusama.get_detailed_message().unwrap().into(),
),
(
RelayChain::Polkadot.get_message().unwrap().into(),
RelayChain::Polkadot.get_detailed_message().unwrap().into(),
),

]),
3, // "Polkadot Local"
)
.expect_select::<ChainType>(
"Choose the chain type: ",
Some(false),
true,
Some(vec![
(
ChainType::Development.get_message().unwrap().into(),
ChainType::Development.get_detailed_message().unwrap().into(),
),
(ChainType::Local.get_message().unwrap().into(), ChainType::Local.get_detailed_message().unwrap().into()),
(ChainType::Live.get_message().unwrap().into(), ChainType::Live.get_detailed_message().unwrap().into()),
]),
3, // "Live"
)
.expect_confirm("Would you like to use local host as a bootnode?", false)
.expect_input(
"What parachain ID should be used?",
"2002".into(),
)
.expect_input(
"Name of the plain chain spec file. If a path is given, the necessary directories will be created:",
"./my-chain-spec.json".into(),
);

let user_prompt = guide_user_to_generate_spec(
BuildSpecCommand {
output_file: None,
release: false,
id: None,
default_bootnode: true,
chain_type: None,
relay: None,
protocol_id: None,
genesis_state: true,
genesis_code: true,
},
&mut cli,
)
.await?;
assert_eq!(user_prompt.output_file, Some(PathBuf::from("./my-chain-spec.json")));
assert_eq!(user_prompt.id, Some(2002));
assert!(user_prompt.release);
assert!(!user_prompt.default_bootnode);
assert_eq!(user_prompt.chain_type, Some(ChainType::Live));
assert_eq!(user_prompt.relay, Some(RelayChain::Polkadot));
assert_eq!(user_prompt.protocol_id, Some("protocol".into()));
assert!(user_prompt.genesis_state);
assert!(user_prompt.genesis_code);

cli.verify()
}

#[tokio::test]
async fn build_fails_no_parachain() -> anyhow::Result<()> {
let mut cli = MockCli::new()
.expect_intro(format!("Building your chain spec"))
.expect_warning(
"NOTE: this command defaults to DEBUG builds for development chain types. Please use `--release` (or simply `-r` for a release build...)",
);
assert!(matches!(
BuildSpecCommand {
output_file: None,
release: false,
id: Some(2002),
default_bootnode: true,
chain_type: None,
relay: None,
protocol_id: None,
genesis_state: true,
genesis_code: true
}
.build(&mut cli),
anyhow::Result::Err(message) if message.to_string().contains("Failed to get manifest path:"),
));

cli.verify()
}

#[tokio::test]
Expand All @@ -592,7 +705,6 @@ mod tests {
"spec"
);

cli.verify()?;
Ok(())
cli.verify()
}
}
41 changes: 7 additions & 34 deletions crates/pop-cli/src/commands/install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,89 +342,65 @@ async fn run_external_script(script_url: &str) -> anyhow::Result<()> {
mod tests {
use super::*;
use crate::cli::MockCli;
use anyhow::Ok;

#[tokio::test]
async fn intro_works() -> anyhow::Result<()> {
let mut cli = MockCli::new().expect_intro("Install dependencies for development");

assert!(matches!(Command { cli: &mut cli, args: InstallArgs { skip_confirm: false } }
.execute()
.await,anyhow::Result::Err(message) if message.to_string() == "🚫 You have cancelled the installation process."
));

cli.verify()?;
Ok(())
cli.verify()
}

#[tokio::test]
async fn install_mac_works() -> anyhow::Result<()> {
let mut cli = MockCli::new().expect_info("More information about the packages to be installed here: https://docs.substrate.io/install/macos/").expect_confirm("📦 Do you want to proceed with the installation of the following packages: homebrew, protobuf, openssl, rustup and cmake ?", false);

assert!(matches!(
install_mac(&mut Command { cli: &mut cli, args: InstallArgs { skip_confirm: false } })
.await,
anyhow::Result::Err(message) if message.to_string() == "🚫 You have cancelled the installation process."
));

cli.verify()?;
Ok(())
cli.verify()
}

#[tokio::test]
async fn install_arch_works() -> anyhow::Result<()> {
let mut cli = MockCli::new().expect_info("More information about the packages to be installed here: https://docs.substrate.io/install/linux/").expect_confirm("📦 Do you want to proceed with the installation of the following packages: curl, git, clang, make, openssl and rustup ?", false);

assert!(matches!(
install_arch(&mut Command { cli: &mut cli, args: InstallArgs { skip_confirm: false } })
.await,
anyhow::Result::Err(message) if message.to_string() == "🚫 You have cancelled the installation process."
));

cli.verify()?;
Ok(())
cli.verify()
}

#[tokio::test]
async fn install_ubuntu_works() -> anyhow::Result<()> {
let mut cli = MockCli::new().expect_info("More information about the packages to be installed here: https://docs.substrate.io/install/linux/").expect_confirm("📦 Do you want to proceed with the installation of the following packages: git, clang, curl, libssl-dev, protobuf-compiler and rustup ?", false);

assert!(matches!(
install_ubuntu(&mut Command { cli: &mut cli, args: InstallArgs { skip_confirm: false } })
.await,
anyhow::Result::Err(message) if message.to_string() == "🚫 You have cancelled the installation process."
));

cli.verify()?;
Ok(())
cli.verify()
}

#[tokio::test]
async fn install_debian_works() -> anyhow::Result<()> {
let mut cli = MockCli::new().expect_info("More information about the packages to be installed here: https://docs.substrate.io/install/linux/").expect_confirm("📦 Do you want to proceed with the installation of the following packages: cmake, pkg-config, libssl-dev, git, gcc, build-essential, protobuf-compiler, clang, libclang-dev and rustup ?", false);

assert!(matches!(
install_debian(&mut Command { cli: &mut cli, args: InstallArgs { skip_confirm: false } })
.await,
anyhow::Result::Err(message) if message.to_string() == "🚫 You have cancelled the installation process."
));

cli.verify()?;
Ok(())
cli.verify()
}

#[tokio::test]
async fn install_redhat_works() -> anyhow::Result<()> {
let mut cli = MockCli::new().expect_info("More information about the packages to be installed here: https://docs.substrate.io/install/linux/").expect_confirm("📦 Do you want to proceed with the installation of the following packages: cmake, openssl-devel, git, protobuf, protobuf-compiler, clang, clang-devel and rustup ?", false);

assert!(matches!(
install_redhat(&mut Command { cli: &mut cli, args: InstallArgs { skip_confirm: false } })
.await,
anyhow::Result::Err(message) if message.to_string() == "🚫 You have cancelled the installation process."
));

cli.verify()?;
Ok(())
cli.verify()
}
#[tokio::test]
async fn not_supported_message_works() -> anyhow::Result<()> {
Expand All @@ -433,10 +409,7 @@ mod tests {
.expect_warning(
"⚠️ Please refer to https://docs.substrate.io/install/ for setup information.",
);

not_supported_message(&mut cli)?;

cli.verify()?;
Ok(())
cli.verify()
}
}
6 changes: 2 additions & 4 deletions crates/pop-cli/src/commands/new/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ mod tests {
assert_eq!(user_input.contract_type, Some(ContractType::Erc));
assert_eq!(user_input.template, Some(ContractTemplate::ERC20));

cli.verify()?;
Ok(())
cli.verify()
}

#[test]
Expand Down Expand Up @@ -318,8 +317,7 @@ mod tests {
&ContractTemplate::ERC20,
&mut cli,
)?;
cli.verify()?;
Ok(())
cli.verify()
}

#[test]
Expand Down
9 changes: 3 additions & 6 deletions crates/pop-cli/src/commands/new/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ mod tests {
}
.generate_pallet(&mut cli)
.await?;
cli.verify()?;
Ok(())
cli.verify()
}
#[tokio::test]
async fn generate_advanced_pallet_works() -> anyhow::Result<()> {
Expand Down Expand Up @@ -257,8 +256,7 @@ mod tests {
}
.generate_pallet(&mut cli)
.await?;
cli.verify()?;
Ok(())
cli.verify()
}

#[tokio::test]
Expand All @@ -278,7 +276,6 @@ mod tests {
}
.generate_pallet(&mut cli)
.await, anyhow::Result::Err(message) if message.to_string() == "Specify at least a config common type to use default config."));
cli.verify()?;
Ok(())
cli.verify()
}
}
6 changes: 2 additions & 4 deletions crates/pop-cli/src/commands/new/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,7 @@ mod tests {
assert_eq!(user_input.decimals, Some(6));
assert_eq!(user_input.initial_endowment, Some(DEFAULT_INITIAL_ENDOWMENT.into()));

cli.verify()?;
Ok(())
cli.verify()
}

#[tokio::test]
Expand Down Expand Up @@ -545,8 +544,7 @@ mod tests {
&mut cli,
)
.await?;
cli.verify()?;
Ok(())
cli.verify()
}

#[test]
Expand Down
Loading

0 comments on commit cca59fb

Please sign in to comment.