Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix implementors display #87035

Merged
merged 3 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ __pycache__/
**node_modules
**package-lock.json

## Rustdoc GUI tests
src/test/rustdoc-gui/src/**.lock

# Before adding new lines, see the comment at the top.
26 changes: 12 additions & 14 deletions src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,27 +907,25 @@ impl Step for RustdocGUI {
// We remove existing folder to be sure there won't be artifacts remaining.
let _ = fs::remove_dir_all(&out_dir);

let mut nb_generated = 0;
let src_path = "src/test/rustdoc-gui/src";
// We generate docs for the libraries present in the rustdoc-gui's src folder.
let libs_dir = builder.build.src.join("src/test/rustdoc-gui/src");
for entry in libs_dir.read_dir().expect("read_dir call failed") {
let entry = entry.expect("invalid entry");
let path = entry.path();
if path.extension().map(|e| e == "rs").unwrap_or(false) {
let mut command = builder.rustdoc_cmd(self.compiler);
command.arg(path).arg("-o").arg(&out_dir);
builder.run(&mut command);
nb_generated += 1;
}
}
assert!(nb_generated > 0, "no documentation was generated...");
let mut cargo = Command::new(&builder.initial_cargo);
cargo
.arg("doc")
.arg("--workspace")
.arg("--target-dir")
.arg(&out_dir)
.env("RUSTDOC", builder.rustdoc(self.compiler))
.env("RUSTC", builder.rustc(self.compiler))
.current_dir(&builder.build.src.join(src_path));
builder.run(&mut cargo);

// We now run GUI tests.
let mut command = Command::new(&nodejs);
command
.arg(builder.build.src.join("src/tools/rustdoc-gui/tester.js"))
.arg("--doc-folder")
.arg(out_dir)
.arg(out_dir.join("doc"))
.arg("--tests-folder")
.arg(builder.build.src.join("src/test/rustdoc-gui"));
for path in &builder.paths {
Expand Down
18 changes: 14 additions & 4 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@ function hideThemeButtonState() {
});
}

var currentNbImpls = implementors.getElementsByClassName("impl").length;
var traitName = document.querySelector("h1.fqn > .in-band > .trait").textContent;
var baseIdName = "impl-" + traitName + "-";
var libs = Object.getOwnPropertyNames(imp);
for (var i = 0, llength = libs.length; i < llength; ++i) {
if (libs[i] === window.currentCrate) { continue; }
Expand All @@ -705,6 +708,7 @@ function hideThemeButtonState() {

var code = document.createElement("code");
code.innerHTML = struct.text;
addClass(code, "in-band");

onEachLazy(code.getElementsByTagName("a"), function(elem) {
var href = elem.getAttribute("href");
Expand All @@ -714,12 +718,18 @@ function hideThemeButtonState() {
}
});

var display = document.createElement("h3");
var currentId = baseIdName + currentNbImpls;
var anchor = document.createElement("a");
anchor.href = "#" + currentId;
addClass(anchor, "anchor");

var display = document.createElement("div");
display.id = currentId;
addClass(display, "impl");
display.innerHTML = "<span class=\"in-band\"><table class=\"table-display\">" +
"<tbody><tr><td><code>" + code.outerHTML + "</code></td><td></td></tr>" +
"</tbody></table></span>";
display.appendChild(anchor);
display.appendChild(code);
list.appendChild(display);
currentNbImpls += 1;
}
}
};
Expand Down
16 changes: 16 additions & 0 deletions src/test/rustdoc-gui/implementors.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// The goal of this test is to check that the external trait implementors, generated with JS,
// have the same display than the "local" ones.
goto: file://|DOC_PATH|/implementors/trait.Whatever.html
assert: "#implementors-list"
// There are supposed to be two implementors listed.
assert-count: ("#implementors-list > .impl", 2)
// Now we check that both implementors have an anchor, an ID and a similar DOM.
assert: ("#implementors-list > .impl:nth-child(1) > a.anchor")
assert-attribute: ("#implementors-list > .impl:nth-child(1)", {"id": "impl-Whatever"})
assert-attribute: ("#implementors-list > .impl:nth-child(1) > a.anchor", {"href": "#impl-Whatever"})
assert: "#implementors-list > .impl:nth-child(1) > code.in-band"

assert: ("#implementors-list > .impl:nth-child(2) > a.anchor")
assert-attribute: ("#implementors-list > .impl:nth-child(2)", {"id": "impl-Whatever-1"})
assert-attribute: ("#implementors-list > .impl:nth-child(2) > a.anchor", {"href": "#impl-Whatever-1"})
assert: "#implementors-list > .impl:nth-child(2) > code.in-band"
18 changes: 18 additions & 0 deletions src/test/rustdoc-gui/src/Cargo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "implementors"
version = "0.1.0"

[[package]]
name = "lib2"
version = "0.1.0"
dependencies = [
"implementors",
]

[[package]]
name = "test_docs"
version = "0.1.0"
6 changes: 6 additions & 0 deletions src/test/rustdoc-gui/src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]
members = [
"test_docs",
"lib2",
"implementors",
]
7 changes: 7 additions & 0 deletions src/test/rustdoc-gui/src/implementors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "implementors"
version = "0.1.0"
edition = "2018"

[lib]
path = "lib.rs"
7 changes: 7 additions & 0 deletions src/test/rustdoc-gui/src/implementors/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub trait Whatever {
fn method() {}
}

pub struct Struct;

impl Whatever for Struct {}
10 changes: 10 additions & 0 deletions src/test/rustdoc-gui/src/lib2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "lib2"
version = "0.1.0"
edition = "2018"

[lib]
path = "lib.rs"

[dependencies]
implementors = { path = "../implementors" }
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ impl Trait for Foo {
type X = u32;
const Y: u32 = 0;
}

impl implementors::Whatever for Foo {}
7 changes: 7 additions & 0 deletions src/test/rustdoc-gui/src/lib2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
7 changes: 7 additions & 0 deletions src/test/rustdoc-gui/src/test_docs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_docs"
version = "0.1.0"
edition = "2018"

[lib]
path = "lib.rs"