diff --git a/examples/hello-world/tests/lib.rs b/examples/hello-world/tests/lib.rs index 50a5674058..31bc36a063 100644 --- a/examples/hello-world/tests/lib.rs +++ b/examples/hello-world/tests/lib.rs @@ -52,7 +52,8 @@ fn test_hello() { fn test_hello_with_test_environment() -> Result<(), RuntimeError> { // Arrange let mut env = TestEnvironment::new(); - let package_address = PackageFactory::compile_and_publish(this_package!(), &mut env)?; + let package_address = + PackageFactory::compile_and_publish(this_package!(), &mut env, CompileProfile::Fast)?; let mut hello = Hello::instantiate_hello(package_address, &mut env)?; diff --git a/radix-clis/assets/template/tests/lib.rs b/radix-clis/assets/template/tests/lib.rs index 3775d690df..5bdc32bcd9 100644 --- a/radix-clis/assets/template/tests/lib.rs +++ b/radix-clis/assets/template/tests/lib.rs @@ -52,7 +52,8 @@ fn test_hello() { fn test_hello_with_test_environment() -> Result<(), RuntimeError> { // Arrange let mut env = TestEnvironment::new(); - let package_address = PackageFactory::compile_and_publish(this_package!(), &mut env)?; + let package_address = + PackageFactory::compile_and_publish(this_package!(), &mut env, CompileProfile::Fast)?; let mut hello = Hello::instantiate_hello(package_address, &mut env)?; diff --git a/radix-clis/src/utils/cargo.rs b/radix-clis/src/utils/cargo.rs index ce207a8359..75e420f2bb 100644 --- a/radix-clis/src/utils/cargo.rs +++ b/radix-clis/src/utils/cargo.rs @@ -64,6 +64,10 @@ pub fn build_package>( } if coverage { compiler_builder.coverage(); + + let mut target_path = PathBuf::from(base_path.as_ref()); + target_path.push("coverage"); + compiler_builder.target_directory(target_path); } env.iter().for_each(|(name, value)| { compiler_builder.env(name, EnvironmentVariableAction::Set(value.clone())); diff --git a/radix-engine-tests/src/common.rs b/radix-engine-tests/src/common.rs index 91f76c5fd0..8c41534768 100644 --- a/radix-engine-tests/src/common.rs +++ b/radix-engine-tests/src/common.rs @@ -38,6 +38,7 @@ pub mod package_loader { pub mod package_loader { use radix_common::prelude::*; use radix_substate_store_queries::typed_substate_layout::*; + use scrypto_test::ledger_simulator::CompileProfile; use std::path::PathBuf; pub struct PackageLoader; @@ -45,7 +46,7 @@ pub mod package_loader { pub fn get(name: &str) -> (Vec, PackageDefinition) { let manifest_dir = PathBuf::from_str(env!("CARGO_MANIFEST_DIR")).unwrap(); let package_dir = manifest_dir.join("assets").join("blueprints").join(name); - scrypto_test::prelude::Compile::compile(package_dir) + scrypto_test::prelude::Compile::compile(package_dir, CompileProfile::FastWithTraceLogs) } } } diff --git a/radix-engine-tests/tests/flash/crypto_utils.rs b/radix-engine-tests/tests/flash/crypto_utils.rs index 9f64adfd85..9cc8505b5a 100644 --- a/radix-engine-tests/tests/flash/crypto_utils.rs +++ b/radix-engine-tests/tests/flash/crypto_utils.rs @@ -89,8 +89,11 @@ fn run_flash_test_test_environment(enable_bls: bool, expect_success: bool) { .build(); // Act - let result = - PackageFactory::compile_and_publish(path_local_blueprint!("crypto_scrypto"), &mut test_env); + let result = PackageFactory::compile_and_publish( + path_local_blueprint!("crypto_scrypto"), + &mut test_env, + CompileProfile::Fast, + ); // Assert if expect_success { diff --git a/radix-engine-tests/tests/vm/system_wasm_buffers.rs b/radix-engine-tests/tests/vm/system_wasm_buffers.rs index 1c7777a7ad..3ca0978b05 100644 --- a/radix-engine-tests/tests/vm/system_wasm_buffers.rs +++ b/radix-engine-tests/tests/vm/system_wasm_buffers.rs @@ -14,7 +14,10 @@ fn get_ledger() -> ( LedgerSimulator, ComponentAddress, ) { - let (code, definition) = Compile::compile(path_local_blueprint!("system_wasm_buffers")); + let (code, definition) = Compile::compile( + path_local_blueprint!("system_wasm_buffers"), + CompileProfile::FastWithTraceLogs, + ); // Arrange let mut ledger = LedgerSimulatorBuilder::new().build(); diff --git a/scrypto-compiler/src/lib.rs b/scrypto-compiler/src/lib.rs index 7a4c752cf4..90493f832d 100644 --- a/scrypto-compiler/src/lib.rs +++ b/scrypto-compiler/src/lib.rs @@ -11,6 +11,7 @@ use std::{env, io}; const MANIFEST_FILE: &str = "Cargo.toml"; const BUILD_TARGET: &str = "wasm32-unknown-unknown"; const SCRYPTO_NO_SCHEMA: &str = "scrypto/no-schema"; +const SCRYPTO_COVERAGE: &str = "scrypto/coverage"; #[derive(Debug)] pub enum ScryptoCompilerError { @@ -201,15 +202,15 @@ pub struct BuildArtifact { } #[derive(Debug, Clone)] -struct CompilerManifestDefinition { +pub struct CompilerManifestDefinition { /// Path to Cargo.toml file. - manifest_path: PathBuf, + pub manifest_path: PathBuf, /// Path to directory where compilation artifacts are stored. - target_directory: PathBuf, + pub target_directory: PathBuf, /// Path to target binary WASM file. - target_binary_wasm_path: PathBuf, + pub target_binary_wasm_path: PathBuf, /// Path to target binary RPD file. - target_binary_rpd_path: PathBuf, + pub target_binary_rpd_path: PathBuf, } /// Programmatic implementation of Scrypto compiler which is a wrapper around rust cargo tool. @@ -540,6 +541,19 @@ impl ScryptoCompiler { } else if !for_package_extract { features.push(["--features", SCRYPTO_NO_SCHEMA]); } + + let mut remove_cargo_rustflags_env = false; + if for_package_extract { + if let Some(idx) = features + .iter() + .position(|[_tag, value]| *value == SCRYPTO_COVERAGE) + { + // for schema extract 'scrypto/coverage' flag must be removed + features.remove(idx); + remove_cargo_rustflags_env = true; + } + } + let features: Vec<&str> = features.into_iter().flatten().collect(); let package: Vec<&str> = self @@ -579,8 +593,15 @@ impl ScryptoCompiler { .iter() .for_each(|(name, action)| { match action { - EnvironmentVariableAction::Set(value) => command.env(name, value), - EnvironmentVariableAction::Unset => command.env_remove(name), + EnvironmentVariableAction::Set(value) => { + // CARGO_ENCODED_RUSTFLAGS for coverage build must be removed for 1st phase compilation + if !(remove_cargo_rustflags_env && name == "CARGO_ENCODED_RUSTFLAGS") { + command.env(name, value); + } + } + EnvironmentVariableAction::Unset => { + command.env_remove(name); + } }; }); @@ -771,6 +792,11 @@ impl ScryptoCompiler { .then_some(()) .ok_or(ScryptoCompilerError::CargoBuildFailure(status)) } + + /// Returns information about the main manifest + pub fn get_main_manifest_definition(&self) -> CompilerManifestDefinition { + self.main_manifest.clone() + } } #[derive(Default)] @@ -868,7 +894,7 @@ impl ScryptoCompilerBuilder { pub fn coverage(&mut self) -> &mut Self { self.input_params .features - .insert(String::from("scrypto/coverage")); + .insert(String::from(SCRYPTO_COVERAGE)); self } @@ -917,7 +943,23 @@ mod tests { .map(|arg| arg.to_str().unwrap()) .collect::>() .join(" "); - let mut ret = cmd.get_program().to_str().unwrap().to_string(); + let envs = cmd + .get_envs() + .into_iter() + .map(|(name, value)| { + if let Some(value) = value { + format!("{}={}", name.to_str().unwrap(), value.to_str().unwrap()) + } else { + format!("{}", name.to_str().unwrap()) + } + }) + .collect::>() + .join(" "); + let mut ret = envs; + if !ret.is_empty() { + ret.push(' '); + } + ret.push_str(cmd.get_program().to_str().unwrap()); ret.push(' '); ret.push_str(&args); ret @@ -1213,4 +1255,73 @@ mod tests { assert_eq!(cmd_to_string(&cmd_phase_2), format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/no-schema --profile release", default_target_path.display(), manifest_path.display())); } + + #[test] + fn test_command_coverage() { + // Arrange + let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let mut target_path = manifest_path.clone(); + manifest_path.push("Cargo.toml"); + target_path.pop(); // ScryptoCompiler dir + target_path.push("coverage"); + let mut cmd_phase_1 = Command::new("cargo"); + let mut cmd_phase_2 = Command::new("cargo"); + + // Act + ScryptoCompiler::builder() + .coverage() + .target_directory(target_path.clone()) + .build() + .unwrap() + .prepare_command_phase_1(&mut cmd_phase_1); + ScryptoCompiler::builder() + .coverage() + .target_directory(target_path.clone()) + .build() + .unwrap() + .prepare_command_phase_2(&mut cmd_phase_2); + + // Assert + assert_eq!(cmd_to_string(&cmd_phase_1), + format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", target_path.display(), manifest_path.display())); + assert_eq!(cmd_to_string(&cmd_phase_2), + format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/coverage --features scrypto/no-schema --profile release", target_path.display(), manifest_path.display())); + } + + #[test] + fn test_command_coverage_with_env() { + // Arrange + let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let mut target_path = manifest_path.clone(); + manifest_path.push("Cargo.toml"); + target_path.pop(); // ScryptoCompiler dir + target_path.push("coverage"); + let action = EnvironmentVariableAction::Set(String::from( + "-Clto=off\x1f-Cinstrument-coverage\x1f-Zno-profiler-runtime\x1f--emit=llvm-ir", + )); + let mut cmd_phase_1 = Command::new("cargo"); + let mut cmd_phase_2 = Command::new("cargo"); + + // Act + ScryptoCompiler::builder() + .coverage() + .target_directory(target_path.clone()) + .env("CARGO_ENCODED_RUSTFLAGS", action.clone()) // CARGO_ENCODED_RUSTFLAGS must be removed for 1st phase + .build() + .unwrap() + .prepare_command_phase_1(&mut cmd_phase_1); + ScryptoCompiler::builder() + .coverage() + .target_directory(target_path.clone()) + .env("CARGO_ENCODED_RUSTFLAGS", action.clone()) + .build() + .unwrap() + .prepare_command_phase_2(&mut cmd_phase_2); + + // Assert + assert_eq!(cmd_to_string(&cmd_phase_1), + format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", target_path.display(), manifest_path.display())); + assert_eq!(cmd_to_string(&cmd_phase_2), + format!("CARGO_ENCODED_RUSTFLAGS=-Clto=off\x1f-Cinstrument-coverage\x1f-Zno-profiler-runtime\x1f--emit=llvm-ir cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/coverage --features scrypto/no-schema --profile release", target_path.display(), manifest_path.display())); + } } diff --git a/scrypto-test/Cargo.toml b/scrypto-test/Cargo.toml index 3894f7ec84..a444742664 100644 --- a/scrypto-test/Cargo.toml +++ b/scrypto-test/Cargo.toml @@ -41,6 +41,8 @@ resource_tracker = ["radix-engine/resource_tracker", "radix-common/resource_trac rocksdb = ["radix-substate-store-impls/rocksdb"] post_run_db_check = [] +coverage = ["radix-common/coverage", "radix-engine/coverage"] + [lib] doctest = false bench = false \ No newline at end of file diff --git a/scrypto-test/src/ledger_simulator/compile.rs b/scrypto-test/src/ledger_simulator/compile.rs index 8e65be1151..383c41fc1e 100644 --- a/scrypto-test/src/ledger_simulator/compile.rs +++ b/scrypto-test/src/ledger_simulator/compile.rs @@ -2,16 +2,33 @@ use crate::prelude::*; use scrypto_compiler::*; use std::path::Path; +pub enum CompileProfile { + /// Uses default compilation options - same as `scrypto build`. Should be used in all cases which requires + /// compilation results to be as close to production as possible (for instance costing related tests). + Standard, + /// Same as Standard with enabled all logs from error to trace level. + StandardWithTraceLogs, + /// Disables WASM optimization to speed-up compilation process, by default used by SDK PackageFactory. + Fast, + /// Disables WASM optimization and enables all logs from error to trace level, by default used by Ledger Simulator. + FastWithTraceLogs, +} + pub struct Compile; impl Compile { - pub fn compile>(package_dir: P) -> (Vec, PackageDefinition) { + pub fn compile>( + package_dir: P, + compile_profile: CompileProfile, + ) -> (Vec, PackageDefinition) { Self::compile_with_env_vars( package_dir, btreemap! { "RUSTFLAGS".to_owned() => "".to_owned(), "CARGO_ENCODED_RUSTFLAGS".to_owned() => "".to_owned(), }, + compile_profile, + true, ) } @@ -19,21 +36,38 @@ impl Compile { pub fn compile_with_env_vars>( package_dir: P, env_vars: sbor::rust::collections::BTreeMap, + compile_profile: CompileProfile, + _use_coverage: bool, ) -> (Vec, PackageDefinition) { // Initialize compiler let mut compiler_builder = ScryptoCompiler::builder(); - compiler_builder - .manifest_path(package_dir.as_ref()) - .optimize_with_wasm_opt(None) - .log_level(Level::Trace); // all logs from error to trace + compiler_builder.manifest_path(package_dir.as_ref()); + + match compile_profile { + CompileProfile::Standard => (), + CompileProfile::StandardWithTraceLogs => { + compiler_builder.log_level(Level::Trace); // all logs from error to trace + } + CompileProfile::Fast => { + compiler_builder.optimize_with_wasm_opt(None); + } + CompileProfile::FastWithTraceLogs => { + compiler_builder.optimize_with_wasm_opt(None); + compiler_builder.log_level(Level::Trace); // all logs from error to trace + } + } env_vars.iter().for_each(|(name, value)| { compiler_builder.env(name, EnvironmentVariableAction::Set(value.clone())); }); #[cfg(feature = "coverage")] - { + if _use_coverage { compiler_builder.coverage(); + + let mut coverage_dir = std::path::PathBuf::from(package_dir.as_ref()); + coverage_dir.push("coverage"); + compiler_builder.target_directory(coverage_dir); } let mut compiler = compiler_builder @@ -42,31 +76,32 @@ impl Compile { #[cfg(feature = "coverage")] // Check if binary exists in coverage directory, if it doesn't only then build it - { - let mut coverage_path = compiler.target_binary_path(); - if coverage_path.is_file() { - let code = fs::read(&coverage_path).unwrap_or_else(|err| { + if _use_coverage { + let manifest = compiler.get_main_manifest_definition(); + if manifest.target_binary_rpd_path.exists() && manifest.target_binary_wasm_path.exists() + { + let code = std::fs::read(&manifest.target_binary_wasm_path).unwrap_or_else(|err| { panic!( "Failed to read built WASM from path {:?} - {:?}", - &path, err - ) - }); - coverage_path.set_extension("rpd"); - let definition = fs::read(&coverage_path).unwrap_or_else(|err| { - panic!( - "Failed to read package definition from path {:?} - {:?}", - &coverage_path, err + &manifest.target_binary_wasm_path, err ) }); + let definition = + std::fs::read(&manifest.target_binary_rpd_path).unwrap_or_else(|err| { + panic!( + "Failed to read package definition from path {:?} - {:?}", + &manifest.target_binary_rpd_path, err + ) + }); let definition = manifest_decode(&definition).unwrap_or_else(|err| { panic!( "Failed to parse package definition from path {:?} - {:?}", - &coverage_path, err + &manifest.target_binary_rpd_path, err ) }); return (code, definition); } - }; + } // Build let mut build_artifacts = compiler.compile().unwrap_or_else(|error| { @@ -95,3 +130,192 @@ impl Compile { } } } + +#[cfg(test)] +mod tests { + use super::{Compile, CompileProfile}; + use std::process::Command; + + fn compile_blueprint(additional_args: &[&str]) -> Vec { + // Build `scrypto` cli + Command::new("cargo") + .arg("build") + .arg("--release") + .arg("--bin") + .arg("scrypto") + .current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/../radix-clis")) + .output() + .inspect_err(|e| println!("Scrypto cli build failed: {}", e)) + .unwrap(); + + // Run `scrypto build` for example blueprit + Command::new(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../radix-clis/target/release/scrypto" + )) + .arg("build") + .args(additional_args) + .current_dir(concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/blueprints/tuple-return" + )) + .output() + .inspect_err(|e| println!("Blueprint compilation falied: {}", e)) + .unwrap(); + + let output_file = concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/blueprints/target/wasm32-unknown-unknown/release/tuple_return.wasm" + ); + std::fs::read(output_file) + .inspect_err(|e| println!("Failed to load file: {}, error: {}", output_file, e)) + .unwrap() + } + + #[test] + fn validate_compile_profile_standard() { + // Compile blueprint using `scrypto compile` command + let output_file_content = compile_blueprint(&[]); + + // Compile same blueprint using Compile object + let (bin, _) = Compile::compile( + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"), + CompileProfile::Standard, + ); + + // Assert + assert_eq!( + output_file_content.len(), + bin.len(), + "Wasm files should have same size." + ); + assert_eq!( + output_file_content, bin, + "Wasm files should have same content." + ) + } + + #[test] + fn validate_compile_profile_standard_with_logs() { + // Compile blueprint using `scrypto compile` command + let output_file_content = compile_blueprint(&[]); + + // Compile same blueprint using Compile object + let (bin, _) = Compile::compile( + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"), + CompileProfile::StandardWithTraceLogs, + ); + + // Assert + assert!( + output_file_content.len() < bin.len(), + "Size of Wasm file compiled by `scrypto build` command should be smaller." + ); + } + + #[test] + fn validate_compile_profile_fast() { + // Compile blueprint using `scrypto compile` command + let output_file_content = compile_blueprint(&[]); + + // Compile same blueprint using Compile object + let (bin, _) = Compile::compile( + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"), + CompileProfile::Fast, + ); + + // Assert + assert!( + output_file_content.len() < bin.len(), + "Size of Wasm file compiled by `scrypto build` command should be smaller." + ); + } + + #[test] + fn validate_compile_profile_fast_with_logs() { + // Compile blueprint using `scrypto compile` command + let output_file_content = compile_blueprint(&[]); + + // Compile same blueprint using Compile object + let (bin, _) = Compile::compile( + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"), + CompileProfile::FastWithTraceLogs, + ); + + // Assert + assert!( + output_file_content.len() < bin.len(), + "Size of Wasm file compiled by `scrypto build` command should be smaller." + ); + } + + #[test] + fn verify_scrypto_build_with_args_for_compile_profile_standard_with_logs() { + // Compile blueprint using `scrypto compile` command + let output_file_content = compile_blueprint(&["--log-level", "TRACE"]); + + // Compile same blueprint using Compile object + let (bin, _) = Compile::compile( + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"), + CompileProfile::StandardWithTraceLogs, + ); + + // Assert + assert_eq!( + output_file_content.len(), + bin.len(), + "Wasm files should have same size." + ); + assert_eq!( + output_file_content, bin, + "Wasm files should have same content." + ) + } + + #[test] + fn verify_scrypto_build_with_args_for_compile_profile_fast() { + // Compile blueprint using `scrypto compile` command + let output_file_content = compile_blueprint(&["--disable-wasm-opt"]); + + // Compile same blueprint using Compile object + let (bin, _) = Compile::compile( + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"), + CompileProfile::Fast, + ); + + // Assert + assert_eq!( + output_file_content.len(), + bin.len(), + "Wasm files should have same size." + ); + assert_eq!( + output_file_content, bin, + "Wasm files should have same content." + ) + } + + #[test] + fn verify_scrypto_build_with_args_for_compile_profile_fast_with_logs() { + // Compile blueprint using `scrypto compile` command + let output_file_content = + compile_blueprint(&["--disable-wasm-opt", "--log-level", "TRACE"]); + + // Compile same blueprint using Compile object + let (bin, _) = Compile::compile( + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"), + CompileProfile::FastWithTraceLogs, + ); + + // Assert + assert_eq!( + output_file_content.len(), + bin.len(), + "Wasm files should have same size." + ); + assert_eq!( + output_file_content, bin, + "Wasm files should have same content." + ) + } +} diff --git a/scrypto-test/src/ledger_simulator/ledger_simulator.rs b/scrypto-test/src/ledger_simulator/ledger_simulator.rs index bea9822391..0070a6e961 100644 --- a/scrypto-test/src/ledger_simulator/ledger_simulator.rs +++ b/scrypto-test/src/ledger_simulator/ledger_simulator.rs @@ -1238,7 +1238,15 @@ impl LedgerSimulator { } pub fn compile>(&mut self, package_dir: P) -> (Vec, PackageDefinition) { - Compile::compile(package_dir) + self.compile_with_option(package_dir, CompileProfile::FastWithTraceLogs) + } + + pub fn compile_with_option>( + &mut self, + package_dir: P, + compile_profile: CompileProfile, + ) -> (Vec, PackageDefinition) { + Compile::compile(package_dir, compile_profile) } // Doesn't need to be here - kept for backward compatibility @@ -2577,31 +2585,31 @@ pub fn assert_receipt_events_can_be_typed(commit_result: &CommitResult) { } pub enum PackagePublishingSource { - CompileAndPublishFromSource(PathBuf), + CompileAndPublishFromSource(PathBuf, CompileProfile), PublishExisting(Vec, PackageDefinition), } impl From for PackagePublishingSource { fn from(value: String) -> Self { - Self::CompileAndPublishFromSource(value.into()) + Self::CompileAndPublishFromSource(value.into(), CompileProfile::FastWithTraceLogs) } } impl<'g> From<&'g str> for PackagePublishingSource { fn from(value: &'g str) -> Self { - Self::CompileAndPublishFromSource(value.into()) + Self::CompileAndPublishFromSource(value.into(), CompileProfile::FastWithTraceLogs) } } impl From for PackagePublishingSource { fn from(value: PathBuf) -> Self { - Self::CompileAndPublishFromSource(value) + Self::CompileAndPublishFromSource(value, CompileProfile::FastWithTraceLogs) } } impl<'g> From<&'g Path> for PackagePublishingSource { fn from(value: &'g Path) -> Self { - Self::CompileAndPublishFromSource(value.into()) + Self::CompileAndPublishFromSource(value.into(), CompileProfile::FastWithTraceLogs) } } @@ -2614,7 +2622,7 @@ impl From<(Vec, PackageDefinition)> for PackagePublishingSource { impl PackagePublishingSource { pub fn code_and_definition(self) -> (Vec, PackageDefinition) { match self { - Self::CompileAndPublishFromSource(path) => Compile::compile(path), + Self::CompileAndPublishFromSource(path, profile) => Compile::compile(path, profile), Self::PublishExisting(code, definition) => (code, definition), } } diff --git a/scrypto-test/src/sdk/package.rs b/scrypto-test/src/sdk/package.rs index 963237a7c9..549eb447aa 100644 --- a/scrypto-test/src/sdk/package.rs +++ b/scrypto-test/src/sdk/package.rs @@ -1,7 +1,6 @@ use std::path::Path; use crate::prelude::*; -use scrypto_compiler::*; pub struct PackageFactory; @@ -59,12 +58,13 @@ impl PackageFactory { pub fn compile_and_publish( path: P, env: &mut TestEnvironment, + compile_profile: CompileProfile, ) -> Result where P: AsRef, D: SubstateDatabase + CommittableSubstateDatabase + 'static, { - let (wasm, package_definition) = Self::compile(path); + let (wasm, package_definition) = Self::compile(path, compile_profile); Self::publish_advanced( OwnerRole::None, package_definition, @@ -75,34 +75,10 @@ impl PackageFactory { ) } - pub fn compile

(path: P) -> (Vec, PackageDefinition) + pub fn compile

(path: P, compile_profile: CompileProfile) -> (Vec, PackageDefinition) where P: AsRef, { - // Initialize compiler - let mut compiler = ScryptoCompiler::builder() - .manifest_path(path.as_ref()) - .optimize_with_wasm_opt(None) - .build() - .unwrap_or_else(|err| panic!("Failed to initialize Scrypto Compiler {:?}", err)); - - // Build - let mut build_artifacts = compiler.compile().unwrap_or_else(|err| { - panic!( - "Failed to compile package: {:?}, error: {:?}", - path.as_ref(), - err - ) - }); - - if !build_artifacts.is_empty() { - let build_artifact = build_artifacts.remove(0); // take first element - ( - build_artifact.wasm.content, - build_artifact.package_definition.content, - ) - } else { - panic!("Build artifacts list is empty: {:?}", path.as_ref(),); - } + Compile::compile_with_env_vars(path, BTreeMap::new(), compile_profile, false) } } diff --git a/scrypto-test/tests/tuple_return.rs b/scrypto-test/tests/tuple_return.rs index 03b8516079..e4b426bfce 100644 --- a/scrypto-test/tests/tuple_return.rs +++ b/scrypto-test/tests/tuple_return.rs @@ -8,6 +8,7 @@ fn tuple_returns_work_with_scrypto_test() { let package_address = PackageFactory::compile_and_publish( concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"), &mut env, + CompileProfile::Fast, ) .unwrap();