Skip to content

Commit

Permalink
Fix build tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifropc committed Nov 18, 2024
1 parent 7453642 commit 40609e1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/crates/soroban-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
59 changes: 37 additions & 22 deletions cmd/crates/soroban-test/tests/it/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use predicates::prelude::predicate;
use soroban_test::TestEnv;
use std::env;

#[test]
fn build_all() {
Expand 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]
Expand All @@ -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]
Expand All @@ -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"),
));
}

Expand Down Expand Up @@ -82,20 +77,17 @@ 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)
.arg("build")
.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]
Expand All @@ -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]
Expand Down Expand Up @@ -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")
);
}
9 changes: 6 additions & 3 deletions cmd/soroban-cli/src/commands/contract/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -378,9 +379,11 @@ fn make_rustflags_to_remap_absolute_paths(print: &Print) -> Result<Option<String
}

let registry_prefix = format!("{cargo_home}/registry/src/");
let new_rustflag = format!("--remap-path-prefix={registry_prefix}=");
let new_rustflag = format!("{registry_prefix}=");

let mut rustflags = get_rustflags().unwrap_or_default();
// --remap-path-prefix is separated by space with the arguments
rustflags.push("--remap-path-prefix".to_string());
rustflags.push(new_rustflag);

let rustflags = rustflags.join(" ");
Expand Down

0 comments on commit 40609e1

Please sign in to comment.