Skip to content

Commit

Permalink
Rollup merge of rust-lang#112468 - GuillaumeGomez:change-rustdoc-js-f…
Browse files Browse the repository at this point in the history
…ormats, r=notriddle

Change format of rustdoc-js tests by putting query and correction directly alongside the expected values

As I was working on fixing merge conflicts in rust-lang#108537, I faced quite a big issue when trying to update the `rustdoc-js*` tests. To make it much simpler, this PR moves the `query` and `correction` directly alongside the expected data so now we know what is the query that is being run without needing to add comments or going back to the top of the file.

r? ```@notriddle```
  • Loading branch information
matthiaskrgr authored Jun 10, 2023
2 parents 4d36c84 + 9803651 commit 299929e
Show file tree
Hide file tree
Showing 68 changed files with 285 additions and 441 deletions.
90 changes: 49 additions & 41 deletions src/tools/rustdoc-js/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function contentToDiffLine(key, value) {
return `"${key}": "${value}",`;
}

function shouldIgnoreField(fieldName) {
return fieldName === "query" || fieldName === "correction";
}

// 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) {
Expand Down Expand Up @@ -135,6 +139,9 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
} else if (expected !== null && typeof expected !== "undefined" &&
expected.constructor == Object) { // eslint-disable-line eqeqeq
for (const key in expected) {
if (shouldIgnoreField(key)) {
continue;
}
if (!Object.prototype.hasOwnProperty.call(expected, key)) {
continue;
}
Expand Down Expand Up @@ -184,6 +191,9 @@ function runSearch(query, expected, doSearch, loadedFile, queryName) {
const error_text = [];

for (const key in expected) {
if (shouldIgnoreField(key)) {
continue;
}
if (!Object.prototype.hasOwnProperty.call(expected, key)) {
continue;
}
Expand Down Expand Up @@ -260,84 +270,83 @@ function checkResult(error_text, loadedFile, displaySuccess) {
return 1;
}

function runCheck(loadedFile, key, callback) {
const expected = loadedFile[key];
const query = loadedFile.QUERY;

if (Array.isArray(query)) {
if (!Array.isArray(expected)) {
console.log("FAILED");
console.log(`==> If QUERY variable is an array, ${key} should be an array too`);
return 1;
} else if (query.length !== expected.length) {
console.log("FAILED");
console.log(`==> QUERY variable should have the same length as ${key}`);
return 1;
function runCheckInner(callback, loadedFile, entry, getCorrections, extra) {
if (typeof entry.query !== "string") {
console.log("FAILED");
console.log("==> Missing `query` field");
return false;
}
let error_text = callback(entry.query, entry, extra ? "[ query `" + entry.query + "`]" : "");
if (checkResult(error_text, loadedFile, false) !== 0) {
return false;
}
if (entry.correction !== undefined) {
error_text = runCorrections(entry.query, entry.correction, getCorrections, loadedFile);
if (checkResult(error_text, loadedFile, false) !== 0) {
return false;
}
for (let i = 0; i < query.length; ++i) {
const error_text = callback(query[i], expected[i], "[ query `" + query[i] + "`]");
if (checkResult(error_text, loadedFile, false) !== 0) {
}
return true;
}

function runCheck(loadedFile, key, getCorrections, callback) {
const expected = loadedFile[key];

if (Array.isArray(expected)) {
for (const entry of expected) {
if (!runCheckInner(callback, loadedFile, entry, getCorrections, true)) {
return 1;
}
}
console.log("OK");
} else {
const error_text = callback(query, expected, "");
if (checkResult(error_text, loadedFile, true) !== 0) {
return 1;
}
} else if (!runCheckInner(callback, loadedFile, expected, getCorrections, false)) {
return 1;
}
console.log("OK");
return 0;
}

function hasCheck(content, checkName) {
return content.startsWith(`const ${checkName}`) || content.includes(`\nconst ${checkName}`);
}

function runChecks(testFile, doSearch, parseQuery, getCorrections) {
let checkExpected = false;
let checkParsed = false;
let checkCorrections = false;
let testFileContent = readFile(testFile) + "exports.QUERY = QUERY;";
let testFileContent = readFile(testFile);

if (testFileContent.indexOf("FILTER_CRATE") !== -1) {
testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;";
} else {
testFileContent += "exports.FILTER_CRATE = null;";
}

if (testFileContent.indexOf("\nconst EXPECTED") !== -1) {
if (hasCheck(testFileContent, "EXPECTED")) {
testFileContent += "exports.EXPECTED = EXPECTED;";
checkExpected = true;
}
if (testFileContent.indexOf("\nconst PARSED") !== -1) {
if (hasCheck(testFileContent, "PARSED")) {
testFileContent += "exports.PARSED = PARSED;";
checkParsed = true;
}
if (testFileContent.indexOf("\nconst CORRECTIONS") !== -1) {
testFileContent += "exports.CORRECTIONS = CORRECTIONS;";
checkCorrections = true;
}
if (!checkParsed && !checkExpected && !checkCorrections) {
if (!checkParsed && !checkExpected) {
console.log("FAILED");
console.log("==> At least `PARSED`, `EXPECTED`, or `CORRECTIONS` is needed!");
console.log("==> At least `PARSED` or `EXPECTED` is needed!");
return 1;
}

const loadedFile = loadContent(testFileContent);
let res = 0;

if (checkExpected) {
res += runCheck(loadedFile, "EXPECTED", (query, expected, text) => {
res += runCheck(loadedFile, "EXPECTED", getCorrections, (query, expected, text) => {
return runSearch(query, expected, doSearch, loadedFile, text);
});
}
if (checkParsed) {
res += runCheck(loadedFile, "PARSED", (query, expected, text) => {
res += runCheck(loadedFile, "PARSED", getCorrections, (query, expected, text) => {
return runParser(query, expected, parseQuery, text);
});
}
if (checkCorrections) {
res += runCheck(loadedFile, "CORRECTIONS", (query, expected) => {
return runCorrections(query, expected, getCorrections, loadedFile);
});
}
return res;
}

Expand Down Expand Up @@ -367,8 +376,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
},
getCorrections: function(queryStr, filterCrate, currentCrate) {
const parsedQuery = searchModule.parseQuery(queryStr);
searchModule.execQuery(parsedQuery, searchWords,
filterCrate, currentCrate);
searchModule.execQuery(parsedQuery, searchWords, filterCrate, currentCrate);
return parsedQuery.correction;
},
parseQuery: searchModule.parseQuery,
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/alias-1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const QUERY = '&';

const EXPECTED = {
'query': '&',
'others': [
{ 'path': 'std', 'name': 'reference' },
],
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/alias-2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const QUERY = '+';

const EXPECTED = {
'query': '+',
'others': [
{ 'path': 'std::ops', 'name': 'AddAssign' },
{ 'path': 'std::ops', 'name': 'Add' },
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/alias-3.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const QUERY = '!';

const EXPECTED = {
'query': '!',
'others': [
{ 'path': 'std', 'name': 'never' },
],
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/alias-4.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const QUERY = '<';

const EXPECTED = {
'query': '<',
'others': [
{ 'name': 'Ord' },
],
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/alias.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// ignore-order

const QUERY = '[';

const EXPECTED = {
'query': '[',
'others': [
{ 'path': 'std', 'name': 'slice' },
{ 'path': 'std::ops', 'name': 'IndexMut' },
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/asrawfd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// ignore-order

const QUERY = 'RawFd::as_raw_fd';

const EXPECTED = {
'query': 'RawFd::as_raw_fd',
'others': [
// Reproduction test for https://github.com/rust-lang/rust/issues/78724
// Validate that type alias methods get the correct path.
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/basic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const QUERY = 'String';

const EXPECTED = {
'query': 'String',
'others': [
{ 'path': 'std::string', 'name': 'String' },
{ 'path': 'std::ffi', 'name': 'CString' },
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/deduplication.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// ignore-order

const QUERY = 'is_nan';

const EXPECTED = {
'query': 'is_nan',
'others': [
{ 'path': 'std::f32', 'name': 'is_nan' },
{ 'path': 'std::f64', 'name': 'is_nan' },
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/enum-option.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const QUERY = 'enum:Option';

const EXPECTED = {
'query': 'enum:Option',
'others': [
{ 'path': 'std::option', 'name': 'Option' },
],
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-js-std/filter-crate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// exact-check

const QUERY = '"hashmap"';
const FILTER_CRATE = 'core';

const EXPECTED = {
'query': 'hashmap',
'others': [
],
};
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/fn-forget.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const QUERY = 'fn:forget';

const EXPECTED = {
'query': 'fn:forget',
'others': [
{ 'path': 'std::mem', 'name': 'forget' },
{ 'path': 'std::fmt', 'name': 'format' },
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/from_u.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const QUERY = 'from_u';

const EXPECTED = {
'query': 'from_u',
'others': [
{ 'path': 'std::char', 'name': 'from_u32' },
{ 'path': 'std::str', 'name': 'from_utf8' },
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/keyword.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// ignore-order

const QUERY = 'fn';

const EXPECTED = {
'query': 'fn',
'others': [
{ 'path': 'std', 'name': 'fn', ty: 15 }, // 15 is for primitive types
{ 'path': 'std', 'name': 'fn', ty: 21 }, // 21 is for keywords
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/macro-check.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// ignore-order

const QUERY = 'panic';

const EXPECTED = {
'query': 'panic',
'others': [
{ 'path': 'std', 'name': 'panic', ty: 14 }, // 15 is for macros
{ 'path': 'std', 'name': 'panic', ty: 0 }, // 0 is for modules
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/macro-print.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const QUERY = 'macro:print';

const EXPECTED = {
'query': 'macro:print',
'others': [
{ 'path': 'std', 'name': 'print' },
{ 'path': 'std', 'name': 'println' },
Expand Down
3 changes: 1 addition & 2 deletions tests/rustdoc-js-std/never.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const QUERY = '!';

const EXPECTED = {
'query': '!',
'others': [
{ 'path': 'std', 'name': 'never' },
],
Expand Down
7 changes: 2 additions & 5 deletions tests/rustdoc-js-std/option-type-signatures.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
const QUERY = [
'option, fnonce -> option',
'option -> default',
];

const EXPECTED = [
{
'query': 'option, fnonce -> option',
'others': [
{ 'path': 'std::option::Option', 'name': 'map' },
],
},
{
'query': 'option -> default',
'others': [
{ 'path': 'std::option::Option', 'name': 'unwrap_or_default' },
{ 'path': 'std::option::Option', 'name': 'get_or_insert_default' },
Expand Down
Loading

0 comments on commit 299929e

Please sign in to comment.