diff --git a/integration-tests/tests/cargo_near_new.rs b/integration-tests/tests/cargo_near_new.rs index 82fa26e7..6908fe7a 100644 --- a/integration-tests/tests/cargo_near_new.rs +++ b/integration-tests/tests/cargo_near_new.rs @@ -1,5 +1,12 @@ +use std::process::{Command, Stdio}; + +use camino::Utf8PathBuf; use serde_json::json; +mod crate_metadata; +mod toolchain_channels; +/// This tests logic of `../../cargo-near/src/commands/new/new-project-template/tests/test_basics.rs`, +/// on contract, which is docker-built from project, generated by `cargo near new` #[cfg(target_os = "linux")] #[tokio::test] async fn test_docker_build() -> Result<(), Box> { @@ -7,11 +14,12 @@ async fn test_docker_build() -> Result<(), Box> { setup_tracing(); - let generated_manifest = { - let generated_dir = run_cargo_near_new()?; - generated_dir.join("Cargo.toml") - }; - assert_new_cmd_workspaces_version_equal_to_test(&generated_manifest)?; + let generated_manifest = run_cargo_near_new()?; + crate_metadata::assert_versions_equal( + &tests_manifest(), + &generated_manifest, + "near_workspaces", + )?; let opts = cargo_near_build::docker::DockerBuildOpts::builder() .manifest_path(generated_manifest.clone()) @@ -35,66 +43,61 @@ async fn test_docker_build() -> Result<(), Box> { include! {"../../cargo-near/src/commands/new/test_basics_on.rs.in"} -fn extract_locked_workspaces_version( - manifest_path: &camino::Utf8PathBuf, - package_name: &str, -) -> color_eyre::Result { - let meta = - cargo_near_build::CrateMetadata::collect(manifest_path.clone().try_into()?, false, None)?; +#[tokio::test] +/// This runs `cargo test` on project, generated by `cargo near new` +async fn test_cargo_test_on_generated_project() -> Result<(), Box> { + use cargo_near_integration_tests::setup_tracing; + + setup_tracing(); - let package = meta.find_dependency(package_name)?; + let generated_manifest = run_cargo_near_new()?; - Ok(package.version.clone()) -} + toolchain_channels::assert_equal( + &tests_toolchain(), + &generated_manifest + .parent() + .expect("has a parent") + .join("rust-toolchain.toml"), + )?; -/// This asserts sync of versions in lock-file of `cargo-near-integration-tests` and -/// `cargo near new` generated template -fn assert_new_cmd_workspaces_version_equal_to_test( - generated_manifest: &camino::Utf8PathBuf, -) -> 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") + // somehow `rustup` doesn't respect `rust-toolchain.toml` in + // the target generated dir + let mut cmd = Command::new("cargo"); + cmd.arg("test") + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .current_dir( + generated_manifest + .parent() + .expect("expected to have parent") + .as_str(), + ); + + let (output, output_string) = { + let child = cmd.spawn()?; + let output = child.wait_with_output()?; + let mut output_string = String::new(); + output_string.push_str(&String::from_utf8_lossy(&output.stderr)); + output_string.push_str(&String::from_utf8_lossy(&output.stdout)); + (output, output_string) }; - let versions = [&tests_manifest, generated_manifest] - .iter() - .map(|manifest| extract_locked_workspaces_version(manifest, "near_workspaces")) - .collect::, color_eyre::Report>>()?; - assert_eq!(versions[0], versions[1]); - Ok(()) -} + assert!( + output.status.success(), + "running `cargo test` errored with msg: {}", + output_string + ); -// TODO: add sync of versions of rust-toolchain.toml-s -#[allow(unused)] -fn get_workspaces_rs_version(manifest_path: &camino::Utf8PathBuf) -> color_eyre::Result { - use color_eyre::eyre::OptionExt; + std::fs::remove_dir_all( + generated_manifest + .parent() + .expect("expected to have parent"), + )?; - let toml_table_str = { - let bytes = std::fs::read(manifest_path).map_err(|err| { - color_eyre::eyre::eyre!("read file, {:?}, err {}", manifest_path, err) - })?; - core::str::from_utf8(&bytes)?.to_owned() - }; - let toml_table = toml_table_str.parse::()?; - let entry = toml_table["dev-dependencies"]["near-workspaces"].clone(); - let result = match entry { - toml::Value::String(version_string) => version_string, - toml::Value::Table(workspaces_table) => workspaces_table["version"] - .as_str() - .ok_or_eyre(format!("expected version string {:#?}", workspaces_table))? - .to_owned(), - _ => { - return Err(color_eyre::eyre::eyre!( - "unexpected variant of toml.dev-dependencies.near-workspaces" - )) - } - }; - Ok(result) + Ok(()) } -/// runs a `cargo near new FOLDER` routine while returning the FOLDER +/// runs a `cargo near new FOLDER` routine while returning the FOLDER/Cargo.toml fn run_cargo_near_new() -> color_eyre::Result { let out_path = { let tmp_dir = tempfile::Builder::new() @@ -114,5 +117,16 @@ fn run_cargo_near_new() -> color_eyre::Result { }, &scope, )?; - Ok(out_path.try_into()?) + let out_path: Utf8PathBuf = out_path.try_into()?; + Ok(out_path.join("Cargo.toml")) +} + +fn tests_manifest() -> camino::Utf8PathBuf { + let cargo_near_integration_tests_dir: camino::Utf8PathBuf = env!("CARGO_MANIFEST_DIR").into(); + cargo_near_integration_tests_dir.join("Cargo.toml") +} + +fn tests_toolchain() -> camino::Utf8PathBuf { + let cargo_near_integration_tests_dir: camino::Utf8PathBuf = env!("CARGO_MANIFEST_DIR").into(); + cargo_near_integration_tests_dir.join("../rust-toolchain.toml") } diff --git a/integration-tests/tests/crate_metadata.rs b/integration-tests/tests/crate_metadata.rs new file mode 100644 index 00000000..339ef8b1 --- /dev/null +++ b/integration-tests/tests/crate_metadata.rs @@ -0,0 +1,36 @@ +/// finds version of package in a crate's Cargo.lock +fn get_locked_package_version( + manifest_path: &camino::Utf8PathBuf, + package_name: &str, +) -> color_eyre::Result { + let meta = + cargo_near_build::CrateMetadata::collect(manifest_path.clone().try_into()?, false, None)?; + + let package = meta.find_dependency(package_name)?; + + Ok(package.version.clone()) +} + +/// This asserts sync of versions of *package_name* in lock-file of +/// project_one vs project_two +pub fn assert_versions_equal( + manifest_one: &camino::Utf8PathBuf, + manifest_two: &camino::Utf8PathBuf, + package_name: &str, +) -> cargo_near::CliResult { + let versions = [manifest_one, manifest_two] + .iter() + .map(|manifest| get_locked_package_version(manifest, package_name)) + .collect::, color_eyre::Report>>()?; + + assert_eq!( + versions[0], + versions[1], + "no sync of versions of `{}` in lock-file of `{}` and \ + `{}` projects", + package_name, + manifest_one.as_str(), + manifest_two.as_str(), + ); + Ok(()) +} diff --git a/integration-tests/tests/lib.rs b/integration-tests/tests/lib.rs deleted file mode 100644 index 9a988669..00000000 --- a/integration-tests/tests/lib.rs +++ /dev/null @@ -1,4 +0,0 @@ -mod abi; -mod build; -mod cargo; -mod util; diff --git a/integration-tests/tests/toolchain_channels.rs b/integration-tests/tests/toolchain_channels.rs new file mode 100644 index 00000000..376b3aef --- /dev/null +++ b/integration-tests/tests/toolchain_channels.rs @@ -0,0 +1,40 @@ +/// this asserts sync of rust toolchains channels of file_one vs file_two +pub fn assert_equal( + toolchain_one: &camino::Utf8PathBuf, + toolchain_two: &camino::Utf8PathBuf, +) -> cargo_near::CliResult { + let channels = [toolchain_one, toolchain_two] + .iter() + .map(|file| get_toolchain_channel(file.as_std_path())) + .collect::, color_eyre::Report>>()?; + assert_eq!( + channels[0], + channels[1], + "no sync of channels of the toolchain `{}` \ + and `{}`", + toolchain_one.as_str(), + toolchain_two.as_str(), + ); + Ok(()) +} + +/// parses `rust-toolchain.toml` and returns `channel` +fn get_toolchain_channel(toolchain_file: &std::path::Path) -> color_eyre::Result { + let toml_table_str = { + let bytes = std::fs::read(toolchain_file).map_err(|err| { + color_eyre::eyre::eyre!("read file, {:?}, err {}", toolchain_file, err) + })?; + core::str::from_utf8(&bytes)?.to_owned() + }; + let toml_table = toml_table_str.parse::()?; + let entry = toml_table["toolchain"]["channel"].clone(); + let result = match entry { + toml::Value::String(channel_string) => channel_string, + _ => { + return Err(color_eyre::eyre::eyre!( + "unexpected variant of toml.toolchain.channel" + )) + } + }; + Ok(result) +}