From 1fe7a1997fec6d14d85ee13e9d0790f6f1a0e07d Mon Sep 17 00:00:00 2001 From: Sergey Kupletsky Date: Tue, 12 Nov 2024 15:32:52 +0100 Subject: [PATCH] fix(require-quotes-in-ports-rule): ensure single port values are wrapped in quotes --- src/rules/require-quotes-in-ports-rule.ts | 5 +- .../require-quotes-in-ports-rule.spec.ts | 49 ++++++++++++------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/rules/require-quotes-in-ports-rule.ts b/src/rules/require-quotes-in-ports-rule.ts index 8877d51..09304c6 100644 --- a/src/rules/require-quotes-in-ports-rule.ts +++ b/src/rules/require-quotes-in-ports-rule.ts @@ -112,8 +112,9 @@ export default class RequireQuotesInPortsRule implements LintRule { this.portsSections.forEach((section) => { RequireQuotesInPortsRule.extractValues(parsedDocument.contents, section, (service, port) => { if (port.type !== this.getQuoteType()) { - // eslint-disable-next-line no-param-reassign - port.type = this.getQuoteType(); + const newPort = new Scalar(String(port.value)); + newPort.type = this.getQuoteType(); + Object.assign(port, newPort); } }); }); diff --git a/tests/rules/require-quotes-in-ports-rule.spec.ts b/tests/rules/require-quotes-in-ports-rule.spec.ts index 95d5ae5..1ebb9c2 100644 --- a/tests/rules/require-quotes-in-ports-rule.spec.ts +++ b/tests/rules/require-quotes-in-ports-rule.spec.ts @@ -8,23 +8,35 @@ const yamlWithoutQuotes = ` services: web: ports: + - 80 - 8080:80 + expose: + - 3000 `; const yamlWithSingleQuotes = ` services: web: ports: + - '80' - '8080:80' + expose: + - '3000' `; const yamlWithDoubleQuotes = ` services: web: ports: + - "80" - "8080:80" + expose: + - "3000" `; +// Helper function to normalize YAML +const normalizeYAML = (yaml: string) => yaml.replaceAll(/\s+/g, ' ').trim(); + const pathToFile = '/docker-compose.yml'; // @ts-ignore TS2349 @@ -37,10 +49,13 @@ test('RequireQuotesInPortsRule: should return a warning when ports are not quote }; const errors = rule.check(context); - t.is(errors.length, 1, 'There should be one warning when ports are not quoted.'); - t.is(errors[0].message, 'Ports in `ports` and `expose` sections should be enclosed in quotes.'); - t.is(errors[0].rule, 'require-quotes-in-ports'); - t.is(errors[0].severity, 'minor'); + t.is(errors.length, 3, 'There should be one warning when ports are not quoted.'); + + const expectedMessage = 'Ports in `ports` and `expose` sections should be enclosed in quotes.'; + + errors.forEach((error, index) => { + t.true(error.message.includes(expectedMessage)); + }); }); // @ts-ignore TS2349 @@ -74,23 +89,23 @@ test('RequireQuotesInPortsRule: should fix unquoted ports by adding single quote const rule = new RequireQuotesInPortsRule({ quoteType: 'single' }); const fixedYAML = rule.fix(yamlWithoutQuotes); - t.true( - fixedYAML.includes(`'8080:80'`), + t.is( + normalizeYAML(fixedYAML), + normalizeYAML(yamlWithSingleQuotes), 'The Ports in `ports` and `expose` sections should be quoted with single quotes.', ); - t.false(fixedYAML.includes('ports:\n - 8080:80'), 'The unquoted ports should no longer exist.'); }); // @ts-ignore TS2349 test('RequireQuotesInPortsRule: should fix double quotes ports by changing them to single quotes', (t: ExecutionContext) => { const rule = new RequireQuotesInPortsRule({ quoteType: 'single' }); - const fixedYAML = rule.fix(yamlWithSingleQuotes); - t.true( - fixedYAML.includes(`'8080:80'`), + const fixedYAML = rule.fix(yamlWithDoubleQuotes); + t.is( + normalizeYAML(fixedYAML), + normalizeYAML(yamlWithSingleQuotes), 'The Ports in `ports` and `expose` sections should be quoted with single quotes.', ); - t.false(fixedYAML.includes(`"8080:80"`), 'The ports should not have double quotes.'); }); // @ts-ignore TS2349 @@ -98,11 +113,11 @@ test('RequireQuotesInPortsRule: should fix unquoted ports by adding double quote const rule = new RequireQuotesInPortsRule({ quoteType: 'double' }); const fixedYAML = rule.fix(yamlWithoutQuotes); - t.true( - fixedYAML.includes(`"8080:80"`), + t.is( + normalizeYAML(fixedYAML), + normalizeYAML(yamlWithDoubleQuotes), 'The Ports in `ports` and `expose` sections should be quoted with double quotes.', ); - t.false(fixedYAML.includes('ports:\n - 8080:80'), 'The unquoted ports should no longer exist.'); }); // @ts-ignore TS2349 @@ -110,9 +125,9 @@ test('RequireQuotesInPortsRule: should fix single quotes ports by changing them const rule = new RequireQuotesInPortsRule({ quoteType: 'double' }); const fixedYAML = rule.fix(yamlWithSingleQuotes); - t.true( - fixedYAML.includes(`"8080:80"`), + t.is( + normalizeYAML(fixedYAML), + normalizeYAML(yamlWithDoubleQuotes), 'The Ports in `ports` and `expose` sections should be quoted with double quotes.', ); - t.false(fixedYAML.includes(`'8080:80'`), 'The ports should not have single quotes.'); });