diff --git a/packages/cli/src/services/__tests__/__fixtures__/invalid-stoplight-info-document.json b/packages/cli/src/services/__tests__/__fixtures__/invalid-stoplight-info-document.json new file mode 100644 index 000000000..1a71c21b7 --- /dev/null +++ b/packages/cli/src/services/__tests__/__fixtures__/invalid-stoplight-info-document.json @@ -0,0 +1,5 @@ +{ + "info": { + "title": "no stoplight :(" + } +} diff --git a/packages/cli/src/services/__tests__/linter.test.ts b/packages/cli/src/services/__tests__/linter.test.ts index 84ed44522..edcda750e 100644 --- a/packages/cli/src/services/__tests__/linter.test.ts +++ b/packages/cli/src/services/__tests__/linter.test.ts @@ -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([ @@ -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`); diff --git a/packages/cli/src/services/linter/utils/listFiles.ts b/packages/cli/src/services/linter/utils/listFiles.ts index 2edd01730..8049b2462 100644 --- a/packages/cli/src/services/linter/utils/listFiles.ts +++ b/packages/cli/src/services/linter/utils/listFiles.ts @@ -10,6 +10,8 @@ async function match(pattern: fg.Pattern | fg.Pattern[]): Promise { 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[]; @@ -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 }