blob: 43f623fa8af420c4e443b007f88c48d770e8e428 [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>The rustdoc test suite - Rust Compiler Development Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="A guide to developing the Rust compiler (rustc)">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="highlight-css" href="../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "../";
const default_light_theme = "light";
const default_dark_theme = "navy";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Rust Compiler Development Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/rust-lang/rustc-dev-guide" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/rust-lang/rustc-dev-guide/edit/master/src/rustdoc-internals/rustdoc-test-suite.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="the-rustdoc-test-suite"><a class="header" href="#the-rustdoc-test-suite">The <code>rustdoc</code> test suite</a></h1>
<p>This page is about the test suite named <code>rustdoc</code> used to test the HTML output of <code>rustdoc</code>.
For other rustdoc-specific test suites, see <a href="../tests/compiletest.html#rustdoc-test-suites">Rustdoc test suites</a>.</p>
<p>Each test file in this test suite is simply a Rust source file <code>file.rs</code> sprinkled with
so-called <em>directives</em> located inside normal Rust code comments.
These come in two flavors: <em>Compiletest</em> and <em>HtmlDocCk</em>.</p>
<p>To learn more about the former, read <a href="../tests/directives.html">Compiletest directives</a>.
For the latter, continue reading.</p>
<p>Internally, <a href="../tests/compiletest.html"><code>compiletest</code></a> invokes the supplementary checker script <a href="https://github.com/rust-lang/rust/blob/master/src/etc/htmldocck.py"><code>htmldocck.py</code></a>.</p>
<h2 id="htmldocck-directives"><a class="header" href="#htmldocck-directives">HtmlDocCk Directives</a></h2>
<p>Directives to HtmlDocCk are assertions that place constraints on the generated HTML.
They look similar to those given to <code>compiletest</code> in that they take the form of <code>//@</code> comments
but ultimately, they are completely distinct and processed by different programs.</p>
<p><a href="https://en.wikipedia.org/wiki/XPath">XPath</a> is used to query parts of the HTML document tree.</p>
<p><strong>Introductory example</strong>:</p>
<pre><code class="language-rust ignore (illustrative)">//@ has file/type.Alias.html
//@ has - '//*[@class="rust item-decl"]//code' 'type Alias = Option&lt;i32&gt;;'
pub type Alias = Option&lt;i32&gt;;</code></pre>
<p>Here, we check that documentation generated for crate <code>file</code> contains a page for the
public type alias <code>Alias</code> where the code block that is found at the top contains the
expected rendering of the item. The <code>//*[@class="rust item-decl"]//code</code> is an XPath
expression.</p>
<p>Conventionally, you place these directives directly above the thing they are meant to test.
Technically speaking however, they don't need to be as HtmlDocCk only looks for the directives.</p>
<p>All directives take a <code>PATH</code> argument.
To avoid repetition, <code>-</code> can be passed to it to re-use the previous <code>PATH</code> argument.
Since the path contains the name of the crate, it is conventional to add a
<code>#![crate_name = "foo"]</code> attribute to the crate root to shorten the resulting path.</p>
<p>All arguments take the form of shell-style (single or double) quoted strings,
with the exception of <code>COUNT</code> and the special <code>-</code> form of <code>PATH</code>.</p>
<p>All directives (except <code>files</code>) can be <em>negated</em> by putting a <code>!</code> in front of their name.
Before you add negated directives, please read about <a href="#caveats">their caveats</a>.</p>
<p>Similar to shell commands,
directives can extend across multiple lines if their last char is <code>\</code>.
In this case, the start of the next line should be <code>//</code>, with no <code>@</code>.</p>
<p>Similar to compiletest directives, besides a space you can also use a colon <code>:</code> to separate
the directive name and the arguments, however a space is preferred for HtmlDocCk directives.</p>
<p>Use the special string <code>{{channel}}</code> in XPaths, <code>PATTERN</code> arguments and <a href="#snapshot">snapshot files</a>
if you'd like to refer to the URL <code>https://doc.rust-lang.org/CHANNEL</code> where <code>CHANNEL</code> refers to the
current release channel (e.g, <code>stable</code> or <code>nightly</code>).</p>
<p>Listed below are all possible directives:</p>
<h3 id="has"><a class="header" href="#has"><code>has</code></a></h3>
<blockquote>
<p>Usage 1: <code>//@ has PATH</code></p>
</blockquote>
<p>Check that the file given by <code>PATH</code> exists.</p>
<blockquote>
<p>Usage 2: <code>//@ has PATH XPATH PATTERN</code></p>
</blockquote>
<p>Checks that the text of each element / attribute / text selected by <code>XPATH</code> in the
whitespace-normalized<sup class="footnote-reference" id="fr-1-1"><a href="#footnote-1">1</a></sup> file given by <code>PATH</code> matches the
(also whitespace-normalized) string <code>PATTERN</code>.</p>
<p><strong>Tip</strong>: If you'd like to avoid whitespace normalization and/or if you'd like to match with a regex,
use <code>matches</code> instead.</p>
<h3 id="hasraw"><a class="header" href="#hasraw"><code>hasraw</code></a></h3>
<blockquote>
<p>Usage: <code>//@ hasraw PATH PATTERN</code></p>
</blockquote>
<p>Checks that the contents of the whitespace-normalized<sup class="footnote-reference" id="fr-1-2"><a href="#footnote-1">1</a></sup> file given by <code>PATH</code>
matches the (also whitespace-normalized) string <code>PATTERN</code>.</p>
<p><strong>Tip</strong>: If you'd like to avoid whitespace normalization and / or if you'd like to match with a
regex, use <code>matchesraw</code> instead.</p>
<h3 id="matches"><a class="header" href="#matches"><code>matches</code></a></h3>
<blockquote>
<p>Usage: <code>//@ matches PATH XPATH PATTERN</code></p>
</blockquote>
<p>Checks that the text of each element / attribute / text selected by <code>XPATH</code> in the
file given by <code>PATH</code> matches the Python-flavored<sup class="footnote-reference" id="fr-2-1"><a href="#footnote-2">2</a></sup> regex <code>PATTERN</code>.</p>
<h3 id="matchesraw"><a class="header" href="#matchesraw"><code>matchesraw</code></a></h3>
<blockquote>
<p>Usage: <code>//@ matchesraw PATH PATTERN</code></p>
</blockquote>
<p>Checks that the contents of the file given by <code>PATH</code> matches the
Python-flavored<sup class="footnote-reference" id="fr-2-2"><a href="#footnote-2">2</a></sup> regex <code>PATTERN</code>.</p>
<h3 id="count"><a class="header" href="#count"><code>count</code></a></h3>
<blockquote>
<p>Usage: <code>//@ count PATH XPATH COUNT</code></p>
</blockquote>
<p>Checks that there are exactly <code>COUNT</code> matches for <code>XPATH</code> within the file given by <code>PATH</code>.</p>
<h3 id="snapshot"><a class="header" href="#snapshot"><code>snapshot</code></a></h3>
<blockquote>
<p>Usage: <code>//@ snapshot NAME PATH XPATH</code></p>
</blockquote>
<p>Checks that the element / text selected by <code>XPATH</code> in the file given by <code>PATH</code> matches the
pre-recorded subtree or text (the "snapshot") in file <code>FILE_STEM.NAME.html</code> where <code>FILE_STEM</code>
is the file stem of the test file.</p>
<p>Pass the <code>--bless</code> option to <code>compiletest</code> to accept the current subtree/text as expected.
This will overwrite the aforementioned file (or create it if it doesn't exist). It will
automatically normalize the channel-dependent URL <code>https://doc.rust-lang.org/CHANNEL</code> to
the special string <code>{{channel}}</code>.</p>
<h3 id="has-dir"><a class="header" href="#has-dir"><code>has-dir</code></a></h3>
<blockquote>
<p>Usage: <code>//@ has-dir PATH</code></p>
</blockquote>
<p>Checks for the existence of the directory given by <code>PATH</code>.</p>
<h3 id="files"><a class="header" href="#files"><code>files</code></a></h3>
<blockquote>
<p>Usage: <code>//@ files PATH ENTRIES</code></p>
</blockquote>
<p>Checks that the directory given by <code>PATH</code> contains exactly <code>ENTRIES</code>.
<code>ENTRIES</code> is a Python-like list of strings inside a quoted string.</p>
<p><strong>Example</strong>: <code>//@ files "foo/bar" '["index.html", "sidebar-items.js"]'</code></p>
<h2 id="compiletest-directives-brief"><a class="header" href="#compiletest-directives-brief">Compiletest Directives (Brief)</a></h2>
<p>As mentioned in the introduction, you also have access to <a href="../tests/directives.html">compiletest directives</a>.
Most importantly, they allow you to register auxiliary crates and
to pass flags to the <code>rustdoc</code> binary under test.
It's <em>strongly recommended</em> to read that chapter if you don't know anything about them yet.</p>
<p>Here are some details that are relevant to this test suite specifically:</p>
<ul>
<li>While you can use both <code>//@ compile-flags</code> and <code>//@ doc-flags</code> to pass flags to <code>rustdoc</code>,
prefer to user the latter to show intent. The former is meant for <code>rustc</code>.</li>
<li>Add <code>//@ build-aux-docs</code> to the test file that has auxiliary crates to not only compile the
auxiliaries with <code>rustc</code> but to also document them with <code>rustdoc</code>.</li>
</ul>
<h2 id="caveats"><a class="header" href="#caveats">Caveats</a></h2>
<p>Testing for the absence of an element or a piece of text is quite fragile and not very future proof.</p>
<p>It's not unusual that the <em>shape</em> of the generated HTML document tree changes from time to time.
This includes for example renamings of CSS classes.</p>
<p>Whenever that happens, <em>positive</em> checks will either continue to match the intended element /
attribute / text (if their XPath expression is general / loose enough) and
thus continue to test the correct thing or they won't in which case they would fail thereby
forcing the author of the change to look at them.</p>
<p>Compare that to <em>negative</em> checks (e.g., <code>//@ !has PATH XPATH PATTERN</code>) which won't fail if their
XPath expression "no longer" matches. The author who changed "the shape" thus won't get notified and
as a result someone else can unintentionally reintroduce <code>PATTERN</code> into the generated docs without
the original negative check failing.</p>
<p><strong>Note</strong>: Please avoid the use of <em>negated</em> checks!</p>
<p><strong>Tip</strong>: If you can't avoid it, please <strong>always</strong> pair it with an analogous positive check in the
immediate vicinity, so people changing "the shape" have a chance to notice and to update the
negated check!</p>
<h2 id="limitations"><a class="header" href="#limitations">Limitations</a></h2>
<p>HtmlDocCk uses the XPath implementation from the Python standard library.
This leads to several limitations:</p>
<ul>
<li>All <code>XPATH</code> arguments must start with <code>//</code> due to a flaw in the implementation.</li>
<li>Many XPath features (functions, axies, etc.) are not supported.</li>
<li>Only well-formed HTML can be parsed (hopefully rustdoc doesn't output mismatched tags).</li>
</ul>
<p>Furthmore, compiletest <a href="../tests/compiletest.html#revisions">revisions</a> are not supported.</p>
<hr>
<ol class="footnote-definition"><li id="footnote-1">
<p>Whitespace normalization means that all spans of consecutive whitespace are replaced with a single space. <a href="#fr-1-1"></a> <a href="#fr-1-2">↩2</a></p>
</li>
<li id="footnote-2">
<p>They are Unicode aware (flag <code>UNICODE</code> is set), match case-sensitively and in single-line mode. <a href="#fr-2-1"></a> <a href="#fr-2-2">↩2</a></p>
</li>
</ol>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../rustdoc-internals/search.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../rustdoc-internals/rustdoc-gui-test-suite.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../rustdoc-internals/search.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../rustdoc-internals/rustdoc-gui-test-suite.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
<script src="../mermaid.min.js"></script>
<script src="../mermaid-init.js"></script>
</div>
</body>
</html>