Skip to content

Commit 3ec7ed7

Browse files
committed
test: introduce SKIP_RUST env var to skip rust compilation in
integration tests
1 parent cfa443f commit 3ec7ed7

File tree

3 files changed

+103
-60
lines changed

3 files changed

+103
-60
lines changed

tests/integration/src/cargo_proj/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn panic_error(what: &str, err: impl Into<anyhow::Error>) -> ! {
4444

4545
pub mod paths;
4646
use self::paths::CargoPathExt;
47+
use crate::compiler_test::skip_rust_compilation;
4748

4849
/*
4950
*
@@ -204,6 +205,13 @@ impl ProjectBuilder {
204205

205206
/// Creates the project.
206207
pub fn build(mut self) -> Project {
208+
let last_path_component =
209+
self.root.root().file_name().unwrap().to_string_lossy().to_string();
210+
if skip_rust_compilation(&self.root(), &last_path_component) {
211+
// Return the root directory without re-creating any files
212+
return self.root;
213+
}
214+
207215
// First, clean the directory if it already exists
208216
self.rm_root();
209217

tests/integration/src/compiler_test.rs

Lines changed: 92 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -167,75 +167,88 @@ impl CompilerTest {
167167
/// Set the Rust source code to compile a library Cargo project to Wasm module
168168
pub fn rust_source_cargo_lib(
169169
cargo_project_folder: PathBuf,
170+
artifact_name: &str,
170171
is_build_std: bool,
171172
entry_func_name: Option<String>,
172173
) -> Self {
173-
let manifest_path = cargo_project_folder.join("Cargo.toml");
174-
let mut cargo_build_cmd = Command::new("cargo");
175-
let compiler_workspace_dir = get_workspace_dir();
176-
// Enable Wasm bulk-memory proposal (uses Wasm `memory.copy` op instead of `memcpy` import)
177-
// Remap the compiler workspace directory to `~` to have a reproducible build that does not
178-
// have the absolute local path baked into the Wasm binary
179-
cargo_build_cmd.env(
180-
"RUSTFLAGS",
181-
format!(
182-
"-C target-feature=+bulk-memory --remap-path-prefix {compiler_workspace_dir}=~"
183-
),
184-
);
185-
cargo_build_cmd
186-
.arg("build")
187-
.arg("--manifest-path")
188-
.arg(manifest_path)
189-
.arg("--release")
190-
.arg("--target=wasm32-wasi");
191-
if is_build_std {
192-
// compile std as part of crate graph compilation
193-
// https://doc.rust-lang.org/cargo/reference/unstable.html#build-std
194-
cargo_build_cmd.arg("-Z")
174+
let expected_wasm_artifact_path = wasm_artifact_path(&cargo_project_folder, artifact_name);
175+
// dbg!(&wasm_artifact_path);
176+
let wasm_artifact_path = if !skip_rust_compilation(&cargo_project_folder, artifact_name)
177+
|| !expected_wasm_artifact_path.exists()
178+
{
179+
let manifest_path = cargo_project_folder.join("Cargo.toml");
180+
let mut cargo_build_cmd = Command::new("cargo");
181+
let compiler_workspace_dir = get_workspace_dir();
182+
// Enable Wasm bulk-memory proposal (uses Wasm `memory.copy` op instead of `memcpy`
183+
// import) Remap the compiler workspace directory to `~` to have a
184+
// reproducible build that does not have the absolute local path baked into
185+
// the Wasm binary
186+
cargo_build_cmd.env(
187+
"RUSTFLAGS",
188+
format!(
189+
"-C target-feature=+bulk-memory --remap-path-prefix {compiler_workspace_dir}=~"
190+
),
191+
);
192+
cargo_build_cmd
193+
.arg("build")
194+
.arg("--manifest-path")
195+
.arg(manifest_path)
196+
.arg("--release")
197+
.arg("--target=wasm32-wasi");
198+
if is_build_std {
199+
// compile std as part of crate graph compilation
200+
// https://doc.rust-lang.org/cargo/reference/unstable.html#build-std
201+
cargo_build_cmd.arg("-Z")
195202
.arg("build-std=std,core,alloc,panic_abort")
196203
.arg("-Z")
197204
// abort on panic without message formatting (core::fmt uses call_indirect)
198205
.arg("build-std-features=panic_immediate_abort");
199-
}
200-
let mut child = cargo_build_cmd
201-
.arg("--message-format=json-render-diagnostics")
202-
.stdout(Stdio::piped())
203-
.spawn()
204-
.unwrap_or_else(|_| {
205-
panic!(
206-
"Failed to execute cargo build {}.",
207-
cargo_build_cmd
208-
.get_args()
209-
.map(|arg| format!("'{}'", arg.to_str().unwrap()))
210-
.collect::<Vec<_>>()
211-
.join(" ")
212-
)
206+
}
207+
let mut child = cargo_build_cmd
208+
.arg("--message-format=json-render-diagnostics")
209+
.stdout(Stdio::piped())
210+
.spawn()
211+
.unwrap_or_else(|_| {
212+
panic!(
213+
"Failed to execute cargo build {}.",
214+
cargo_build_cmd
215+
.get_args()
216+
.map(|arg| format!("'{}'", arg.to_str().unwrap()))
217+
.collect::<Vec<_>>()
218+
.join(" ")
219+
)
220+
});
221+
222+
// Find the Wasm artifacts from the cargo build output for debugging purposes
223+
let mut wasm_artifacts = find_wasm_artifacts(&mut child);
224+
let output = child.wait().expect("Couldn't get cargo's exit status");
225+
if !output.success() {
226+
report_cargo_error(child);
227+
}
228+
assert!(output.success());
229+
// filter out dependencies
230+
wasm_artifacts.retain(|path| {
231+
let path_str = path.to_str().unwrap();
232+
!path_str.contains("release/deps")
213233
});
214-
let mut wasm_artifacts = find_wasm_artifacts(&mut child);
215-
let output = child.wait().expect("Couldn't get cargo's exit status");
216-
if !output.success() {
217-
report_cargo_error(child);
218-
}
219-
assert!(output.success());
220-
// filter out dependencies
221-
wasm_artifacts.retain(|path| {
222-
let path_str = path.to_str().unwrap();
223-
!path_str.contains("release/deps")
224-
});
225-
// dbg!(&wasm_artifacts);
226-
assert_eq!(wasm_artifacts.len(), 1, "Expected one Wasm artifact");
227-
let wasm_comp_path = &wasm_artifacts.first().unwrap();
228-
let artifact_name = wasm_comp_path.file_stem().unwrap().to_str().unwrap().to_string();
229-
dbg!(&artifact_name);
234+
dbg!(&wasm_artifacts);
235+
assert_eq!(wasm_artifacts.len(), 1, "Expected one Wasm artifact");
236+
wasm_artifacts.first().unwrap().to_path_buf()
237+
} else {
238+
expected_wasm_artifact_path
239+
};
240+
230241
let entrypoint = entry_func_name.map(|func_name| FunctionIdent {
231-
module: Ident::new(Symbol::intern(artifact_name.clone()), SourceSpan::default()),
242+
module: Ident::new(Symbol::intern(artifact_name), SourceSpan::default()),
232243
function: Ident::new(Symbol::intern(func_name.to_string()), SourceSpan::default()),
233244
});
234-
let input_file = InputFile::from_path(wasm_artifacts.first().unwrap()).unwrap();
245+
let input_file = InputFile::from_path(wasm_artifact_path).unwrap();
235246
Self {
236247
config: WasmTranslationConfig::default(),
237248
session: default_session(input_file),
238-
source: CompilerTestSource::RustCargoLib { artifact_name },
249+
source: CompilerTestSource::RustCargoLib {
250+
artifact_name: artifact_name.to_string(),
251+
},
239252
entrypoint,
240253
hir: None,
241254
masm_program: None,
@@ -416,7 +429,7 @@ impl CompilerTest {
416429
.as_str(),
417430
)
418431
.build();
419-
Self::rust_source_cargo_lib(proj.root(), is_build_std, Some("entrypoint".to_string()))
432+
Self::rust_source_cargo_lib(proj.root(), name, is_build_std, Some("entrypoint".to_string()))
420433
}
421434

422435
/// Set the Rust source code to compile with `miden-stdlib-sys` (stdlib + intrinsics)
@@ -481,7 +494,7 @@ impl CompilerTest {
481494
)
482495
.build();
483496

484-
Self::rust_source_cargo_lib(proj.root(), is_build_std, Some("entrypoint".to_string()))
497+
Self::rust_source_cargo_lib(proj.root(), name, is_build_std, Some("entrypoint".to_string()))
485498
}
486499

487500
/// Compare the compiled Wasm against the expected output
@@ -564,7 +577,8 @@ impl CompilerTest {
564577
/// The compiled Wasm component/module
565578
fn wasm_bytes(&self) -> Vec<u8> {
566579
match &self.session.input.file {
567-
InputType::Real(file_path) => fs::read(file_path).unwrap(),
580+
InputType::Real(file_path) => fs::read(file_path)
581+
.unwrap_or_else(|_| panic!("Failed to read Wasm file: {}", file_path.display())),
568582
InputType::Stdin { name: _, input } => input.clone(),
569583
}
570584
}
@@ -585,6 +599,26 @@ impl CompilerTest {
585599
}
586600
}
587601

602+
fn wasm_artifact_path(cargo_project_folder: &Path, artifact_name: &str) -> PathBuf {
603+
cargo_project_folder
604+
.to_path_buf()
605+
.join("target")
606+
.join("wasm32-wasi")
607+
.join("release")
608+
.join(artifact_name)
609+
.with_extension("wasm")
610+
}
611+
612+
/// Directs if we should do the Rust compilation step or not
613+
pub fn skip_rust_compilation(cargo_project_folder: &Path, artifact_name: &str) -> bool {
614+
let expected_wasm_artifact_path = wasm_artifact_path(cargo_project_folder, artifact_name);
615+
let skip_rust = std::env::var("SKIP_RUST").is_ok() && expected_wasm_artifact_path.exists();
616+
if skip_rust {
617+
eprintln!("Skipping Rust compilation");
618+
};
619+
skip_rust
620+
}
621+
588622
// Assemble the VM MASM program from the compiled IR MASM modules
589623
fn masm_prog_from_modules(
590624
user_ns_name: &str,

tests/integration/src/rust_masm_tests/rust_sdk.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ use crate::{cargo_proj::project, compiler_test::sdk_crate_path, CompilerTest};
66

77
#[test]
88
fn account() {
9+
let artifact_name = "miden_sdk_account_test";
910
let mut test = CompilerTest::rust_source_cargo_lib(
1011
PathBuf::from("../rust-apps-wasm/rust-sdk/account-test"),
12+
artifact_name,
1113
true,
1214
None,
1315
);
14-
let artifact_name = test.source.artifact_name();
1516
test.expect_wasm(expect_file![format!(
1617
"../../expected/rust_sdk_account_test/{artifact_name}.wat"
1718
)]);
@@ -85,7 +86,7 @@ fn basic_wallet() {
8586
)
8687
.build();
8788

88-
let mut test = CompilerTest::rust_source_cargo_lib(proj.root(), true, None);
89+
let mut test = CompilerTest::rust_source_cargo_lib(proj.root(), project_name, true, None);
8990
let artifact_name = test.source.artifact_name();
9091
test.expect_wasm(expect_file![format!("../../expected/{project_name}/{artifact_name}.wat")]);
9192
test.expect_ir(expect_file![format!("../../expected/{project_name}/{artifact_name}.hir")]);

0 commit comments

Comments
 (0)