diff --git a/crates/moon/src/cli/doc.rs b/crates/moon/src/cli/doc.rs index 925d9be8..cbde9d8c 100644 --- a/crates/moon/src/cli/doc.rs +++ b/crates/moon/src/cli/doc.rs @@ -17,6 +17,7 @@ // For inquiries, you can contact us via e-mail at jichuruanjian@idea.edu.cn. use anyhow::bail; +use moonbuild::dry_run::print_commands; use mooncake::pkg::sync::auto_sync; use moonutil::common::{ read_module_desc_file_in_dir, CargoPathExt, FileLock, MoonbuildOpt, MooncOpt, RunMode, @@ -64,16 +65,6 @@ pub fn run_doc(cli: UniversalFlags, cmd: DocSubcommand) -> anyhow::Result { let bind = cmd.bind; let port = cmd.port; - if cli.dry_run { - println!( - "moondoc {} -o {}{}", - source_dir.display(), - static_dir.display(), - if serve { " -serve-mode" } else { "" } - ); - return Ok(0); - } - let mod_desc = read_module_desc_file_in_dir(&source_dir)?; let mut moonc_opt = MooncOpt::default(); @@ -105,7 +96,6 @@ pub fn run_doc(cli: UniversalFlags, cmd: DocSubcommand) -> anyhow::Result { &moonc_opt, &moonbuild_opt, )?; - moonbuild::entry::run_check(&moonc_opt, &moonbuild_opt, &module)?; let mut args = vec![ source_dir.display().to_string(), @@ -115,10 +105,22 @@ pub fn run_doc(cli: UniversalFlags, cmd: DocSubcommand) -> anyhow::Result { moonutil::moon_dir::core_bundle(moonc_opt.link_opt.target_backend) .display() .to_string(), + "-packages-json".to_string(), + moonbuild_opt + .target_dir + .join("packages.json") + .display() + .to_string(), ]; if serve { args.push("-serve-mode".to_string()) } + if cli.dry_run { + print_commands(&module, &moonc_opt, &moonbuild_opt)?; + println!("moondoc {}", args.join(" ")); + return Ok(0); + } + moonbuild::entry::run_check(&moonc_opt, &moonbuild_opt, &module)?; let output = std::process::Command::new("moondoc").args(&args).output()?; if output.status.code().unwrap() != 0 { eprintln!("{}", String::from_utf8_lossy(&output.stderr)); diff --git a/crates/moon/tests/test_cases/mod.rs b/crates/moon/tests/test_cases/mod.rs index fe9aacca..a3f303fd 100644 --- a/crates/moon/tests/test_cases/mod.rs +++ b/crates/moon/tests/test_cases/mod.rs @@ -5986,3 +5986,70 @@ fn test_js() { "#]], ); } + +#[test] +fn test_moon_doc_dry_run() { + let dir = TestDir::new("moon_doc.in"); + check( + &get_stdout_with_args_and_replace_dir(&dir, ["doc", "--dry-run"]), + expect![[r#" + moonc check ./src/lib/hello.mbt -o ./target/wasm-gc/release/check/lib/lib.mi -pkg username/hello/lib -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources username/hello/lib:./src/lib -target wasm-gc + moonc check ./src/main/main.mbt -o ./target/wasm-gc/release/check/main/main.mi -pkg username/hello/main -is-main -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -i ./target/wasm-gc/release/check/lib/lib.mi:lib -pkg-sources username/hello/main:./src/main -target wasm-gc + moonc check ./src/lib/hello_test.mbt -o ./target/wasm-gc/release/check/lib/lib.blackbox_test.mi -pkg username/hello/lib_blackbox_test -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -i ./target/wasm-gc/release/check/lib/lib.mi:lib -pkg-sources username/hello/lib_blackbox_test:./src/lib -target wasm-gc + moondoc $ROOT -o $ROOT/target/doc -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -packages-json $ROOT/target/wasm-gc/release/check/packages.json + "#]], + ); +} + +#[test] +#[ignore = "wait for the latest release of moonc"] +fn test_moon_doc() { + let dir = TestDir::new("moon_doc.in"); + let _ = get_stderr_on_success_with_args_and_replace_dir(&dir, ["doc"]); + check( + &read(&dir.join("target/doc/username/hello/lib/members.md")), + expect![[r##" + # username/hello/lib + * * * + # Index + + |Value|Description| + |---|---| + |[hello](#hello)|| + + + * * * + # Value and Function + ## hello + ```moonbit + :::source,username/hello/lib/hello.mbt,1:::pub fn hello() -> String + ``` + + + + + "##]], + ); + check( + &read(&dir.join("target/doc/username/hello/main/members.md")), + expect![[r#" + # username/hello/main + * * * + # Index + "#]], + ); + check( + &read(&dir.join("target/doc/username/hello/_sidebar.md")), + expect![[r#" + - [Contents](username/hello/) + - Packages + - [username/hello/lib](username/hello/lib/members) + - [username/hello/main](username/hello/main/members) + + - Dependencies + - [moonbitlang/core](moonbitlang/core/) + + + "#]], + ); +} diff --git a/crates/moon/tests/test_cases/moon_doc.in/.gitignore b/crates/moon/tests/test_cases/moon_doc.in/.gitignore new file mode 100644 index 00000000..b1283a74 --- /dev/null +++ b/crates/moon/tests/test_cases/moon_doc.in/.gitignore @@ -0,0 +1,2 @@ +target/ +.mooncakes/ diff --git a/crates/moon/tests/test_cases/moon_doc.in/README.md b/crates/moon/tests/test_cases/moon_doc.in/README.md new file mode 100644 index 00000000..ae00983f --- /dev/null +++ b/crates/moon/tests/test_cases/moon_doc.in/README.md @@ -0,0 +1 @@ +# username/hello \ No newline at end of file diff --git a/crates/moon/tests/test_cases/moon_doc.in/moon.mod.json b/crates/moon/tests/test_cases/moon_doc.in/moon.mod.json new file mode 100644 index 00000000..9e55a905 --- /dev/null +++ b/crates/moon/tests/test_cases/moon_doc.in/moon.mod.json @@ -0,0 +1,10 @@ +{ + "name": "username/hello", + "version": "0.1.0", + "readme": "README.md", + "repository": "", + "license": "", + "keywords": [], + "description": "", + "source": "src" +} \ No newline at end of file diff --git a/crates/moon/tests/test_cases/moon_doc.in/src/lib/hello.mbt b/crates/moon/tests/test_cases/moon_doc.in/src/lib/hello.mbt new file mode 100644 index 00000000..9012592a --- /dev/null +++ b/crates/moon/tests/test_cases/moon_doc.in/src/lib/hello.mbt @@ -0,0 +1,3 @@ +pub fn hello() -> String { + "Hello, world!" +} diff --git a/crates/moon/tests/test_cases/moon_doc.in/src/lib/hello_test.mbt b/crates/moon/tests/test_cases/moon_doc.in/src/lib/hello_test.mbt new file mode 100644 index 00000000..e0e3a7d1 --- /dev/null +++ b/crates/moon/tests/test_cases/moon_doc.in/src/lib/hello_test.mbt @@ -0,0 +1,5 @@ +test "hello" { + if @lib.hello() != "Hello, world!" { + fail!("@lib.hello() != \"Hello, world!\"") + } +} diff --git a/crates/moon/tests/test_cases/moon_doc.in/src/lib/moon.pkg.json b/crates/moon/tests/test_cases/moon_doc.in/src/lib/moon.pkg.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/crates/moon/tests/test_cases/moon_doc.in/src/lib/moon.pkg.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/crates/moon/tests/test_cases/moon_doc.in/src/main/main.mbt b/crates/moon/tests/test_cases/moon_doc.in/src/main/main.mbt new file mode 100644 index 00000000..d25cb935 --- /dev/null +++ b/crates/moon/tests/test_cases/moon_doc.in/src/main/main.mbt @@ -0,0 +1,3 @@ +fn main { + println(@lib.hello()) +} diff --git a/crates/moon/tests/test_cases/moon_doc.in/src/main/moon.pkg.json b/crates/moon/tests/test_cases/moon_doc.in/src/main/moon.pkg.json new file mode 100644 index 00000000..48783525 --- /dev/null +++ b/crates/moon/tests/test_cases/moon_doc.in/src/main/moon.pkg.json @@ -0,0 +1,6 @@ +{ + "is-main": true, + "import": [ + "username/hello/lib" + ] +} \ No newline at end of file