Skip to content

Commit b93c8a7

Browse files
Trotttargos
authored andcommitted
test: fix flaky doctool and test
Doctool tests have been failing a lot in CI on Win2008 R2. It appears async functions and callback-based functions are being used in combination such that the callback-based function cannot guarantee that it will invoke its callback. Convert the callback-based functions to async functions so we have one paradigm and reliable results. PR-URL: #29979 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent a23b5cb commit b93c8a7

File tree

3 files changed

+19
-34
lines changed

3 files changed

+19
-34
lines changed

test/doctool/test-doctool-html.js

+11-21
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const remark2rehype = require('remark-rehype');
2222
const raw = require('rehype-raw');
2323
const htmlStringify = require('rehype-stringify');
2424

25-
function toHTML({ input, filename, nodeVersion }, cb) {
25+
async function toHTML({ input, filename, nodeVersion }) {
2626
const content = unified()
2727
.use(markdown)
2828
.use(html.firstHeader)
@@ -34,10 +34,7 @@ function toHTML({ input, filename, nodeVersion }, cb) {
3434
.use(htmlStringify)
3535
.processSync(input);
3636

37-
html.toHTML(
38-
{ input, content, filename, nodeVersion },
39-
cb
40-
);
37+
return html.toHTML({ input, content, filename, nodeVersion });
4138
}
4239

4340
// Test data is a list of objects with two properties.
@@ -107,23 +104,16 @@ testData.forEach(({ file, html }) => {
107104
// Normalize expected data by stripping whitespace.
108105
const expected = html.replace(spaces, '');
109106

110-
readFile(file, 'utf8', common.mustCall((err, input) => {
107+
readFile(file, 'utf8', common.mustCall(async (err, input) => {
111108
assert.ifError(err);
112-
toHTML(
113-
{
114-
input: input,
115-
filename: 'foo',
116-
nodeVersion: process.version,
117-
},
118-
common.mustCall((err, output) => {
119-
assert.ifError(err);
109+
const output = await toHTML({ input: input,
110+
filename: 'foo',
111+
nodeVersion: process.version });
120112

121-
const actual = output.replace(spaces, '');
122-
// Assert that the input stripped of all whitespace contains the
123-
// expected markup.
124-
assert(actual.includes(expected),
125-
`ACTUAL: ${actual}\nEXPECTED: ${expected}`);
126-
})
127-
);
113+
const actual = output.replace(spaces, '');
114+
// Assert that the input stripped of all whitespace contains the
115+
// expected markup.
116+
assert(actual.includes(expected),
117+
`ACTUAL: ${actual}\nEXPECTED: ${expected}`);
128118
}));
129119
});

tools/doc/generate.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ if (!filename) {
6767
}
6868

6969

70-
fs.readFile(filename, 'utf8', (er, input) => {
70+
fs.readFile(filename, 'utf8', async (er, input) => {
7171
if (er) throw er;
7272

7373
const content = unified()
@@ -84,15 +84,10 @@ fs.readFile(filename, 'utf8', (er, input) => {
8484

8585
const basename = path.basename(filename, '.md');
8686

87-
html.toHTML(
88-
{ input, content, filename, nodeVersion },
89-
(err, html) => {
90-
const target = path.join(outputDir, `${basename}.html`);
91-
if (err) throw err;
92-
fs.writeFileSync(target, html);
93-
}
94-
);
87+
const myHtml = await html.toHTML({ input, content, filename, nodeVersion });
88+
const htmlTarget = path.join(outputDir, `${basename}.html`);
89+
fs.writeFileSync(htmlTarget, myHtml);
9590

96-
const target = path.join(outputDir, `${basename}.json`);
97-
fs.writeFileSync(target, JSON.stringify(content.json, null, 2));
91+
const jsonTarget = path.join(outputDir, `${basename}.json`);
92+
fs.writeFileSync(jsonTarget, JSON.stringify(content.json, null, 2));
9893
});

tools/doc/html.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const gtocHTML = unified()
6363
const templatePath = path.join(docPath, 'template.html');
6464
const template = fs.readFileSync(templatePath, 'utf8');
6565

66-
async function toHTML({ input, content, filename, nodeVersion }, cb) {
66+
async function toHTML({ input, content, filename, nodeVersion }) {
6767
filename = path.basename(filename, '.md');
6868

6969
const id = filename.replace(/\W+/g, '-');
@@ -87,7 +87,7 @@ async function toHTML({ input, content, filename, nodeVersion }, cb) {
8787
HTML = HTML.replace('__ALTDOCS__', '');
8888
}
8989

90-
cb(null, HTML);
90+
return HTML;
9191
}
9292

9393
// Set the section name based on the first header. Default to 'Index'.

0 commit comments

Comments
 (0)