From c197886bee8590586cbf50cfd964423050e22eed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Cs=C3=A1nyi?= Date: Wed, 22 Sep 2021 15:44:10 +0200 Subject: [PATCH 1/4] stuff --- .../rulesets/.defaultsForSpectral.yaml | 14 ++++++ .../negative-case-multiple.yaml | 50 +++++++++++++++++++ .../negative-case.yaml | 29 +++++++++++ .../positive-case.yaml | 29 +++++++++++ ...mple-name-should-not-contain-space.test.js | 48 ++++++++++++++++++ 5 files changed, 170 insertions(+) create mode 100644 test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case-multiple.yaml create mode 100644 test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case.yaml create mode 100644 test/spectral/mockFiles/examples-name-should-not-contain-space/positive-case.yaml create mode 100644 test/spectral/tests/custom-rules/example-name-should-not-contain-space.test.js diff --git a/src/spectral/rulesets/.defaultsForSpectral.yaml b/src/spectral/rulesets/.defaultsForSpectral.yaml index f1fdeb32e..a279eaf08 100644 --- a/src/spectral/rulesets/.defaultsForSpectral.yaml +++ b/src/spectral/rulesets/.defaultsForSpectral.yaml @@ -244,3 +244,17 @@ rules: - $.servers[*][variables][*][default] then: function: truthy + + # ensure that examples name does not contain space + examples-name-should-not-contain-space: + description: 'Examples name should not contain space' + message: "{{description}}" + severity: warn + resolved: true + formats: ["oas3"] + given: + - $.paths[*][*].responses[*][*][*].examples + then: + function: pattern + notMatch: '*\\ *' + diff --git a/test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case-multiple.yaml b/test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case-multiple.yaml new file mode 100644 index 000000000..068510f86 --- /dev/null +++ b/test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case-multiple.yaml @@ -0,0 +1,50 @@ +openapi: 3.0.3 +info: + title: Examples name should not contain space + description: Examples name should not contain space + version: 1.0.0 +servers: + - url: 'https://example.com' +paths: + /v1/users: + get: + operationId: get_users + summary: returns user list + responses: + 200: + description: returns list of users + content: + 'application/json': + schema: + properties: + id: + type: string + examples: + success example: + summary: 'successful example' + value: 'success value' + failed_example: + summary: 'failed request' + value: 'failed request value' + /v1/user: + post: + operationId: create_user + summary: creates a user + responses: + 201: + description: creates a user + content: + 'application/json': + schema: + properties: + id: + type: string + examples: + success: + summary: 'success' + value: 'success' + fail: + summary: 'failed' + value: 'failed' + + diff --git a/test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case.yaml b/test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case.yaml new file mode 100644 index 000000000..bc5c6e6b8 --- /dev/null +++ b/test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case.yaml @@ -0,0 +1,29 @@ +openapi: 3.0.3 +info: + title: Examples name should not contain space + description: Examples name should not contain space + version: 1.0.0 +servers: + - url: 'https://example.com' +paths: + /v1/users: + get: + operationId: get_users + summary: returns user list + responses: + 200: + description: returns list of users + content: + 'application/json': + schema: + properties: + id: + type: string + examples: + success example: + summary: 'successful example' + value: 'success value' + failed example: + summary: 'failed request' + value: 'failed request value' + diff --git a/test/spectral/mockFiles/examples-name-should-not-contain-space/positive-case.yaml b/test/spectral/mockFiles/examples-name-should-not-contain-space/positive-case.yaml new file mode 100644 index 000000000..ce8303775 --- /dev/null +++ b/test/spectral/mockFiles/examples-name-should-not-contain-space/positive-case.yaml @@ -0,0 +1,29 @@ +openapi: 3.0.3 +info: + title: Examples name should not contain space + description: Examples name should not contain space + version: 1.0.0 +servers: + - url: 'https://example.com' +paths: + /v1/users: + get: + operationId: get_users + summary: returns user list + responses: + 200: + description: returns list of users + content: + 'application/json': + schema: + properties: + id: + type: string + examples: + success: + summary: 'successful example' + value: 'success value' + fail: + summary: 'failed request' + value: 'failed request value' + diff --git a/test/spectral/tests/custom-rules/example-name-should-not-contain-space.test.js b/test/spectral/tests/custom-rules/example-name-should-not-contain-space.test.js new file mode 100644 index 000000000..d14b15b07 --- /dev/null +++ b/test/spectral/tests/custom-rules/example-name-should-not-contain-space.test.js @@ -0,0 +1,48 @@ +const commandLineValidator = require('../../../../src/cli-validator/runValidator'); +const inCodeValidator = require('../../../../src/lib'); +const { getCapturedText } = require('../../../test-utils'); +const re = /^Validator: spectral/; + +describe('spectral - examples name should not contain space', function() { + + it('should not display error when examples name does not have space', async () => { + // const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); + const program = {}; + program.args = [ + './test/spectral/mockFiles/examples-name-should-not-contain-space/positive-case.yaml' + ]; + program.default_mode = true; + program.print_validator_modules = true; + + const exitCode = await commandLineValidator(program); + // const capturedText = getCapturedText(consoleSpy.mock.calls); + // const allOutput = capturedText.join(''); + // + // const validatorsText = allOutput.match(/Validator:\s\w.+/g) || []; + // // const foundOtherValidator = false; + // + // expect(validatorsText.length).toBeGreaterThan(0); + // expect(exitCode).toEqual(0); + }); + + // it('should display error when examples name contains space', async () => { + // const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); + // const program = {}; + // program.args = [ + // './test/mockFiles/examples-name-should-not-contain-space/negative-case.yaml' + // ]; + // program.defaul_mode = true; + // program.print_validatior_modules = true; + // + // const exitCode = await commandLineValidator(program); + // const capturedText = getCapturedText(consoleSpy.mock.calls); + // const allOutput = capturedText.join(''); + // + // const validatorsText = allOutput.match(/Validator:\s\w.+/g) || []; + // // const foundOtherValidator = false; + // + // expect(validatorsText.length).toBeGreaterThan(0); + // expect(exitCode).toEqual(0); + // }); + +}); From db499502f34ccccec35af269493bbc6b201f41b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Cs=C3=A1nyi?= Date: Thu, 23 Sep 2021 19:13:28 +0200 Subject: [PATCH 2/4] fix: add test cases and spectral rule --- .../rulesets/.defaultsForSpectral.yaml | 8 +-- .../negative-case-multiple.yaml | 50 ------------- ...mple-name-should-not-contain-space.test.js | 72 ++++++++++--------- ...get-message-and-path-from-captured-text.js | 25 +++++++ test/test-utils/index.js | 1 + 5 files changed, 69 insertions(+), 87 deletions(-) delete mode 100644 test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case-multiple.yaml create mode 100644 test/test-utils/get-message-and-path-from-captured-text.js diff --git a/src/spectral/rulesets/.defaultsForSpectral.yaml b/src/spectral/rulesets/.defaultsForSpectral.yaml index a279eaf08..07e21c688 100644 --- a/src/spectral/rulesets/.defaultsForSpectral.yaml +++ b/src/spectral/rulesets/.defaultsForSpectral.yaml @@ -244,17 +244,17 @@ rules: - $.servers[*][variables][*][default] then: function: truthy - # ensure that examples name does not contain space examples-name-should-not-contain-space: description: 'Examples name should not contain space' message: "{{description}}" severity: warn - resolved: true + resolved: false formats: ["oas3"] given: - - $.paths[*][*].responses[*][*][*].examples + - $.paths[*][*].responses[*][*][*].examples[*]~ then: function: pattern - notMatch: '*\\ *' + functionOptions: + notMatch: '[a-zA-Z0-9]\ +[a-zA-Z0-9]' diff --git a/test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case-multiple.yaml b/test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case-multiple.yaml deleted file mode 100644 index 068510f86..000000000 --- a/test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case-multiple.yaml +++ /dev/null @@ -1,50 +0,0 @@ -openapi: 3.0.3 -info: - title: Examples name should not contain space - description: Examples name should not contain space - version: 1.0.0 -servers: - - url: 'https://example.com' -paths: - /v1/users: - get: - operationId: get_users - summary: returns user list - responses: - 200: - description: returns list of users - content: - 'application/json': - schema: - properties: - id: - type: string - examples: - success example: - summary: 'successful example' - value: 'success value' - failed_example: - summary: 'failed request' - value: 'failed request value' - /v1/user: - post: - operationId: create_user - summary: creates a user - responses: - 201: - description: creates a user - content: - 'application/json': - schema: - properties: - id: - type: string - examples: - success: - summary: 'success' - value: 'success' - fail: - summary: 'failed' - value: 'failed' - - diff --git a/test/spectral/tests/custom-rules/example-name-should-not-contain-space.test.js b/test/spectral/tests/custom-rules/example-name-should-not-contain-space.test.js index d14b15b07..4087b9f73 100644 --- a/test/spectral/tests/custom-rules/example-name-should-not-contain-space.test.js +++ b/test/spectral/tests/custom-rules/example-name-should-not-contain-space.test.js @@ -1,12 +1,10 @@ const commandLineValidator = require('../../../../src/cli-validator/runValidator'); -const inCodeValidator = require('../../../../src/lib'); const { getCapturedText } = require('../../../test-utils'); -const re = /^Validator: spectral/; +const { getMessageAndPathFromCapturedText } = require('../../../test-utils'); -describe('spectral - examples name should not contain space', function() { - - it('should not display error when examples name does not have space', async () => { - // const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); +describe('spectral - examples name should not contain space', () => { + it('should not display error when examples name does not contain space', async () => { + const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); const program = {}; program.args = [ './test/spectral/mockFiles/examples-name-should-not-contain-space/positive-case.yaml' @@ -15,34 +13,42 @@ describe('spectral - examples name should not contain space', function() { program.print_validator_modules = true; const exitCode = await commandLineValidator(program); - // const capturedText = getCapturedText(consoleSpy.mock.calls); - // const allOutput = capturedText.join(''); - // - // const validatorsText = allOutput.match(/Validator:\s\w.+/g) || []; - // // const foundOtherValidator = false; - // - // expect(validatorsText.length).toBeGreaterThan(0); - // expect(exitCode).toEqual(0); + const capturedText = getCapturedText(consoleSpy.mock.calls); + + const messages = getMessageAndPathFromCapturedText( + 'Examples name should not contain space', + capturedText + ); + + expect(messages.length).toEqual(0); + + expect(exitCode).toEqual(0); }); - // it('should display error when examples name contains space', async () => { - // const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); - // const program = {}; - // program.args = [ - // './test/mockFiles/examples-name-should-not-contain-space/negative-case.yaml' - // ]; - // program.defaul_mode = true; - // program.print_validatior_modules = true; - // - // const exitCode = await commandLineValidator(program); - // const capturedText = getCapturedText(consoleSpy.mock.calls); - // const allOutput = capturedText.join(''); - // - // const validatorsText = allOutput.match(/Validator:\s\w.+/g) || []; - // // const foundOtherValidator = false; - // - // expect(validatorsText.length).toBeGreaterThan(0); - // expect(exitCode).toEqual(0); - // }); + it('should display error when multiple examples names contain space', async () => { + const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); + const program = {}; + program.args = [ + './test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case.yaml' + ]; + program.default_mode = true; + program.print_validator_modules = true; + + const exitCode = await commandLineValidator(program); + const capturedText = getCapturedText(consoleSpy.mock.calls); + const messages = getMessageAndPathFromCapturedText( + 'Examples name should not contain space', + capturedText + ); + + expect(messages[0][1].get('Path')).toEqual( + 'paths./v1/users.get.responses.200.content.application/json.examples.success example' + ); + expect(messages[1][1].get('Path')).toEqual( + 'paths./v1/users.get.responses.200.content.application/json.examples.failed example' + ); + + expect(exitCode).toEqual(0); + }); }); diff --git a/test/test-utils/get-message-and-path-from-captured-text.js b/test/test-utils/get-message-and-path-from-captured-text.js new file mode 100644 index 000000000..48151bf61 --- /dev/null +++ b/test/test-utils/get-message-and-path-from-captured-text.js @@ -0,0 +1,25 @@ +module.exports.getMessageAndPathFromCapturedText = getMessageAndPathFromCapturedText; + +function getMessageAndPathFromCapturedText(pattern, capturedText) { + const messages = []; + for (let i = 0; i < capturedText.length; i++) { + if (capturedText[i].includes(pattern)) { + const aMessage = []; + + const messageMap = new Map(); + const messageSplit = capturedText[i].split(':'); + messageMap.set(messageSplit[0].trim(), messageSplit[1].trim()); + aMessage.push(messageMap); + + const pathMap = new Map(); + const pathSplit = capturedText[i + 1].split(':'); + pathMap.set(pathSplit[0].trim(), pathSplit[1].trim()); + aMessage.push(pathMap); + + messages.push(aMessage); + // we jump the index by one due to that i+1 entry is already processed + i++; + } + } + return messages; +} diff --git a/test/test-utils/index.js b/test/test-utils/index.js index c9477cf0b..c66269d75 100644 --- a/test/test-utils/index.js +++ b/test/test-utils/index.js @@ -1,2 +1,3 @@ module.exports.getCapturedText = require('./get-captured-text').getCapturedText; module.exports.getCapturedTextWithColor = require('./get-captured-text').getCapturedTextWithColor; +module.exports.getMessageAndPathFromCapturedText = require('./get-message-and-path-from-captured-text').getMessageAndPathFromCapturedText; From 516e3db62b007a0bac4f6fc57939f146037abf27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Cs=C3=A1nyi?= Date: Wed, 29 Sep 2021 16:34:45 +0200 Subject: [PATCH 3/4] fix: rename rule --- src/spectral/rulesets/.defaultsForSpectral.yaml | 2 +- ...ntain-space.test.js => examples-name-contains-space.test.js} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename test/spectral/tests/custom-rules/{example-name-should-not-contain-space.test.js => examples-name-contains-space.test.js} (100%) diff --git a/src/spectral/rulesets/.defaultsForSpectral.yaml b/src/spectral/rulesets/.defaultsForSpectral.yaml index 07e21c688..db92807a9 100644 --- a/src/spectral/rulesets/.defaultsForSpectral.yaml +++ b/src/spectral/rulesets/.defaultsForSpectral.yaml @@ -245,7 +245,7 @@ rules: then: function: truthy # ensure that examples name does not contain space - examples-name-should-not-contain-space: + examples-name-contains-space: description: 'Examples name should not contain space' message: "{{description}}" severity: warn diff --git a/test/spectral/tests/custom-rules/example-name-should-not-contain-space.test.js b/test/spectral/tests/custom-rules/examples-name-contains-space.test.js similarity index 100% rename from test/spectral/tests/custom-rules/example-name-should-not-contain-space.test.js rename to test/spectral/tests/custom-rules/examples-name-contains-space.test.js From fd802755a56d3cd62d41ab99a1a2f40638e7c571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Cs=C3=A1nyi?= Date: Wed, 29 Sep 2021 16:37:21 +0200 Subject: [PATCH 4/4] fix: use more precise regexp --- src/spectral/rulesets/.defaultsForSpectral.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spectral/rulesets/.defaultsForSpectral.yaml b/src/spectral/rulesets/.defaultsForSpectral.yaml index db92807a9..2cbf1ca27 100644 --- a/src/spectral/rulesets/.defaultsForSpectral.yaml +++ b/src/spectral/rulesets/.defaultsForSpectral.yaml @@ -256,5 +256,5 @@ rules: then: function: pattern functionOptions: - notMatch: '[a-zA-Z0-9]\ +[a-zA-Z0-9]' + notMatch: '^(.*\s+.*)+$'