From 290618103f22a94aec1ab587e0b7c7f183f9a53e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Mon, 2 May 2022 23:55:51 +0200 Subject: [PATCH 1/2] feat(cli): sort linting results alphabetically --- .../invalid-stoplight-info-document.json | 5 ++++ .../missing-stoplight-info-document-copy.json | 2 +- .../missing-stoplight-info-document.json | 2 +- .../cli/src/services/__tests__/linter.test.ts | 25 ++++++++++++++++++- .../src/services/linter/utils/listFiles.ts | 4 ++- 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 packages/cli/src/services/__tests__/__fixtures__/invalid-stoplight-info-document.json 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__/__fixtures__/missing-stoplight-info-document-copy.json b/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document-copy.json index 1a71c21b7..11fb16299 100644 --- a/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document-copy.json +++ b/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document-copy.json @@ -1,5 +1,5 @@ { "info": { - "title": "no stoplight :(" + "title": "" } } diff --git a/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document.json b/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document.json index 1a71c21b7..11fb16299 100644 --- a/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document.json +++ b/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document.json @@ -1,5 +1,5 @@ { "info": { - "title": "no stoplight :(" + "title": "" } } 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 } From 1d438e267f1836f9af96941d9249967fcae5f64a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Tue, 3 May 2022 00:08:53 +0200 Subject: [PATCH 2/2] revert(cli): test changes --- .../__fixtures__/missing-stoplight-info-document-copy.json | 2 +- .../__tests__/__fixtures__/missing-stoplight-info-document.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document-copy.json b/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document-copy.json index 11fb16299..1a71c21b7 100644 --- a/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document-copy.json +++ b/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document-copy.json @@ -1,5 +1,5 @@ { "info": { - "title": "" + "title": "no stoplight :(" } } diff --git a/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document.json b/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document.json index 11fb16299..1a71c21b7 100644 --- a/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document.json +++ b/packages/cli/src/services/__tests__/__fixtures__/missing-stoplight-info-document.json @@ -1,5 +1,5 @@ { "info": { - "title": "" + "title": "no stoplight :(" } }