Skip to content

Commit

Permalink
Rollup merge of rust-lang#79443 - GuillaumeGomez:improve-rustdoc-js-e…
Browse files Browse the repository at this point in the history
…rror-output, r=jyn514

Improve rustdoc JS tests error output

It's pretty common when starting to add new tests for rustdoc-js to have issues to understand the errors. With this, it should make things a bit simpler. So now, in case of an error, it displays:

```
---- [js-doc-test] rustdoc-js/basic.rs stdout ----

error: rustdoc-js test failed!
failed to decode compiler output as json: line: {
output: Checking "basic" ... FAILED
==> Result not found in 'others': '{"path":"basic","name":"Fo"}'
Diff of first error:
{
    "path": "basic",
    - "name": "Fo",
    + "name": "Foo",
}
thread '[js-doc-test] rustdoc-js/basic.rs' panicked at 'explicit panic', src/tools/compiletest/src/json.rs:126:21
```

I think it was `@camelid` who asked about it a few days ago?

r? `@jyn514`
  • Loading branch information
GuillaumeGomez authored Nov 28, 2020
2 parents 8dfa754 + fa14f22 commit 25217a8
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/tools/rustdoc-js/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
return content;
}

function contentToDiffLine(key, value) {
return `"${key}": "${value}",`;
}

// This function is only called when no matching result was found and therefore will only display
// the diff between the two items.
function betterLookingDiff(entry, data) {
let output = ' {\n';
let spaces = ' ';
for (let key in entry) {
if (!entry.hasOwnProperty(key)) {
continue;
}
let value = data[key];
if (value !== entry[key]) {
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
output += '+' + spaces + contentToDiffLine(key, value) + '\n';
} else {
output += spaces + contentToDiffLine(key, value) + '\n';
}
}
return output + ' }';
}

function lookForEntry(entry, data) {
for (var i = 0; i < data.length; ++i) {
var allGood = true;
Expand Down Expand Up @@ -281,6 +305,13 @@ function runSearch(query, expected, index, loaded, loadedFile, queryName) {
if (entry_pos === null) {
error_text.push(queryName + "==> Result not found in '" + key + "': '" +
JSON.stringify(entry[i]) + "'");
// By default, we just compare the two first items.
let item_to_diff = 0;
if ((ignore_order === false || exact_check === true) && i < results[key].length) {
item_to_diff = i;
}
error_text.push("Diff of first error:\n" +
betterLookingDiff(entry[i], results[key][item_to_diff]));
} else if (exact_check === true && prev_pos + 1 !== entry_pos) {
error_text.push(queryName + "==> Exact check failed at position " + (prev_pos + 1) +
": expected '" + JSON.stringify(entry[i]) + "' but found '" +
Expand Down

0 comments on commit 25217a8

Please sign in to comment.