diff --git a/src/tools/run-make-support/src/diff/mod.rs b/src/tools/run-make-support/src/diff/mod.rs index 3e0bdc1c6f64c..24fa88af82ec2 100644 --- a/src/tools/run-make-support/src/diff/mod.rs +++ b/src/tools/run-make-support/src/diff/mod.rs @@ -87,9 +87,7 @@ impl Diff { self } - #[track_caller] - pub fn run(&mut self) { - self.drop_bomb.defuse(); + fn run_common(&self) -> (&str, &str, String, String) { let expected = self.expected.as_ref().expect("expected text not set"); let mut actual = self.actual.as_ref().expect("actual text not set").to_string(); let expected_name = self.expected_name.as_ref().unwrap(); @@ -104,6 +102,14 @@ impl Diff { .header(expected_name, actual_name) .to_string(); + (expected_name, actual_name, output, actual) + } + + #[track_caller] + pub fn run(&mut self) { + self.drop_bomb.defuse(); + let (expected_name, actual_name, output, actual) = self.run_common(); + if !output.is_empty() { // If we can bless (meaning we have a file to write into and the `RUSTC_BLESS_TEST` // environment variable set), then we write into the file and return. @@ -120,4 +126,26 @@ impl Diff { ) } } + + #[track_caller] + pub fn run_fail(&mut self) { + self.drop_bomb.defuse(); + let (expected_name, actual_name, output, actual) = self.run_common(); + + if output.is_empty() { + // If we can bless (meaning we have a file to write into and the `RUSTC_BLESS_TEST` + // environment variable set), then we write into the file and return. + if let Some(ref expected_file) = self.expected_file { + if std::env::var("RUSTC_BLESS_TEST").is_ok() { + println!("Blessing `{}`", expected_file.display()); + fs_wrapper::write(expected_file, actual); + return; + } + } + panic!( + "test failed: `{}` is not different from `{}`\n\n{}", + expected_name, actual_name, output + ) + } + } } diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index ff98bd538db1f..31cb32d349ace 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -23,8 +23,6 @@ run-make/dep-info-spaces/Makefile run-make/dep-info/Makefile run-make/dump-ice-to-disk/Makefile run-make/dump-mono-stats/Makefile -run-make/emit-path-unhashed/Makefile -run-make/emit-shared-files/Makefile run-make/emit-to-stdout/Makefile run-make/env-dep-info/Makefile run-make/export-executable-symbols/Makefile diff --git a/tests/run-make/emit-path-unhashed/Makefile b/tests/run-make/emit-path-unhashed/Makefile deleted file mode 100644 index 611f8578140e4..0000000000000 --- a/tests/run-make/emit-path-unhashed/Makefile +++ /dev/null @@ -1,37 +0,0 @@ -include ../tools.mk - -OUT=$(TMPDIR)/emit - -# --emit KIND=PATH should not affect crate hash vs --emit KIND -all: $(OUT)/a/libfoo.rlib $(OUT)/b/libfoo.rlib $(OUT)/c/libfoo.rlib \ - $(TMPDIR)/libfoo.rlib - $(RUSTC) -Zls=root $(TMPDIR)/libfoo.rlib > $(TMPDIR)/base.txt - $(RUSTC) -Zls=root $(OUT)/a/libfoo.rlib > $(TMPDIR)/a.txt - $(RUSTC) -Zls=root $(OUT)/b/libfoo.rlib > $(TMPDIR)/b.txt - $(RUSTC) -Zls=root $(OUT)/c/libfoo.rlib > $(TMPDIR)/c.txt - - diff $(TMPDIR)/base.txt $(TMPDIR)/a.txt - diff $(TMPDIR)/base.txt $(TMPDIR)/b.txt - - # Different KIND parameters do affect hash. - # diff exits 1 on difference, 2 on trouble - diff $(TMPDIR)/base.txt $(TMPDIR)/c.txt ; test "$$?" -eq 1 - -# Default output name -$(TMPDIR)/libfoo.rlib: foo.rs - $(RUSTC) --emit link foo.rs - -# Output named with -o -$(OUT)/a/libfoo.rlib: foo.rs - mkdir -p $(OUT)/a - $(RUSTC) --emit link -o $@ foo.rs - -# Output named with KIND=PATH -$(OUT)/b/libfoo.rlib: foo.rs - mkdir -p $(OUT)/b - $(RUSTC) --emit link=$@ foo.rs - -# Output multiple kinds -$(OUT)/c/libfoo.rlib: foo.rs - mkdir -p $(OUT)/c - $(RUSTC) --emit link=$@,metadata foo.rs diff --git a/tests/run-make/emit-path-unhashed/rmake.rs b/tests/run-make/emit-path-unhashed/rmake.rs new file mode 100644 index 0000000000000..ce56c19758868 --- /dev/null +++ b/tests/run-make/emit-path-unhashed/rmake.rs @@ -0,0 +1,34 @@ +// Specifying how rustc outputs a file can be done in different ways, such as +// the output flag or the KIND=NAME syntax. However, some of these methods used +// to result in different hashes on output files even though they yielded the +// exact same result otherwise. This was fixed in #86045, and this test checks +// that the hash is only modified when the output is made different, such as by +// adding a new output type (in this test, metadata). +// See https://github.com/rust-lang/rust/issues/86044 + +use run_make_support::{diff, fs_wrapper, rustc}; + +fn main() { + fs_wrapper::create_dir("emit"); + fs_wrapper::create_dir("emit/a"); + fs_wrapper::create_dir("emit/b"); + fs_wrapper::create_dir("emit/c"); + // The default output name. + rustc().emit("link").input("foo.rs").run(); + // The output is named with the output flag. + rustc().emit("link").output("emit/a/libfoo.rlib").input("foo.rs").run(); + // The output is named with link=NAME. + rustc().emit("link=emit/b/libfoo.rlib").input("foo.rs").run(); + // The output is named with link=NAME, with an additional kind tacked on. + rustc().emit("link=emit/c/libfoo.rlib,metadata").input("foo.rs").run(); + + let base = rustc().arg("-Zls=root").input("libfoo.rlib").run().stdout_utf8(); + let a = rustc().arg("-Zls=root").input("emit/a/libfoo.rlib").run().stdout_utf8(); + let b = rustc().arg("-Zls=root").input("emit/b/libfoo.rlib").run().stdout_utf8(); + let c = rustc().arg("-Zls=root").input("emit/c/libfoo.rlib").run().stdout_utf8(); + // Both the output flag and link=NAME methods do not modify the hash of the output file. + diff().expected_text("base", &base).actual_text("a", a).run(); + diff().expected_text("base", &base).actual_text("b", b).run(); + // However, having multiple types of outputs does modify the hash. + diff().expected_text("base", &base).actual_text("c", c).run_fail(); +} diff --git a/tests/run-make/emit-shared-files/Makefile b/tests/run-make/emit-shared-files/Makefile deleted file mode 100644 index 27c72b003686d..0000000000000 --- a/tests/run-make/emit-shared-files/Makefile +++ /dev/null @@ -1,46 +0,0 @@ -include ../tools.mk - -INVOCATION_ONLY = $(TMPDIR)/invocation-only -TOOLCHAIN_ONLY = $(TMPDIR)/toolchain-only -ALL_SHARED = $(TMPDIR)/all-shared - -all: invocation-only toolchain-only all-shared - -invocation-only: - $(RUSTDOC) -Z unstable-options --emit=invocation-specific --output $(INVOCATION_ONLY) --resource-suffix=-xxx --theme y.css --extend-css z.css x.rs - [ -e $(INVOCATION_ONLY)/search-index-xxx.js ] - [ -e $(INVOCATION_ONLY)/settings.html ] - [ -e $(INVOCATION_ONLY)/x/all.html ] - [ -e $(INVOCATION_ONLY)/x/index.html ] - [ -e $(INVOCATION_ONLY)/theme-xxx.css ] # generated from z.css - ! [ -e $(INVOCATION_ONLY)/storage-xxx.js ] - ! [ -e $(INVOCATION_ONLY)/SourceSerif4-It.ttf.woff2 ] - - # FIXME: this probably shouldn't have a suffix - [ -e $(INVOCATION_ONLY)/y-xxx.css ] - # FIXME: this is technically incorrect (see `write_shared`) - ! [ -e $(INVOCATION_ONLY)/main-xxx.js ] - -toolchain-only: - $(RUSTDOC) -Z unstable-options --emit=toolchain-shared-resources --output $(TOOLCHAIN_ONLY) --resource-suffix=-xxx --extend-css z.css x.rs - [ -e $(TOOLCHAIN_ONLY)/static.files/storage-*.js ] - [ -e $(TOOLCHAIN_ONLY)/static.files/SourceSerif4-It-*.ttf.woff2 ] - ! [ -e $(TOOLCHAIN_ONLY)/search-index-xxx.js ] - ! [ -e $(TOOLCHAIN_ONLY)/x/index.html ] - ! [ -e $(TOOLCHAIN_ONLY)/theme.css ] - - [ -e $(TOOLCHAIN_ONLY)/static.files/main-*.js ] - ! [ -e $(TOOLCHAIN_ONLY)/y-xxx.css ] - -all-shared: - $(RUSTDOC) -Z unstable-options --emit=toolchain-shared-resources,unversioned-shared-resources --output $(ALL_SHARED) --resource-suffix=-xxx --extend-css z.css x.rs - [ -e $(ALL_SHARED)/static.files/storage-*.js ] - [ -e $(ALL_SHARED)/static.files/SourceSerif4-It-*.ttf.woff2 ] - ! [ -e $(ALL_SHARED)/search-index-xxx.js ] - ! [ -e $(ALL_SHARED)/settings.html ] - ! [ -e $(ALL_SHARED)/x ] - ! [ -e $(ALL_SHARED)/src ] - ! [ -e $(ALL_SHARED)/theme.css ] - - [ -e $(ALL_SHARED)/static.files/main-*.js ] - ! [ -e $(ALL_SHARED)/y-xxx.css ] diff --git a/tests/run-make/emit-shared-files/rmake.rs b/tests/run-make/emit-shared-files/rmake.rs new file mode 100644 index 0000000000000..33c1231024624 --- /dev/null +++ b/tests/run-make/emit-shared-files/rmake.rs @@ -0,0 +1,102 @@ +// This test checks the functionality of one of rustdoc's unstable options, +// the ability to specify emit restrictions with `--emit`. +// `invocation-only` should only emit crate-specific files. +// `toolchain-only` should only emit toolchain-specific files. +// `all-shared` should only emit files that can be shared between crates. +// See https://github.com/rust-lang/rust/pull/83478 + +use run_make_support::{has_extension, has_prefix, rustdoc, shallow_find_files}; +use std::path::Path; + +fn main() { + rustdoc() + .arg("-Zunstable-options") + .arg("--emit=invocation-specific") + .output("invocation-only") + .arg("--resource-suffix=-xxx") + .args(&["--theme", "y.css"]) + .args(&["--extend-css", "z.css"]) + .input("x.rs") + .run(); + assert!(Path::new("invocation-only/search-index-xxx.js").exists()); + assert!(Path::new("invocation-only/settings.html").exists()); + assert!(Path::new("invocation-only/x/all.html").exists()); + assert!(Path::new("invocation-only/x/index.html").exists()); + assert!(Path::new("invocation-only/theme-xxx.css").exists()); // generated from z.css + assert!(!Path::new("invocation-only/storage-xxx.js").exists()); + assert!(!Path::new("invocation-only/SourceSerif4-It.ttf.woff2").exists()); + // FIXME: this probably shouldn't have a suffix + assert!(Path::new("invocation-only/y-xxx.css").exists()); + // FIXME: this is technically incorrect (see `write_shared`) + assert!(!Path::new("invocation-only/main-xxx.js").exists()); + + rustdoc() + .arg("-Zunstable-options") + .arg("--emit=toolchain-shared-resources") + .output("toolchain-only") + .arg("--resource-suffix=-xxx") + .args(&["--extend-css", "z.css"]) + .input("x.rs") + .run(); + assert_eq!( + shallow_find_files("toolchain-only/static.files", |path| { + has_prefix(path, "storage-") && has_extension(path, "js") + }) + .len(), + 1 + ); + assert_eq!( + shallow_find_files("toolchain-only/static.files", |path| { + has_prefix(path, "SourceSerif4-It-") && has_extension(path, "woff2") + }) + .len(), + 1 + ); + assert_eq!( + shallow_find_files("toolchain-only/static.files", |path| { + has_prefix(path, "main-") && has_extension(path, "js") + }) + .len(), + 1 + ); + assert!(!Path::new("toolchain-only/search-index-xxx.js").exists()); + assert!(!Path::new("toolchain-only/x/index.html").exists()); + assert!(!Path::new("toolchain-only/theme.css").exists()); + assert!(!Path::new("toolchain-only/y-xxx.css").exists()); + + rustdoc() + .arg("-Zunstable-options") + .arg("--emit=toolchain-shared-resources,unversioned-shared-resources") + .output("all-shared") + .arg("--resource-suffix=-xxx") + .args(&["--extend-css", "z.css"]) + .input("x.rs") + .run(); + assert_eq!( + shallow_find_files("all-shared/static.files", |path| { + has_prefix(path, "storage-") && has_extension(path, "js") + }) + .len(), + 1 + ); + assert_eq!( + shallow_find_files("all-shared/static.files", |path| { + has_prefix(path, "SourceSerif4-It-") && has_extension(path, "woff2") + }) + .len(), + 1 + ); + assert!(!Path::new("all-shared/search-index-xxx.js").exists()); + assert!(!Path::new("all-shared/settings.html").exists()); + assert!(!Path::new("all-shared/x").exists()); + assert!(!Path::new("all-shared/src").exists()); + assert!(!Path::new("all-shared/theme.css").exists()); + assert_eq!( + shallow_find_files("all-shared/static.files", |path| { + has_prefix(path, "main-") && has_extension(path, "js") + }) + .len(), + 1 + ); + assert!(!Path::new("all-shared/y-xxx.css").exists()); +}