Skip to content

Commit

Permalink
Moved process markdownlint results to a dedicated file 🚓
Browse files Browse the repository at this point in the history
  • Loading branch information
01taylop committed Jul 24, 2024
1 parent 6a1bae4 commit 398b801
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 233 deletions.
3 changes: 3 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./node_modules/lint-pilot/markdownlint.json"
}
165 changes: 3 additions & 162 deletions src/linters/markdownlint/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,166 +70,6 @@ describe('markdownlint', () => {
}
})

it('resolves with results and a summary when markdownlint successfully lints (no files)', async () => {
jest.mocked(markdownlintAsync).mockResolvedValueOnce({})

expect(await markdownlintLib.lintFiles({
files: [],
fix: false,
})).toStrictEqual({
results: {},
summary: {
deprecatedRules: [],
errorCount: 0,
fileCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: 'MarkdownLint',
warningCount: 0,
},
})
})

it('resolves with results and a summary when markdownlint successfully lints (no errors)', async () => {
const lintResults: LintResults = {
'README.md': [],
}

jest.mocked(markdownlintAsync).mockResolvedValueOnce(lintResults)

expect(await markdownlintLib.lintFiles({
files: testFiles,
fix: false,
})).toStrictEqual({
results: {},
summary: {
deprecatedRules: [],
errorCount: 0,
fileCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: 'MarkdownLint',
warningCount: 0,
},
})
})

it('resolves with results and a summary when markdownlint successfully lints (with errors)', async () => {
const commonResult = {
ruleNames: ['MD000', 'test-rule-name'],
ruleInformation: 'test-rule-information',
errorDetail: 'test-error-detail',
errorContext: 'test-error-context',
errorRange: [1, 2],
}

const lintResults: LintResults = {
// No errors
'CHANGELOG.md': [],
'CONTRIBUTING.md': [{
...commonResult,
lineNumber: 1,
fixInfo: {
lineNumber: 1,
},
ruleDescription: 'test-rule-description',
}],
// 5 errors
'README.md': [{
...commonResult,
lineNumber: 7,
errorRange: [],
fixInfo: {
lineNumber: 7,
},
ruleDescription: 'no-error-range',
}, {
...commonResult,
errorDetail: '',
lineNumber: 9,
ruleDescription: 'no-error-detail',
}, {
...commonResult,
lineNumber: 13,
ruleNames: ['MD000', 'test-rule-b'],
ruleDescription: 'sorted-by-name',
}, {
...commonResult,
lineNumber: 13,
ruleNames: ['MD000', 'test-rule-a'],
ruleDescription: 'sorted-by-name',
}, {
...commonResult,
errorDetail: '',
lineNumber: 3,
ruleDescription: 'sort-by-line-number',
}],
}

const resultThemes = {
messageTheme: expect.any(Function),
positionTheme: expect.any(Function),
ruleTheme: expect.any(Function),
}

jest.mocked(markdownlintAsync).mockResolvedValueOnce(lintResults)

expect(await markdownlintLib.lintFiles({
files: testFiles,
fix: false,
})).toStrictEqual({
results: {
'CONTRIBUTING.md': [{
...resultThemes,
message: 'test-rule-description: test-error-detail',
position: '1:1',
rule: 'test-rule-name',
severity: 'X',
}],
'README.md': [{
...resultThemes,
message: 'sort-by-line-number',
position: '3:1',
rule: 'test-rule-name',
severity: 'X',
}, {
...resultThemes,
message: 'no-error-range: test-error-detail',
position: '7',
rule: 'test-rule-name',
severity: 'X',
}, {
...resultThemes,
message: 'no-error-detail',
position: '9:1',
rule: 'test-rule-name',
severity: 'X',
}, {
...resultThemes,
message: 'sorted-by-name: test-error-detail',
position: '13:1',
rule: 'test-rule-a',
severity: 'X',
}, {
...resultThemes,
message: 'sorted-by-name: test-error-detail',
position: '13:1',
rule: 'test-rule-b',
severity: 'X',
}],
},
summary: {
deprecatedRules: [],
errorCount: 6,
fileCount: 3,
fixableErrorCount: 2,
fixableWarningCount: 0,
linter: 'MarkdownLint',
warningCount: 0,
},
})
})

