Skip to content

Commit

Permalink
test: add common test_basics_on between new template and test
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Oct 22, 2024
1 parent f9b7660 commit 9f9ac14
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 39 deletions.
4 changes: 4 additions & 0 deletions cargo-near/src/commands/new/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@ use serde_json::json;

#[tokio::test]
async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>> {
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::<String>()?, "Hello World!");

test_basics_on(&contract_wasm).await?;
Ok(())
}

TEST_BASICS_ON_INCLUDE

18 changes: 18 additions & 0 deletions cargo-near/src/commands/new/test_basics_on.rs.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
async fn test_basics_on(contract_wasm: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
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::<String>()?, "Hello World!");

Ok(())
}
2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,69 @@
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<dyn std::error::Error>> {
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();
cargo_near_integration_tests_dir.join("Cargo.toml")
};
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]
.iter()
.map(|manifest| get_workspaces_rs_version(manifest))
.collect::<Result<Vec<_>, 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<String> {
use color_eyre::eyre::OptionExt;

Expand All @@ -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<std::path::PathBuf> {
fn run_cargo_near_new() -> color_eyre::Result<camino::Utf8PathBuf> {
let out_path = {
let tmp_dir = tempfile::Builder::new()
.prefix("cargo_near_new_")
Expand All @@ -94,5 +109,5 @@ fn run_cargo_near_new() -> color_eyre::Result<std::path::PathBuf> {
},
&scope,
)?;
Ok(out_path)
Ok(out_path.try_into()?)
}

0 comments on commit 9f9ac14

Please sign in to comment.