Skip to content

Commit be69519

Browse files
committed
fix: add and modify tests for rustdoc json
1 parent b5b1bbf commit be69519

File tree

3 files changed

+192
-1
lines changed

3 files changed

+192
-1
lines changed

tests/testsuite/build_dir.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,44 @@ fn cargo_doc_should_output_to_target_dir() {
502502
assert_exists(&docs_dir.join("foo/index.html"));
503503
}
504504

505+
#[cargo_test(nightly, reason = "--output-format is unstable")]
506+
fn cargo_rustdoc_json_should_output_to_target_dir() {
507+
let p = project()
508+
.file("src/lib.rs", "")
509+
.file(
510+
".cargo/config.toml",
511+
r#"
512+
[build]
513+
target-dir = "target-dir"
514+
build-dir = "build-dir"
515+
"#,
516+
)
517+
.build();
518+
519+
p.cargo("-Zbuild-dir-new-layout rustdoc -Zfine-grain-locking -Zunstable-options --output-format json")
520+
.masquerade_as_nightly_cargo(&["new build-dir layout", "rustdoc-output-format"])
521+
.enable_mac_dsym()
522+
.run();
523+
524+
let docs_dir = p.root().join("target-dir/doc");
525+
526+
assert_exists(&docs_dir);
527+
assert_exists(&docs_dir.join("foo.json"));
528+
529+
p.root().join("build-dir").assert_build_dir_layout(str![r#"
530+
[ROOT]/foo/build-dir/.rustc_info.json
531+
[ROOT]/foo/build-dir/.rustdoc_fingerprint.json
532+
[ROOT]/foo/build-dir/CACHEDIR.TAG
533+
[ROOT]/foo/build-dir/debug/.cargo-build-lock
534+
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/.lock
535+
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/doc-lib-foo
536+
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/doc-lib-foo.json
537+
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/invoked.timestamp
538+
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/out/foo.json
539+
540+
"#]);
541+
}
542+
505543
#[cargo_test]
506544
fn cargo_package_should_build_in_build_dir_and_output_to_target_dir() {
507545
let p = project()

tests/testsuite/build_dir_legacy.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,43 @@ fn cargo_doc_should_output_to_target_dir() {
459459
assert_exists(&docs_dir.join("foo/index.html"));
460460
}
461461

462+
#[cargo_test(nightly, reason = "--output-format is unstable")]
463+
fn cargo_rustdoc_json_should_output_to_target_dir() {
464+
let p = project()
465+
.file("src/lib.rs", "")
466+
.file(
467+
".cargo/config.toml",
468+
r#"
469+
[build]
470+
target-dir = "target-dir"
471+
build-dir = "build-dir"
472+
"#,
473+
)
474+
.build();
475+
476+
p.cargo("rustdoc -Zunstable-options --output-format json")
477+
.masquerade_as_nightly_cargo(&["new build-dir layout", "rustdoc-output-format"])
478+
.enable_mac_dsym()
479+
.run();
480+
481+
let docs_dir = p.root().join("target-dir/doc");
482+
483+
assert_exists(&docs_dir);
484+
assert_exists(&docs_dir.join("foo.json"));
485+
486+
p.root().join("build-dir").assert_build_dir_layout(str![r#"
487+
[ROOT]/foo/build-dir/.rustc_info.json
488+
[ROOT]/foo/build-dir/.rustdoc_fingerprint.json
489+
[ROOT]/foo/build-dir/CACHEDIR.TAG
490+
[ROOT]/foo/build-dir/debug/.cargo-build-lock
491+
[ROOT]/foo/build-dir/debug/.fingerprint/foo-[HASH]/doc-lib-foo
492+
[ROOT]/foo/build-dir/debug/.fingerprint/foo-[HASH]/doc-lib-foo.json
493+
[ROOT]/foo/build-dir/debug/.fingerprint/foo-[HASH]/invoked.timestamp
494+
[ROOT]/foo/build-dir/debug/build/foo-[HASH]/out/foo.json
495+
496+
"#]);
497+
}
498+
462499
#[cargo_test]
463500
fn cargo_package_should_build_in_build_dir_and_output_to_target_dir() {
464501
let p = project()

tests/testsuite/rustdoc.rs

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Tests for the `cargo rustdoc` command.
22
3+
use std::fs;
4+
35
use crate::prelude::*;
46
use cargo_test_support::str;
57
use cargo_test_support::{basic_manifest, cross_compile, project};
@@ -44,7 +46,25 @@ fn rustdoc_simple_json() {
4446
.masquerade_as_nightly_cargo(&["rustdoc-output-format"])
4547
.with_stderr_data(str![[r#"
4648
[DOCUMENTING] foo v0.0.1 ([ROOT]/foo)
47-
[RUNNING] `rustdoc [..] --crate-name foo [..]-o [ROOT]/foo/target/doc [..] --output-format=json[..]
49+
[RUNNING] `rustdoc [..] --crate-name foo [..]-o [ROOT]/foo/target/debug/build/foo-[HASH]/out [..] --output-format=json[..]
50+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
51+
[GENERATED] [ROOT]/foo/target/doc/foo.json
52+
53+
"#]])
54+
.run();
55+
assert!(p.root().join("target/doc/foo.json").is_file());
56+
}
57+
58+
59+
#[cargo_test(nightly, reason = "--output-format is unstable")]
60+
fn rustdoc_json_with_new_layout() {
61+
let p = project().file("src/lib.rs", "").build();
62+
63+
p.cargo("rustdoc -Z unstable-options -Z build-dir-new-layout --output-format json -v")
64+
.masquerade_as_nightly_cargo(&["rustdoc-output-format"])
65+
.with_stderr_data(str![[r#"
66+
[DOCUMENTING] foo v0.0.1 ([ROOT]/foo)
67+
[RUNNING] `rustdoc [..] --crate-name foo [..]-o [ROOT]/foo/target/debug/build/foo/[HASH]/out [..] --output-format=json[..]
4868
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
4969
[GENERATED] [ROOT]/foo/target/doc/foo.json
5070
@@ -322,3 +342,99 @@ fn fail_with_glob() {
322342
"#]])
323343
.run();
324344
}
345+
346+
#[cargo_test(nightly, reason = "--output-format is unstable")]
347+
fn rustdoc_json_same_crate_different_version() {
348+
let entry = project()
349+
.file(
350+
"Cargo.toml",
351+
r#"
352+
[package]
353+
name = "entry"
354+
version = "0.1.0"
355+
edition = "2021"
356+
357+
[dependencies]
358+
dep_v1 = { path = "../dep_v1", package = "dep" }
359+
dep_v2 = { path = "../dep_v2", package = "dep" }
360+
"#,
361+
)
362+
.file("src/lib.rs", "pub fn entry() {}")
363+
.build();
364+
365+
let _dep_v1 = project()
366+
.at("dep_v1")
367+
.file(
368+
"Cargo.toml",
369+
r#"
370+
[package]
371+
name = "dep"
372+
version = "1.0.0"
373+
edition = "2021"
374+
"#,
375+
)
376+
.file("src/lib.rs", "pub fn dep_v1_fn() {}")
377+
.build();
378+
379+
let _dep_v2 = project()
380+
.at("dep_v2")
381+
.file(
382+
"Cargo.toml",
383+
r#"
384+
[package]
385+
name = "dep"
386+
version = "2.0.0"
387+
edition = "2021"
388+
"#,
389+
)
390+
.file("src/lib.rs", "pub fn dep_v2_fn() {}")
391+
.build();
392+
393+
entry
394+
.cargo("rustdoc -v -Z unstable-options --output-format json -p dep@1.0.0")
395+
.masquerade_as_nightly_cargo(&["rustdoc-output-format"])
396+
.with_stderr_data(str![[r#"
397+
[LOCKING] 2 packages to latest compatible versions
398+
[DOCUMENTING] dep v1.0.0 ([ROOT]/dep_v1)
399+
[RUNNING] `rustdoc [..] --crate-name dep [ROOT]/dep_v1/src/lib.rs [..] --output-format=json[..]`
400+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
401+
[GENERATED] [ROOT]/foo/target/doc/dep.json
402+
403+
"#]])
404+
.run();
405+
406+
let dep_json = fs::read_to_string(entry.root().join("target/doc/dep.json")).unwrap();
407+
assert!(dep_json.contains("dep_v1_fn"));
408+
assert!(!dep_json.contains("dep_v2_fn"));
409+
410+
entry
411+
.cargo("rustdoc -v -Z unstable-options --output-format json -p dep@2.0.0")
412+
.masquerade_as_nightly_cargo(&["rustdoc-output-format"])
413+
.with_stderr_data(str![[r#"
414+
[DOCUMENTING] dep v2.0.0 ([ROOT]/dep_v2)
415+
[RUNNING] `rustdoc [..] --crate-name dep [ROOT]/dep_v2/src/lib.rs [..] --output-format=json[..]`
416+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
417+
[GENERATED] [ROOT]/foo/target/doc/dep.json
418+
419+
"#]])
420+
.run();
421+
422+
let dep_json = fs::read_to_string(entry.root().join("target/doc/dep.json")).unwrap();
423+
assert!(!dep_json.contains("dep_v1_fn"));
424+
assert!(dep_json.contains("dep_v2_fn"));
425+
426+
entry
427+
.cargo("rustdoc -v -Z unstable-options --output-format json -p dep@1.0.0")
428+
.masquerade_as_nightly_cargo(&["rustdoc-output-format"])
429+
.with_stderr_data(str![[r#"
430+
[FRESH] dep v1.0.0 ([ROOT]/dep_v1)
431+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
432+
[GENERATED] [ROOT]/foo/target/doc/dep.json
433+
434+
"#]])
435+
.run();
436+
437+
let dep_json = fs::read_to_string(entry.root().join("target/doc/dep.json")).unwrap();
438+
assert!(dep_json.contains("dep_v1_fn"));
439+
assert!(!dep_json.contains("dep_v2_fn"));
440+
}

0 commit comments

Comments
 (0)