diff --git a/lib/generator.ts b/lib/generator.ts index 46b915a2..781cb008 100644 --- a/lib/generator.ts +++ b/lib/generator.ts @@ -179,24 +179,28 @@ export async function generate(path: string, options?: GenerateOptions) { urlRuleDoc ); - const contents = readFileSync(pathToDoc).toString(); + const contentsOld = readFileSync(pathToDoc).toString(); const contentsNew = await postprocess( updateRuleOptionsList( - replaceOrCreateHeader(contents, newHeaderLines, END_RULE_HEADER_MARKER), + replaceOrCreateHeader( + contentsOld, + newHeaderLines, + END_RULE_HEADER_MARKER + ), rule ), resolve(pathToDoc) ); if (check) { - if (contentsNew !== contents) { + if (contentsNew !== contentsOld) { console.error( `Please run eslint-doc-generator. A rule doc is out-of-date: ${relative( getPluginRoot(path), pathToDoc )}` ); - console.error(diff(contentsNew, contents, { expand: false })); + console.error(diff(contentsNew, contentsOld, { expand: false })); process.exitCode = 1; } } else { @@ -209,7 +213,7 @@ export async function generate(path: string, options?: GenerateOptions) { for (const section of ruleDocSectionInclude) { expectSectionHeaderOrFail( `\`${name}\` rule doc`, - contents, + contentsNew, [section], true ); @@ -219,7 +223,7 @@ export async function generate(path: string, options?: GenerateOptions) { for (const section of ruleDocSectionExclude) { expectSectionHeaderOrFail( `\`${name}\` rule doc`, - contents, + contentsNew, [section], false ); @@ -229,7 +233,7 @@ export async function generate(path: string, options?: GenerateOptions) { // Options section. expectSectionHeaderOrFail( `\`${name}\` rule doc`, - contents, + contentsNew, ['Options', 'Config'], hasOptions(schema) ); @@ -237,7 +241,7 @@ export async function generate(path: string, options?: GenerateOptions) { expectContentOrFail( `\`${name}\` rule doc`, 'rule option', - contents, + contentsNew, namedOption, true ); // Each rule option is mentioned. diff --git a/test/lib/generate/__snapshots__/rule-options-list-test.ts.snap b/test/lib/generate/__snapshots__/rule-options-list-test.ts.snap index 67b872aa..4cb62c09 100644 --- a/test/lib/generate/__snapshots__/rule-options-list-test.ts.snap +++ b/test/lib/generate/__snapshots__/rule-options-list-test.ts.snap @@ -43,7 +43,7 @@ exports[`generate (rule options list) with no options generates the documentatio "# test/no-foo -## Options + diff --git a/test/lib/generate/rule-options-list-test.ts b/test/lib/generate/rule-options-list-test.ts index 5ef92b46..8c490381 100644 --- a/test/lib/generate/rule-options-list-test.ts +++ b/test/lib/generate/rule-options-list-test.ts @@ -4,6 +4,7 @@ import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { readFileSync } from 'node:fs'; import { jest } from '@jest/globals'; +import * as sinon from 'sinon'; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -74,7 +75,10 @@ describe('generate (rule options list)', function () { }); it('generates the documentation', async function () { + const consoleErrorStub = sinon.stub(console, 'error'); await generate('.'); + expect(consoleErrorStub.callCount).toBe(0); + consoleErrorStub.restore(); expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot(); }); }); @@ -126,7 +130,10 @@ describe('generate (rule options list)', function () { }); it('generates the documentation', async function () { + const consoleErrorStub = sinon.stub(console, 'error'); await generate('.'); + expect(consoleErrorStub.callCount).toBe(0); + consoleErrorStub.restore(); expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot(); }); }); @@ -155,7 +162,7 @@ describe('generate (rule options list)', function () { 'README.md': '## Rules\n', - 'docs/rules/no-foo.md': `## Options + 'docs/rules/no-foo.md': ` `, @@ -170,7 +177,10 @@ describe('generate (rule options list)', function () { }); it('generates the documentation', async function () { + const consoleErrorStub = sinon.stub(console, 'error'); await generate('.'); + expect(consoleErrorStub.callCount).toBe(0); + consoleErrorStub.restore(); expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot(); }); }); @@ -211,7 +221,10 @@ describe('generate (rule options list)', function () { }); it('generates the documentation', async function () { + const consoleErrorStub = sinon.stub(console, 'error'); await generate('.'); + expect(consoleErrorStub.callCount).toBe(0); + consoleErrorStub.restore(); expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot(); }); }); @@ -255,7 +268,10 @@ describe('generate (rule options list)', function () { }); it('generates the documentation', async function () { + const consoleErrorStub = sinon.stub(console, 'error'); await generate('.'); + expect(consoleErrorStub.callCount).toBe(0); + consoleErrorStub.restore(); expect(readFileSync('docs/rules/no-foo.md', 'utf8')).toMatchSnapshot(); }); });