Skip to content
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

feat(cli): sort linting results alphabetically #2147

Merged
merged 3 commits into from May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,5 @@
{
"info": {
"title": "no stoplight :("
}
}
25 changes: 24 additions & 1 deletion packages/cli/src/services/__tests__/linter.test.ts
Expand Up @@ -118,8 +118,8 @@ describe('Linter service', () => {

it('given a list of files is provided, outputs issues for each file', () => {
const documents = [
join(__dirname, `./__fixtures__/invalid-stoplight-info-document.json`),
join(__dirname, `./__fixtures__/missing-stoplight-info-document.json`),
join(__dirname, `./__fixtures__/missing-stoplight-info-document-copy.json`),
];

return expect(run(['lint', ...documents].join(' '))).resolves.toEqual([
Expand All @@ -142,6 +142,29 @@ describe('Linter service', () => {
]);
});

it('sorts linting results in an alphabetical order', () => {
const documents = [
join(__dirname, `./__fixtures__/missing-stoplight-info-document.json`),
join(__dirname, `./__fixtures__/openapi-3.0-valid.yaml`),
join(__dirname, `./__fixtures__/invalid-stoplight-info-document.json`),
];

return expect(run(['lint', ...documents].join(' '))).resolves.toEqual([
expect.objectContaining({
code: 'info-matches-stoplight',
source: join(__dirname, `./__fixtures__/invalid-stoplight-info-document.json`),
}),
expect.objectContaining({
code: 'info-matches-stoplight',
source: join(__dirname, `./__fixtures__/missing-stoplight-info-document.json`),
}),
expect.objectContaining({
code: 'info-matches-stoplight',
source: join(__dirname, `./__fixtures__/openapi-3.0-valid.yaml`),
}),
]);
});

describe('when glob is provided', () => {
const documents = join(__dirname, `./__fixtures__/missing-stoplight-info*.json`);

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/services/linter/utils/listFiles.ts
Expand Up @@ -10,6 +10,8 @@ async function match(pattern: fg.Pattern | fg.Pattern[]): Promise<string[]> {
return (await fg(pattern, GLOB_OPTIONS)).map(normalize);
}

const compareString = (a: string, b: string): number => a.localeCompare(b);

export async function listFiles(patterns: string[], ignoreUnmatchedGlobs: boolean): Promise<[string[], string[]]> {
const { files, urls } = patterns.reduce<{
files: string[];
Expand Down Expand Up @@ -49,5 +51,5 @@ export async function listFiles(patterns: string[], ignoreUnmatchedGlobs: boolea
);
}

return [[...urls, ...filesFound], fileSearchWithoutResult]; // let's normalize OS paths produced by fast-glob to have consistent paths across all platforms
return [[...urls, ...filesFound].sort(compareString), fileSearchWithoutResult]; // let's normalize OS paths produced by fast-glob to have consistent paths across all platforms
}