-
Notifications
You must be signed in to change notification settings - Fork 16
feat(plugin-eslint): add artifact loading logic #1077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BioPhoton
wants to merge
17
commits into
main
Choose a base branch
from
feat/plugin-eslint/add-artifact-loading-logic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
576ec36
feat(models): add globPath schema and types
BioPhoton 0a333ef
feat(models): export PluginArtifactOptions
BioPhoton 577d719
feat(plugin-eslint): add loadArtifacts helper
BioPhoton 31072f8
feat(plugin-eslint): use loadArtifacts helper
BioPhoton fd29da9
test(models): fix unit tests
BioPhoton 6c657a5
test(plugin-eslint): fix unit tests and lint
BioPhoton 47adbb4
fix(plugin-eslint): remove path resolved log
BioPhoton 03db1b9
fix: refine logic
BioPhoton cd3ee27
fix: wip
BioPhoton 79e541a
fix: fix lint issue
BioPhoton cd34ac4
Update packages/plugin-eslint/src/lib/runner/index.ts
BioPhoton 58bdc6f
Update packages/plugin-eslint/src/lib/runner/utils.ts
BioPhoton 3200adc
Merge remote-tracking branch 'origin/main' into feat/plugin-eslint/ad…
BioPhoton 638f4a3
refactor: fix tests
BioPhoton 1825a65
refactor: fix lint
BioPhoton 16e99f8
Merge remote-tracking branch 'origin/main' into feat/plugin-eslint/ad…
BioPhoton 3139b50
Merge branch 'main' into feat/plugin-eslint/add-artifact-loading-logic
BioPhoton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
packages/plugin-eslint/src/lib/runner/index.unit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import type { z } from 'zod'; | ||
import type { | ||
Audit, | ||
AuditOutput, | ||
pluginArtifactOptionsSchema, | ||
} from '@code-pushup/models'; | ||
import { ui } from '@code-pushup/utils'; | ||
import type { ESLintTarget } from '../config.js'; | ||
import { generateAuditOutputs } from './index.js'; | ||
import * as lintModule from './lint.js'; | ||
import type { LinterOutput } from './types.js'; | ||
import * as utilsFileModule from './utils.js'; | ||
|
||
describe('generateAuditOutputs', () => { | ||
const loadArtifactsSpy = vi.spyOn(utilsFileModule, 'loadArtifacts'); | ||
const lintSpy = vi.spyOn(lintModule, 'lint'); | ||
|
||
const mockAudits: Audit[] = [ | ||
{ slug: 'max-lines', title: 'Max lines', description: 'Test' }, | ||
{ slug: 'no-unused-vars', title: 'No unused vars', description: 'Test' }, | ||
]; | ||
const mockTargetPatterns = { patterns: ['src/**/*.ts'] }; | ||
const mockTargetPatternsAndConfigs = { | ||
patterns: ['lib/**/*.js'], | ||
eslintrc: '.eslintrc.js', | ||
}; | ||
const mockTargets: ESLintTarget[] = [ | ||
mockTargetPatterns, | ||
mockTargetPatternsAndConfigs, | ||
]; | ||
|
||
const mockLinterOutputs: LinterOutput[] = [ | ||
{ | ||
results: [ | ||
{ | ||
filePath: 'test.ts', | ||
messages: [ | ||
{ | ||
ruleId: 'max-lines', | ||
severity: 1, | ||
message: 'File has too many lines', | ||
line: 1, | ||
column: 1, | ||
}, | ||
], | ||
} as any, | ||
], | ||
ruleOptionsPerFile: { 'test.ts': { 'max-lines': [] } }, | ||
}, | ||
{ | ||
results: [ | ||
{ | ||
filePath: 'test.ts', | ||
messages: [ | ||
{ | ||
ruleId: 'max-lines', | ||
severity: 1, | ||
message: 'File has too many lines', | ||
line: 1, | ||
column: 1, | ||
}, | ||
], | ||
} as any, | ||
], | ||
ruleOptionsPerFile: { 'test.ts': { 'max-lines': [] } }, | ||
}, | ||
]; | ||
|
||
const mockedAuditOutputs: AuditOutput[] = [ | ||
{ | ||
slug: 'max-lines', | ||
score: 0, | ||
value: 2, | ||
displayValue: '2 warnings', | ||
details: { | ||
issues: [ | ||
{ | ||
message: 'File has too many lines', | ||
severity: 'warning', | ||
source: { | ||
file: 'test.ts', | ||
position: { | ||
startLine: 1, | ||
startColumn: 1, | ||
}, | ||
}, | ||
}, | ||
{ | ||
message: 'File has too many lines', | ||
severity: 'warning', | ||
source: { | ||
file: 'test.ts', | ||
position: { | ||
startLine: 1, | ||
startColumn: 1, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
slug: 'no-unused-vars', | ||
score: 1, | ||
value: 0, | ||
displayValue: 'passed', | ||
details: { issues: [] }, | ||
}, | ||
]; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should use loadArtifacts when artifacts are provided', async () => { | ||
const artifacts: z.infer<typeof pluginArtifactOptionsSchema> = { | ||
artifactsPaths: ['path/to/artifacts.json'], | ||
}; | ||
loadArtifactsSpy.mockResolvedValue(mockLinterOutputs); | ||
|
||
await expect( | ||
generateAuditOutputs({ | ||
audits: mockAudits, | ||
targets: mockTargets, | ||
artifacts, | ||
}), | ||
).resolves.toStrictEqual(mockedAuditOutputs); | ||
|
||
expect(loadArtifactsSpy).toHaveBeenCalledWith(artifacts); | ||
expect(lintSpy).not.toHaveBeenCalled(); | ||
expect(ui()).toHaveLogged('log', 'ESLint plugin executing 2 lint targets'); | ||
}); | ||
|
||
it('should use internal linting logic when artifacts are not provided', async () => { | ||
lintSpy.mockResolvedValueOnce(mockLinterOutputs.at(0)!); | ||
lintSpy.mockResolvedValueOnce(mockLinterOutputs.at(0)!); | ||
|
||
await expect( | ||
generateAuditOutputs({ | ||
audits: mockAudits, | ||
targets: mockTargets, | ||
outputDir: 'custom-output', | ||
}), | ||
).resolves.toStrictEqual(mockedAuditOutputs); | ||
|
||
expect(loadArtifactsSpy).not.toHaveBeenCalled(); | ||
expect(lintSpy).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { ESLint } from 'eslint'; | ||
import { glob } from 'glob'; | ||
import type { PluginArtifactOptions } from '@code-pushup/models'; | ||
import { executeProcess, readJsonFile } from '@code-pushup/utils'; | ||
import type { LinterOutput } from './types.js'; | ||
|
||
export async function loadArtifacts( | ||
artifacts: PluginArtifactOptions, | ||
): Promise<LinterOutput[]> { | ||
if (artifacts.generateArtifactsCommand) { | ||
const { command, args = [] } = | ||
typeof artifacts.generateArtifactsCommand === 'string' | ||
? { command: artifacts.generateArtifactsCommand } | ||
: artifacts.generateArtifactsCommand; | ||
|
||
await executeProcess({ | ||
command, | ||
args, | ||
ignoreExitCode: true, | ||
}); | ||
} | ||
|
||
const initialArtifactPaths = Array.isArray(artifacts.artifactsPaths) | ||
? artifacts.artifactsPaths | ||
: [artifacts.artifactsPaths]; | ||
|
||
const artifactPaths = await glob(initialArtifactPaths); | ||
|
||
return await Promise.all( | ||
artifactPaths.map(async artifactPath => { | ||
// ESLint CLI outputs raw ESLint.LintResult[], but we need LinterOutput format | ||
const results = await readJsonFile<ESLint.LintResult[]>(artifactPath); | ||
return { | ||
results, | ||
ruleOptionsPerFile: {}, // TODO | ||
}; | ||
}), | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.