it('does not fix lint errors when the fix option is disabled', async () => {
const lintResults: LintResults = {
'README.md': [],
Expand Down Expand Up @@ -276,8 +116,9 @@ describe('markdownlint', () => {
'README.md': [],
}

jest.mocked(markdownlintAsync).mockResolvedValueOnce(lintResultsWithError)
jest.mocked(markdownlintAsync).mockResolvedValueOnce(lintResultsWithoutError)
jest.mocked(markdownlintAsync)
.mockResolvedValueOnce(lintResultsWithError)
.mockResolvedValueOnce(lintResultsWithoutError)

await markdownlintLib.lintFiles({
files: testFiles,
Expand Down
15 changes: 1 addition & 14 deletions src/linters/markdownlint/__tests__/loadConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,7 @@ describe('loadConfig', () => {
expect(markdownlint.readConfigSync).toHaveBeenCalledWith(`${process.cwd()}/.markdownlint.json`)
})

it('returns the development config when NODE_ENV is development', () => {
process.env.NODE_ENV = 'development'

jest.mocked(fs.existsSync).mockReturnValueOnce(false)

expect(loadConfig()).toStrictEqual(['development', {
default: true,
}])
expect(markdownlint.readConfigSync).toHaveBeenCalledWith(expect.stringContaining('lint-pilot/config/markdownlint.json'))
})

it('returns the default config when NODE_ENV is production', () => {
process.env.NODE_ENV = 'production'

it('returns the default config if no custom config exists', () => {
jest.mocked(fs.existsSync).mockReturnValueOnce(false)

expect(loadConfig()).toStrictEqual(['default', {
Expand Down
139 changes: 139 additions & 0 deletions src/linters/markdownlint/__tests__/processResults.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { expectedResultThemes, markdownlintError } from '@Jest/testData'

import { type LintResults } from '../markdownlintAsync'
import processResults from '../processResults'

describe('processResults', () => {

it('returns results and a summary when there are no lint results', () => {
const report = processResults({})

expect(report).toStrictEqual({
results: {},
summary: {
deprecatedRules: [],
errorCount: 0,
fileCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: 'MarkdownLint',
warningCount: 0,
},
})
})

it('returns results and a summary when there are no errors', () => {
const lintResults: LintResults = {
'README.md': [],
}

const report = processResults(lintResults)

expect(report).toStrictEqual({
results: {},
summary: {
deprecatedRules: [],
errorCount: 0,
fileCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: 'MarkdownLint',
warningCount: 0,
},
})
})

it('returns results sorted by lineNumber and a summary when there are errors', () => {
const lintResults: LintResults = {
// No errors
'CHANGELOG.md': [],
'CONTRIBUTING.md': [markdownlintError],
// 5 errors
'README.md': [{
...markdownlintError,
lineNumber: 7,
errorRange: [],
ruleDescription: 'no-error-range',
}, {
...markdownlintError,
errorDetail: '',
fixInfo: undefined,
lineNumber: 9,
ruleDescription: 'no-error-detail',
}, {
...markdownlintError,
fixInfo: undefined,
lineNumber: 13,
ruleNames: ['MD000', 'test-rule-b'],
ruleDescription: 'sorted-by-name',
}, {
...markdownlintError,
fixInfo: undefined,
lineNumber: 13,
ruleNames: ['MD000', 'test-rule-a'],
ruleDescription: 'sorted-by-name',
}, {
...markdownlintError,
fixInfo: undefined,
errorDetail: '',
lineNumber: 3,
ruleDescription: 'sort-by-line-number',
}],
}

const report = processResults(lintResults)

expect(report).toStrictEqual({
results: {
'CONTRIBUTING.md': [{
...expectedResultThemes,
message: 'test-rule-description: test-error-detail',
position: '1:1',
rule: 'test-rule-name',
severity: 'X',
}],
'README.md': [{
...expectedResultThemes,
message: 'sort-by-line-number',
position: '3:1',
rule: 'test-rule-name',
severity: 'X',
}, {
...expectedResultThemes,
message: 'no-error-range: test-error-detail',
position: '7',
rule: 'test-rule-name',
severity: 'X',
}, {
...expectedResultThemes,
message: 'no-error-detail',
position: '9:1',
rule: 'test-rule-name',
severity: 'X',
}, {
...expectedResultThemes,
message: 'sorted-by-name: test-error-detail',
position: '13:1',
rule: 'test-rule-a',
severity: 'X',
}, {
...expectedResultThemes,
message: 'sorted-by-name: test-error-detail',
position: '13:1',
rule: 'test-rule-b',
severity: 'X',
}],
},
summary: {
deprecatedRules: [],
errorCount: 6,
fileCount: 3,
fixableErrorCount: 2,
fixableWarningCount: 0,
linter: 'MarkdownLint',
warningCount: 0,
},
})
})

})
Loading

0 comments on commit 398b801

Please sign in to comment.