Skip to content

Commit

Permalink
internal: tweak --build-only (#219)
Browse files Browse the repository at this point in the history
* internal: tweak --build-only

* add test case to ensure --build-only omit artifacts
  • Loading branch information
Young-Flash authored Aug 30, 2024
1 parent 3d2ec03 commit 16a7474
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 17 deletions.
31 changes: 19 additions & 12 deletions crates/moon/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use moonutil::common::FileLock;
use moonutil::common::RunMode;
use moonutil::common::SurfaceTarget;
use moonutil::common::TargetBackend;
use moonutil::common::TestArtifacts;
use moonutil::common::MOONBITLANG_CORE;
use moonutil::common::MOON_PKG_JSON;
use moonutil::common::{MoonbuildOpt, OutputFormat};
Expand All @@ -34,7 +35,6 @@ use moonutil::dirs::PackageDirs;
use moonutil::mooncakes::sync::AutoSyncFlags;
use moonutil::mooncakes::RegistryConfig;
use n2::trace;
use std::path::PathBuf;

use super::{BuildFlags, UniversalFlags};

Expand Down Expand Up @@ -102,10 +102,9 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu
.join(format!("{}.core", file_name))
.display()
.to_string());
let output_wasm_or_js_path = &(output_artifact_path
.join(format!("{}.{}", file_name, target_backend.to_extension()))
.display()
.to_string());

let output_wasm_or_js_path =
output_artifact_path.join(format!("{}.{}", file_name, target_backend.to_extension()));

let pkg_name = "moon/run/single";
let build_package_command = [
Expand Down Expand Up @@ -133,7 +132,7 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu
.display()
.to_string()),
"-o",
output_wasm_or_js_path,
&output_wasm_or_js_path.display().to_string(),
"-pkg-sources",
&format!("{}:{}", pkg_name, mbt_file_parent_path.display()),
"-pkg-sources",
Expand All @@ -156,7 +155,7 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu
TargetBackend::Wasm | TargetBackend::WasmGC => "moonrun",
TargetBackend::Js => "node",
};
println!("{runner} {output_wasm_or_js_path}");
println!("{} {}", runner, output_wasm_or_js_path.display());
}
return Ok(0);
}
Expand Down Expand Up @@ -184,16 +183,18 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu
}

if cmd.build_only {
let test_artifacts = TestArtifacts {
artifacts_path: vec![output_wasm_or_js_path],
};
println!("{}", serde_json_lenient::to_string(&test_artifacts)?);
return Ok(0);
}

trace::scope("run", || match target_backend {
TargetBackend::Wasm | TargetBackend::WasmGC => {
moonbuild::build::run_wat(&PathBuf::from(output_wasm_or_js_path), &cmd.args)
}
TargetBackend::Js => {
moonbuild::build::run_js(&PathBuf::from(output_wasm_or_js_path), &cmd.args)
moonbuild::build::run_wat(&output_wasm_or_js_path, &cmd.args)
}
TargetBackend::Js => moonbuild::build::run_js(&output_wasm_or_js_path, &cmd.args),
})?;

Ok(0)
Expand Down Expand Up @@ -292,7 +293,13 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res
trace::open("trace.json").context("failed to open `trace.json`")?;
}

let result = entry::run_run(&package_path, &moonc_opt, &moonbuild_opt, &module);
let result = entry::run_run(
&package_path,
&moonc_opt,
&moonbuild_opt,
&module,
cmd.build_only,
);
if trace_flag {
trace::close();
}
Expand Down
6 changes: 6 additions & 0 deletions crates/moon/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ fn do_run_test(
auto_update,
module,
)?;

// don't print test summary if build_only
if build_only {
return Ok(0);
}

let total = test_res.len();
let passed = test_res.iter().filter(|r| r.is_ok()).count();

Expand Down
37 changes: 37 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ fn test_design() {
main2
"#]],
);

