|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { shouldIncludeResult } from '../../lib/helpers/should-include-result' |
| 3 | +import { reportingConfig } from '../../style/github-docs' |
| 4 | + |
| 5 | +describe('lint report exclusions', () => { |
| 6 | + // Helper function to simulate the reporting logic from lint-report.ts |
| 7 | + function shouldIncludeInReport(flaw, filePath) { |
| 8 | + if (!flaw.ruleNames || !Array.isArray(flaw.ruleNames)) { |
| 9 | + return false |
| 10 | + } |
| 11 | + |
| 12 | + // First check exclusions using shared function |
| 13 | + if (!shouldIncludeResult(flaw, filePath)) { |
| 14 | + return false |
| 15 | + } |
| 16 | + |
| 17 | + // Extract all possible rule names including sub-rules from search-replace |
| 18 | + const allRuleNames = [...flaw.ruleNames] |
| 19 | + if (flaw.ruleNames.includes('search-replace') && flaw.errorDetail) { |
| 20 | + const match = flaw.errorDetail.match(/^([^:]+):/) |
| 21 | + if (match) { |
| 22 | + allRuleNames.push(match[1]) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + // Apply reporting-specific filtering |
| 27 | + // Check if severity should be included |
| 28 | + if (reportingConfig.includeSeverities.includes(flaw.severity)) { |
| 29 | + return true |
| 30 | + } |
| 31 | + |
| 32 | + // Check if any rule name is in the include list |
| 33 | + const hasIncludedRule = allRuleNames.some((ruleName) => |
| 34 | + reportingConfig.includeRules.includes(ruleName), |
| 35 | + ) |
| 36 | + if (hasIncludedRule) { |
| 37 | + return true |
| 38 | + } |
| 39 | + |
| 40 | + return false |
| 41 | + } |
| 42 | + |
| 43 | + test('TODOCS placeholder errors are excluded for documentation file', () => { |
| 44 | + const flaw = { |
| 45 | + severity: 'error', |
| 46 | + ruleNames: ['search-replace'], |
| 47 | + errorDetail: 'todocs-placeholder: Catch occurrences of TODOCS placeholder.', |
| 48 | + } |
| 49 | + |
| 50 | + const excludedFilePath = |
| 51 | + 'content/contributing/collaborating-on-github-docs/using-the-todocs-placeholder-to-leave-notes.md' |
| 52 | + const regularFilePath = 'content/some-other-article.md' |
| 53 | + |
| 54 | + // Should be excluded for the specific documentation file |
| 55 | + expect(shouldIncludeInReport(flaw, excludedFilePath)).toBe(false) |
| 56 | + |
| 57 | + // Should still be included for other files |
| 58 | + expect(shouldIncludeInReport(flaw, regularFilePath)).toBe(true) |
| 59 | + }) |
| 60 | + |
| 61 | + test('TODOCS placeholder errors are excluded with different path formats', () => { |
| 62 | + const flaw = { |
| 63 | + severity: 'error', |
| 64 | + ruleNames: ['search-replace'], |
| 65 | + errorDetail: 'todocs-placeholder: Catch occurrences of TODOCS placeholder.', |
| 66 | + } |
| 67 | + |
| 68 | + // Test various path formats that should match |
| 69 | + const pathVariants = [ |
| 70 | + 'content/contributing/collaborating-on-github-docs/using-the-todocs-placeholder-to-leave-notes.md', |
| 71 | + './content/contributing/collaborating-on-github-docs/using-the-todocs-placeholder-to-leave-notes.md', |
| 72 | + '/absolute/path/content/contributing/collaborating-on-github-docs/using-the-todocs-placeholder-to-leave-notes.md', |
| 73 | + ] |
| 74 | + |
| 75 | + pathVariants.forEach((path) => { |
| 76 | + expect(shouldIncludeInReport(flaw, path)).toBe(false) |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + test('other rules are not affected by TODOCS file exclusions', () => { |
| 81 | + const flaw = { |
| 82 | + severity: 'error', |
| 83 | + ruleNames: ['docs-domain'], |
| 84 | + } |
| 85 | + |
| 86 | + const excludedFilePath = |
| 87 | + 'content/contributing/collaborating-on-github-docs/using-the-todocs-placeholder-to-leave-notes.md' |
| 88 | + |
| 89 | + // Should still be included for other rules even in the excluded file |
| 90 | + expect(shouldIncludeInReport(flaw, excludedFilePath)).toBe(true) |
| 91 | + }) |
| 92 | + |
| 93 | + test('multiple rule names with mixed exclusions', () => { |
| 94 | + const flaw = { |
| 95 | + severity: 'error', |
| 96 | + ruleNames: ['search-replace', 'docs-domain'], |
| 97 | + errorDetail: 'todocs-placeholder: Catch occurrences of TODOCS placeholder.', |
| 98 | + } |
| 99 | + |
| 100 | + const excludedFilePath = |
| 101 | + 'content/contributing/collaborating-on-github-docs/using-the-todocs-placeholder-to-leave-notes.md' |
| 102 | + |
| 103 | + // Should be excluded because one of the rules (todocs-placeholder) is excluded for this file |
| 104 | + expect(shouldIncludeInReport(flaw, excludedFilePath)).toBe(false) |
| 105 | + }) |
| 106 | + |
| 107 | + test('exclusion configuration exists and is properly structured', () => { |
| 108 | + expect(reportingConfig.excludeFilesFromRules).toBeDefined() |
| 109 | + expect(reportingConfig.excludeFilesFromRules['todocs-placeholder']).toBeDefined() |
| 110 | + expect(Array.isArray(reportingConfig.excludeFilesFromRules['todocs-placeholder'])).toBe(true) |
| 111 | + expect( |
| 112 | + reportingConfig.excludeFilesFromRules['todocs-placeholder'].includes( |
| 113 | + 'content/contributing/collaborating-on-github-docs/using-the-todocs-placeholder-to-leave-notes.md', |
| 114 | + ), |
| 115 | + ).toBe(true) |
| 116 | + }) |
| 117 | + |
| 118 | + describe('shared shouldIncludeResult function', () => { |
| 119 | + test('excludes TODOCS placeholder errors for specific file', () => { |
| 120 | + const flaw = { |
| 121 | + severity: 'error', |
| 122 | + ruleNames: ['search-replace'], |
| 123 | + errorDetail: 'todocs-placeholder: Catch occurrences of TODOCS placeholder.', |
| 124 | + } |
| 125 | + |
| 126 | + const excludedFilePath = |
| 127 | + 'content/contributing/collaborating-on-github-docs/using-the-todocs-placeholder-to-leave-notes.md' |
| 128 | + const regularFilePath = 'content/some-other-article.md' |
| 129 | + |
| 130 | + // Should be excluded for the specific documentation file |
| 131 | + expect(shouldIncludeResult(flaw, excludedFilePath)).toBe(false) |
| 132 | + |
| 133 | + // Should be included for other files |
| 134 | + expect(shouldIncludeResult(flaw, regularFilePath)).toBe(true) |
| 135 | + }) |
| 136 | + |
| 137 | + test('includes flaws by default when no exclusions apply', () => { |
| 138 | + const flaw = { |
| 139 | + severity: 'error', |
| 140 | + ruleNames: ['some-other-rule'], |
| 141 | + } |
| 142 | + |
| 143 | + const filePath = 'content/some-article.md' |
| 144 | + |
| 145 | + expect(shouldIncludeResult(flaw, filePath)).toBe(true) |
| 146 | + }) |
| 147 | + |
| 148 | + test('handles missing errorDetail gracefully', () => { |
| 149 | + const flaw = { |
| 150 | + severity: 'error', |
| 151 | + ruleNames: ['search-replace'], |
| 152 | + // no errorDetail |
| 153 | + } |
| 154 | + |
| 155 | + const filePath = 'content/some-article.md' |
| 156 | + |
| 157 | + expect(shouldIncludeResult(flaw, filePath)).toBe(true) |
| 158 | + }) |
| 159 | + }) |
| 160 | +}) |
0 commit comments