#[cfg(test)]
mod tests {
use radix_common::prelude::*;
use radix_engine_interface::types::Level;
use scrypto_compiler::*;
use std::{env, path::PathBuf, process::Stdio};
use tempdir::TempDir;
fn prepare() -> (PathBuf, TempDir) {
let mut test_assets_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_assets_path.extend(["tests", "assets", "scenario_1", "blueprint", "Cargo.toml"]);
(
test_assets_path,
TempDir::new("scrypto-compiler-test").unwrap(),
)
}
#[test]
fn test_compilation() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.compile();
assert!(status.is_ok(), "{:?}", status);
let build_artifacts = status.unwrap();
assert_eq!(build_artifacts.len(), 1);
assert!(build_artifacts[0].wasm.path.exists());
assert!(build_artifacts[0].package_definition.path.exists());
assert!(
std::fs::metadata(&build_artifacts[0].wasm.path)
.unwrap()
.len()
> 0,
"Wasm file should not be empty."
);
assert!(
std::fs::metadata(&build_artifacts[0].package_definition.path)
.unwrap()
.len()
> 7,
"Package definition file should not be empty, so should be longer than 7 bytes."
);
let mut target_path = target_directory.path().to_path_buf();
target_path.extend(["wasm32-unknown-unknown", "release", "test_blueprint.wasm"]);
assert_eq!(build_artifacts[0].wasm.path, target_path);
assert_eq!(
build_artifacts[0].package_definition.path,
target_path.with_extension("rpd")
);
}
#[test]
fn test_compilation_in_current_dir() {
let (blueprint_manifest_path, target_directory) = prepare();
let mut package_directory = blueprint_manifest_path.clone();
package_directory.pop(); std::env::set_current_dir(package_directory).unwrap();
let status = ScryptoCompiler::builder()
.target_directory(target_directory.path())
.compile();
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_env_var() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.env("TEST", EnvironmentVariableAction::Set(String::from("1 1")))
.env("OTHER", EnvironmentVariableAction::Unset)
.env(
"RUSTFLAGS",
EnvironmentVariableAction::Set(String::from("-C opt-level=3")),
)
.compile();
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_with_feature() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.feature("feature-1")
.compile();
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_with_feature_and_loglevel() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.feature("feature-1")
.log_level(Level::Warn)
.compile();
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_fails_with_non_existing_feature() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.feature("feature-2")
.compile();
assert!(match status {
Err(ScryptoCompilerError::CargoBuildFailure(exit_status)) =>
exit_status.code().unwrap() == 101,
_ => false,
});
}
#[test]
fn test_compilation_workspace() {
let (blueprint_manifest_path, target_directory) = prepare();
let mut workspace_directory = blueprint_manifest_path.clone();
workspace_directory.pop(); workspace_directory.pop(); workspace_directory.push("Cargo.toml");
let status = ScryptoCompiler::builder()
.manifest_path(workspace_directory)
.target_directory(target_directory.path())
.compile();
assert!(status.is_ok(), "{:?}", status);
let build_artifacts = status.unwrap();
assert_eq!(build_artifacts.len(), 3);
let names = [
"test_blueprint.wasm",
"test_blueprint_2.wasm",
"test_blueprint_3.wasm",
];
for i in 0..names.len() {
assert!(build_artifacts[i].wasm.path.exists());
assert!(build_artifacts[i].package_definition.path.exists());
assert!(
std::fs::metadata(&build_artifacts[i].wasm.path)
.unwrap()
.len()
> 0,
"Wasm file should not be empty."
);
assert!(
std::fs::metadata(&build_artifacts[i].package_definition.path)
.unwrap()
.len()
> 7,
"Package definition file should not be empty, so should be longer than 7 bytes."
);
let mut target_path = target_directory.path().to_path_buf();
target_path.extend(["wasm32-unknown-unknown", "release", names[i]]);
assert_eq!(build_artifacts[i].wasm.path, target_path);
assert_eq!(
build_artifacts[i].package_definition.path,
target_path.with_extension("rpd")
);
}
assert!(!build_artifacts[0]
.wasm
.path
.with_file_name("test_blueprint_4.wasm")
.exists());
}
#[test]
fn test_compilation_workspace_in_current_dir() {
let (blueprint_manifest_path, target_directory) = prepare();
let mut workspace_directory = blueprint_manifest_path.clone();
workspace_directory.pop(); workspace_directory.pop(); std::env::set_current_dir(workspace_directory).unwrap();
let status = ScryptoCompiler::builder()
.target_directory(target_directory.path())
.compile();
assert!(status.is_ok(), "{:?}", status);
let build_artifacts = status.unwrap();
assert_eq!(build_artifacts.len(), 3);
}
#[test]
#[allow(clippy::needless_range_loop)]
fn test_compilation_workspace_with_package() {
let (blueprint_manifest_path, target_directory) = prepare();
let mut workspace_directory = blueprint_manifest_path.clone();
workspace_directory.pop(); workspace_directory.pop(); workspace_directory.push("Cargo.toml");
let status = ScryptoCompiler::builder()
.manifest_path(workspace_directory)
.target_directory(target_directory.path())
.package("test_blueprint_2")
.package("test_blueprint_3")
.package("test_blueprint_4") .compile();
assert!(status.is_ok(), "{:?}", status);
let build_artifacts = status.unwrap();
assert_eq!(build_artifacts.len(), 3);
let names = [
"test_blueprint_2.wasm",
"test_blueprint_3.wasm",
"test_blueprint_4.wasm",
];
for i in 0..names.len() {
assert!(build_artifacts[i].wasm.path.exists());
assert!(build_artifacts[i].package_definition.path.exists());
}
assert!(!build_artifacts[0]
.wasm
.path
.with_file_name("test_blueprint_1.wasm")
.exists());
}
#[test]
fn test_compilation_workspace_with_non_existing_package() {
let (blueprint_manifest_path, target_directory) = prepare();
let mut workspace_directory = blueprint_manifest_path.clone();
workspace_directory.pop(); workspace_directory.pop(); workspace_directory.push("Cargo.toml");
let status = ScryptoCompiler::builder()
.manifest_path(workspace_directory)
.target_directory(target_directory.path())
.package("test_blueprint_2")
.package("non_existing_package")
.package("test_blueprint_3")
.compile();
assert!(match status {
Err(ScryptoCompilerError::CargoWrongPackageId(package)) =>
package == "non_existing_package",
_ => false,
});
}
#[test]
fn test_compilation_workspace_without_scrypto_package() {
let mut blueprint_manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
blueprint_manifest_path.extend([
"tests",
"assets",
"scenario_2",
"some_project",
"Cargo.toml",
]);
let target_directory = TempDir::new("scrypto-compiler-test").unwrap();
let mut workspace_directory = blueprint_manifest_path.clone();
workspace_directory.pop(); workspace_directory.pop(); workspace_directory.push("Cargo.toml");
let status = ScryptoCompiler::builder()
.manifest_path(workspace_directory)
.target_directory(target_directory.path())
.compile();
assert_matches!(status, Err(ScryptoCompilerError::NothingToCompile));
}
#[test]
fn test_compilation_profile_release() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.profile(Profile::Release)
.compile();
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_profile_debug() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.profile(Profile::Debug)
.compile();
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_profile_test() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.profile(Profile::Test)
.compile();
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_profile_bench() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.profile(Profile::Bench)
.compile();
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_profile_custom() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.profile(Profile::Custom(String::from("custom")))
.compile();
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_without_wasm_optimisations() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.optimize_with_wasm_opt(None)
.compile();
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_with_stdio() {
let (blueprint_manifest_path, target_directory) = prepare();
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.target_directory(target_directory.path())
.compile_with_stdio(Some(Stdio::piped()), Some(Stdio::null()), None);
assert!(status.is_ok(), "{:?}", status);
}
#[test]
fn test_compilation_with_wasm_reference_types_disabled() {
let mut blueprint_manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
blueprint_manifest_path.extend(["tests", "assets", "call_indirect", "Cargo.toml"]);
let status = ScryptoCompiler::builder()
.manifest_path(blueprint_manifest_path)
.compile();
assert!(status.is_ok(), "{:?}", status);
}
}