get_stdout_with_args(&dir, ["clean"]);
check(
&get_stdout_with_args_and_replace_dir(
&dir,
["run", "main2", "--target", "js", "--build-only"],
),
expect![[r#"
{"artifacts_path":["$ROOT/target/js/release/build/main2/main2.js"]}
"#]],
);
assert!(dir.join("target/js/release/build/main2/main2.js").exists());
}

#[test]
Expand Down Expand Up @@ -5213,6 +5225,18 @@ fn test_moon_run_single_mbt_file() {
"#]],
);

let output = get_stdout_with_args_and_replace_dir(
&dir,
["run", "a/b/single.mbt", "--target", "js", "--build-only"],
);
check(
&output,
expect![[r#"
{"artifacts_path":["$ROOT/a/b/target/single.js"]}
"#]],
);
assert!(dir.join("a/b/target/single.js").exists());

let output = get_stdout_with_args_and_replace_dir(&dir, ["run", "a/b/single.mbt", "--dry-run"]);
check(
&output,
Expand Down Expand Up @@ -5572,6 +5596,19 @@ fn test_specify_source_dir_004() {
Finished. moon: ran 3 tasks, now up to date
"#]],
);

get_stdout_with_args(&dir, ["clean"]);
check(
&get_stdout_with_args_and_replace_dir(
&dir,
["run", "nes/t/ed/src/main", "--target", "js", "--build-only"],
),
expect![[r#"
{"artifacts_path":["$ROOT/target/js/release/build/main/main.js"]}
"#]],
);
assert!(dir.join("target/js/release/build/main/main.js").exists());

check(
&get_stdout_with_args_and_replace_dir(&dir, ["run", "nes/t/ed/src/main"]),
expect![[r#"
Expand Down
29 changes: 24 additions & 5 deletions crates/moonbuild/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::check::normal::write_pkg_lst;
use crate::expect::{apply_snapshot, render_snapshot_fail};
use crate::runtest::TestStatistics;

use moonutil::common::{MoonbuildOpt, MooncOpt, TargetBackend};
use moonutil::common::{MoonbuildOpt, MooncOpt, TargetBackend, TestArtifacts};

use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -196,6 +196,7 @@ pub fn run_run(
moonc_opt: &MooncOpt,
moonbuild_opt: &MoonbuildOpt,
module: &ModuleDB,
build_only: bool,
) -> anyhow::Result<i32> {
run_build(moonc_opt, moonbuild_opt, module)?;
let (source_dir, target_dir) = (&moonbuild_opt.source_dir, &moonbuild_opt.target_dir);
Expand Down Expand Up @@ -242,6 +243,15 @@ pub fn run_run(
));
let wat_path = dunce::canonicalize(&wat_path)
.context(format!("cannot find wat file at `{:?}`", &wat_path))?;

if build_only {
let test_artifacts = TestArtifacts {
artifacts_path: vec![wat_path],
};
println!("{}", serde_json_lenient::to_string(&test_artifacts)?);
return Ok(0);
}

trace::scope("run", || {
if moonc_opt.link_opt.target_backend == TargetBackend::Wasm
|| moonc_opt.link_opt.target_backend == TargetBackend::WasmGC
Expand Down Expand Up @@ -315,10 +325,6 @@ pub fn run_test(
let result = n2_run_interface(state, moonbuild_opt)?;
render_result(result, moonbuild_opt.quiet, "testing")?;

if build_only {
return Ok(vec![]);
}

let runtime = tokio::runtime::Builder::new_multi_thread()
// todo: add config item
.worker_threads(16)
Expand All @@ -333,6 +339,9 @@ pub fn run_test(
let filter_index = test_opt.as_ref().and_then(|it| it.filter_index);

let printed = Arc::new(AtomicBool::new(false));
let mut test_artifacts = TestArtifacts {
artifacts_path: vec![],
};
for (pkgname, _) in module
.packages
.iter()
Expand Down Expand Up @@ -399,6 +408,11 @@ pub fn run_test(
);

std::fs::write(&wrapper_js_driver_path, js_driver)?;
test_artifacts
.artifacts_path
.push(wrapper_js_driver_path.clone());
} else {
test_artifacts.artifacts_path.push(artifact_path.clone());
}

let printed = Arc::clone(&printed);
Expand Down Expand Up @@ -443,6 +457,11 @@ pub fn run_test(
}
}

if build_only {
println!("{}", serde_json_lenient::to_string(&test_artifacts)?);
return Ok(vec![]);
}

let res = if moonbuild_opt.no_parallelize {
runtime.block_on(async {
let mut results = vec![];
Expand Down
5 changes: 5 additions & 0 deletions crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ impl TestOpt {
}
}

#[derive(serde::Serialize, Clone)]
pub struct TestArtifacts {
pub artifacts_path: Vec<PathBuf>,
}

#[derive(Debug, Clone, Default)]
pub struct FmtOpt {
pub check: bool,
Expand Down

0 comments on commit 16a7474

Please sign in to comment.