From 9f9ac1464fe13b844f39d3eb7eadf4559b68b58a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dj8yf0=CE=BCl?= Date: Tue, 22 Oct 2024 23:14:56 +0300 Subject: [PATCH] test: add common test_basics_on between new template and test --- cargo-near/src/commands/new/mod.rs | 4 ++ .../new-project-template/tests/test_basics.rs | 19 ++---- .../src/commands/new/test_basics_on.rs.in | 18 ++++++ integration-tests/Cargo.toml | 2 +- ...r_new_integration.rs => cargo_near_new.rs} | 61 ++++++++++++------- 5 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 cargo-near/src/commands/new/test_basics_on.rs.in rename integration-tests/tests/{cargo_near_new_integration.rs => cargo_near_new.rs} (74%) diff --git a/cargo-near/src/commands/new/mod.rs b/cargo-near/src/commands/new/mod.rs index 631dc257..8ed794d7 100644 --- a/cargo-near/src/commands/new/mod.rs +++ b/cargo-near/src/commands/new/mod.rs @@ -48,6 +48,10 @@ impl NewContext { .replace( "cargo-near-new-ci-tool-version-self", env!("CARGO_PKG_VERSION"), + ) + .replace( + "TEST_BASICS_ON_INCLUDE", + include_str!("./test_basics_on.rs.in"), ), ) .wrap_err_with(|| format!("Failed to write to file: {}", new_file_path.display()))?; diff --git a/cargo-near/src/commands/new/new-project-template/tests/test_basics.rs b/cargo-near/src/commands/new/new-project-template/tests/test_basics.rs index e86b4bf0..f60dd454 100644 --- a/cargo-near/src/commands/new/new-project-template/tests/test_basics.rs +++ b/cargo-near/src/commands/new/new-project-template/tests/test_basics.rs @@ -2,22 +2,11 @@ use serde_json::json; #[tokio::test] async fn test_contract_is_operational() -> Result<(), Box> { - let sandbox = near_workspaces::sandbox().await?; let contract_wasm = near_workspaces::compile_project("./").await?; - let contract = sandbox.dev_deploy(&contract_wasm).await?; - - let user_account = sandbox.dev_create_account().await?; - - let outcome = user_account - .call(contract.id(), "set_greeting") - .args_json(json!({"greeting": "Hello World!"})) - .transact() - .await?; - assert!(outcome.is_success()); - - let user_message_outcome = contract.view("get_greeting").args_json(json!({})).await?; - assert_eq!(user_message_outcome.json::()?, "Hello World!"); - + test_basics_on(&contract_wasm).await?; Ok(()) } + +TEST_BASICS_ON_INCLUDE + diff --git a/cargo-near/src/commands/new/test_basics_on.rs.in b/cargo-near/src/commands/new/test_basics_on.rs.in new file mode 100644 index 00000000..c9cf687d --- /dev/null +++ b/cargo-near/src/commands/new/test_basics_on.rs.in @@ -0,0 +1,18 @@ +async fn test_basics_on(contract_wasm: &[u8]) -> Result<(), Box> { + let sandbox = near_workspaces::sandbox().await?; + let contract = sandbox.dev_deploy(&contract_wasm).await?; + + let user_account = sandbox.dev_create_account().await?; + + let outcome = user_account + .call(contract.id(), "set_greeting") + .args_json(json!({"greeting": "Hello World!"})) + .transact() + .await?; + assert!(outcome.is_success()); + + let user_message_outcome = contract.view("get_greeting").args_json(json!({})).await?; + assert_eq!(user_message_outcome.json::()?, "Hello World!"); + + Ok(()) +} diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index f99f72a3..3997f223 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -26,7 +26,7 @@ schemars = "0.8" serde_json = "1.0" syn = "2" tempfile = "3.3" -tokio = "1.0" +tokio = { version = "1.12.0", features = ["full"] } quote = "1.0" near-workspaces = "0.14.1" zstd = "0.13" diff --git a/integration-tests/tests/cargo_near_new_integration.rs b/integration-tests/tests/cargo_near_new.rs similarity index 74% rename from integration-tests/tests/cargo_near_new_integration.rs rename to integration-tests/tests/cargo_near_new.rs index 1a800b4b..72135f1a 100644 --- a/integration-tests/tests/cargo_near_new_integration.rs +++ b/integration-tests/tests/cargo_near_new.rs @@ -1,10 +1,41 @@ +use serde_json::json; + #[cfg(target_os = "linux")] -#[test] -fn test_docker_build() -> cargo_near::CliResult { +#[tokio::test] +async fn test_docker_build() -> Result<(), Box> { use cargo_near_integration_tests::setup_tracing; setup_tracing(); + let generated_manifest = { + let generated_dir = run_cargo_near_new()?; + generated_dir.join("Cargo.toml") + }; + + let opts = cargo_near_build::docker::DockerBuildOpts::builder() + .manifest_path(generated_manifest.clone()) + .context(cargo_near_build::BuildContext::Build) + .build(); + + let artifact = cargo_near_build::docker::build(opts)?; + + std::fs::remove_dir_all( + generated_manifest + .parent() + .expect("expected to have parent"), + )?; + + let contract_wasm = std::fs::read(artifact.path)?; + + test_basics_on(&contract_wasm).await?; + + Ok(()) +} + +include! {"../../cargo-near/src/commands/new/test_basics_on.rs.in"} + +#[test] +fn test_new_cmd_workspaces_toolchain_version() -> cargo_near::CliResult { let tests_manifest = { let cargo_near_integration_tests_dir: camino::Utf8PathBuf = env!("CARGO_MANIFEST_DIR").into(); @@ -12,7 +43,7 @@ fn test_docker_build() -> cargo_near::CliResult { }; let generated_manifest = { let generated_dir = run_cargo_near_new()?; - generated_dir.join("Cargo.toml").try_into()? + generated_dir.join("Cargo.toml") }; let versions = [&tests_manifest, &generated_manifest] @@ -20,35 +51,19 @@ fn test_docker_build() -> cargo_near::CliResult { .map(|manifest| get_workspaces_rs_version(manifest)) .collect::, color_eyre::Report>>()?; - // This ensures sync of versions in source code of tests and `new` generation template + // This ensures sync of versions in source code of tests and `new` generated template assert_eq!(versions[0], versions[1]); - let opts = cargo_near_build::docker::DockerBuildOpts::builder() - .manifest_path(generated_manifest.clone()) - .context(cargo_near_build::BuildContext::Build) - .build(); - - cargo_near_build::docker::build(opts)?; + // TODO: add sync of versions of rust-toolchain.toml-s std::fs::remove_dir_all( generated_manifest .parent() .expect("expected to have parent"), )?; - - // TODO: - // ../../cargo-near/src/commands/new/new-project-template/tests/test_basics.rs - Ok(()) } -#[test] -fn test_new_command() -> cargo_near::CliResult { - let _generated_dir = run_cargo_near_new()?; - Ok(()) -} - -#[cfg(target_os = "linux")] fn get_workspaces_rs_version(manifest_path: &camino::Utf8PathBuf) -> color_eyre::Result { use color_eyre::eyre::OptionExt; @@ -75,7 +90,7 @@ fn get_workspaces_rs_version(manifest_path: &camino::Utf8PathBuf) -> color_eyre: Ok(result) } -fn run_cargo_near_new() -> color_eyre::Result { +fn run_cargo_near_new() -> color_eyre::Result { let out_path = { let tmp_dir = tempfile::Builder::new() .prefix("cargo_near_new_") @@ -94,5 +109,5 @@ fn run_cargo_near_new() -> color_eyre::Result { }, &scope, )?; - Ok(out_path) + Ok(out_path.try_into()?) }