From 01e5fe1276356790a849b1bacf8689f36db197ad Mon Sep 17 00:00:00 2001 From: Gleb Date: Fri, 15 Nov 2024 12:21:18 -0800 Subject: [PATCH] Fix build tests --- Cargo.lock | 1 + cmd/crates/soroban-test/Cargo.toml | 1 + cmd/crates/soroban-test/tests/it/build.rs | 59 ++++++++++++------- .../src/commands/contract/build.rs | 9 ++- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9ffab2eb4..2a9d9dfbe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4602,6 +4602,7 @@ dependencies = [ "either", "fs_extra", "hex", + "home", "predicates", "sep5", "serde_json", diff --git a/cmd/crates/soroban-test/Cargo.toml b/cmd/crates/soroban-test/Cargo.toml index 3d137d158..6911ef65e 100644 --- a/cmd/crates/soroban-test/Cargo.toml +++ b/cmd/crates/soroban-test/Cargo.toml @@ -33,6 +33,7 @@ assert_fs = "1.0.7" predicates = { workspace = true } fs_extra = "1.3.0" toml = { workspace = true } +home = "0.5.9" [dev-dependencies] diff --git a/cmd/crates/soroban-test/tests/it/build.rs b/cmd/crates/soroban-test/tests/it/build.rs index 925d88df1..92baa9322 100644 --- a/cmd/crates/soroban-test/tests/it/build.rs +++ b/cmd/crates/soroban-test/tests/it/build.rs @@ -1,5 +1,6 @@ use predicates::prelude::predicate; use soroban_test::TestEnv; +use std::env; #[test] fn build_all() { @@ -13,11 +14,9 @@ fn build_all() { .arg("--print-commands-only") .assert() .success() - .stdout(predicate::eq("\ -cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release + .stdout(predicate::eq(with_flags("cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release cargo rustc --manifest-path=contracts/call/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -cargo rustc --manifest-path=contracts/add/add2/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -")); +cargo rustc --manifest-path=contracts/add/add2/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release"))); } #[test] @@ -33,9 +32,7 @@ fn build_package_by_name() { .arg("--package=add") .assert() .success() - .stdout(predicate::eq("\ -cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -")); + .stdout(predicate::eq(with_flags("cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release"))); } #[test] @@ -51,9 +48,7 @@ fn build_package_by_current_dir() { .assert() .success() .stdout(predicate::eq( - "\ -cargo rustc --manifest-path=Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -", + with_flags("cargo rustc --manifest-path=Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release"), )); } @@ -82,6 +77,7 @@ fn build_all_when_in_non_package_directory() { let sandbox = TestEnv::default(); let cargo_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); let fixture_path = cargo_dir.join("tests/fixtures/workspace/contracts/add/src/"); + sandbox .new_assert_cmd("contract") .current_dir(fixture_path) @@ -89,13 +85,9 @@ fn build_all_when_in_non_package_directory() { .arg("--print-commands-only") .assert() .success() - .stdout(predicate::eq( - "\ -cargo rustc --manifest-path=../Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -cargo rustc --manifest-path=../../call/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -cargo rustc --manifest-path=../add2/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -", - )); + .stdout(predicate::eq(with_flags( + "cargo rustc --manifest-path=../Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release" + ))); } #[test] @@ -110,11 +102,7 @@ fn build_default_members() { .arg("--print-commands-only") .assert() .success() - .stdout(predicate::eq( - "\ -cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release -", - )); + .stdout(predicate::eq(with_flags("cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release"))); } #[test] @@ -150,3 +138,30 @@ fn build_with_metadata() { .stdout(predicate::str::contains("Description: A test add contract")) .stdout(predicate::str::contains("contract meta: added on build")); } + +fn with_flags(expected: &str) -> String { + let cargo_home = home::cargo_home().unwrap(); + let cargo_home = format!("{}", cargo_home.display()); + let registry_prefix = format!("{cargo_home}/registry/src/"); + + let vec: Vec<_> = if env::var("RUSTFLAGS").is_ok() { + expected.split("\n").map(|x| x.to_string()).collect() + } else { + expected + .split("\n") + .map(|x| { + format!( + "CARGO_BUILD_RUSTFLAGS='--remap-path-prefix {}=' {}", + registry_prefix, x + ) + }) + .collect() + }; + + return format!( + "\ +{} +", + vec.join("\n") + ); +} diff --git a/cmd/soroban-cli/src/commands/contract/build.rs b/cmd/soroban-cli/src/commands/contract/build.rs index ac1aa1302..8377c3c3c 100644 --- a/cmd/soroban-cli/src/commands/contract/build.rs +++ b/cmd/soroban-cli/src/commands/contract/build.rs @@ -323,8 +323,9 @@ impl Cmd { /// debugging is expected to be minimal. /// /// This works by setting the `CARGO_BUILD_RUSTFLAGS` environment variable, -/// with appropriate `--remap-path-prefix` option. It preserves the values of an -/// existing `CARGO_BUILD_RUSTFLAGS` environment variable. +/// with an appropriate +/// [`--remap-path-prefix`](https://doc.rust-lang.org/rustc/command-line-arguments.html#--remap-path-prefix-remap-source-names-in-output) +/// option. It preserves the values of an existing `CARGO_BUILD_RUSTFLAGS` environment variable. /// /// This must be done some via some variation of `RUSTFLAGS` and not as /// arguments to `cargo rustc` because the latter only applies to the crate @@ -378,9 +379,11 @@ fn make_rustflags_to_remap_absolute_paths(print: &Print) -> Result