Skip to content

Commit

Permalink
feat(cli): sort linting results alphabetically (#2147)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored May 11, 2022
1 parent e6a90f2 commit 84d48cf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"info": {
"title": "no stoplight :("
}
}
25 changes: 24 additions & 1 deletion packages/cli/src/services/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}

0 comments on commit 84d48cf

Please sign in to comment.