Skip to content

Commit

Permalink
fix(require-quotes-in-ports-rule): ensure single port values are wrap…
Browse files Browse the repository at this point in the history
…ped in quotes
  • Loading branch information
zavoloklom committed Nov 14, 2024
1 parent 74fa0e3 commit c9b9073
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/rules/require-quotes-in-ports-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
Expand Down
49 changes: 32 additions & 17 deletions tests/rules/require-quotes-in-ports-rule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -74,45 +89,45 @@ 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
test('RequireQuotesInPortsRule: should fix unquoted ports by adding double quotes and not modify already quoted ports', (t: ExecutionContext) => {
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
test('RequireQuotesInPortsRule: should fix single quotes ports by changing them to double quotes', (t: ExecutionContext) => {
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.');
});

0 comments on commit c9b9073

Please sign in to comment.