Skip to content

Commit d5d6036

Browse files
authored
Rollup merge of #79443 - GuillaumeGomez:improve-rustdoc-js-error-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``
2 parents 851def2 + fa14f22 commit d5d6036

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/tools/rustdoc-js/tester.js

+31
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
190190
return content;
191191
}
192192

193+
function contentToDiffLine(key, value) {
194+
return `"${key}": "${value}",`;
195+
}
196+
197+
// This function is only called when no matching result was found and therefore will only display
198+
// the diff between the two items.
199+
function betterLookingDiff(entry, data) {
200+
let output = ' {\n';
201+
let spaces = ' ';
202+
for (let key in entry) {
203+
if (!entry.hasOwnProperty(key)) {
204+
continue;
205+
}
206+
let value = data[key];
207+
if (value !== entry[key]) {
208+
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
209+
output += '+' + spaces + contentToDiffLine(key, value) + '\n';
210+
} else {
211+
output += spaces + contentToDiffLine(key, value) + '\n';
212+
}
213+
}
214+
return output + ' }';
215+
}
216+
193217
function lookForEntry(entry, data) {
194218
for (var i = 0; i < data.length; ++i) {
195219
var allGood = true;
@@ -281,6 +305,13 @@ function runSearch(query, expected, index, loaded, loadedFile, queryName) {
281305
if (entry_pos === null) {
282306
error_text.push(queryName + "==> Result not found in '" + key + "': '" +
283307
JSON.stringify(entry[i]) + "'");
308+
// By default, we just compare the two first items.
309+
let item_to_diff = 0;
310+
if ((ignore_order === false || exact_check === true) && i < results[key].length) {
311+
item_to_diff = i;
312+
}
313+
error_text.push("Diff of first error:\n" +
314+
betterLookingDiff(entry[i], results[key][item_to_diff]));
284315
} else if (exact_check === true && prev_pos + 1 !== entry_pos) {
285316
error_text.push(queryName + "==> Exact check failed at position " + (prev_pos + 1) +
286317
": expected '" + JSON.stringify(entry[i]) + "' but found '" +

0 commit comments

Comments
 (0)