Skip to content

Commit

Permalink
fix: resolve error "key already set" in Service Keys Order Rule
Browse files Browse the repository at this point in the history
ref: #9
  • Loading branch information
zavoloklom committed Sep 20, 2024
1 parent 2687b8b commit 336723d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/rules/service-keys-order-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class ServiceKeysOrderRule implements LintRule {
private getCorrectOrder(keys: string[]): string[] {
const otherKeys = keys.filter((key) => !Object.values(this.groups).flat().includes(key)).sort();

return this.groupOrder.flatMap((group) => this.groups[group].concat(otherKeys));
return this.groupOrder.flatMap((group) => this.groups[group]).concat(otherKeys);
}

public check(context: LintContext): LintMessage[] {
Expand Down
22 changes: 19 additions & 3 deletions tests/rules/service-keys-order-rule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ const yamlWithIncorrectOrder = `
services:
web:
image: nginx
annotations:
- com.example.foo=bar
ports:
- 80:80
environment:
- NODE_ENV=production
volumes:
- ./data:/data
cpu_rt_runtime: '400ms'
cpu_rt_period: '1400us'
`;

const yamlWithCorrectOrder = `
Expand All @@ -26,6 +30,10 @@ services:
- NODE_ENV=production
ports:
- 80:80
annotations:
- com.example.foo=bar
cpu_rt_period: '1400us'
cpu_rt_runtime: '400ms'
`;

// Helper function to strip spaces and normalize strings for comparison
Expand All @@ -40,10 +48,18 @@ test('ServiceKeysOrderRule: should return a warning when service keys are in the
};

const errors = rule.check(context);
t.is(errors.length, 2, 'There should be two warnings when service keys are out of order.');
t.is(errors.length, 4, 'There should be two warnings when service keys are out of order.');

t.true(errors[0].message.includes(`Key "environment" in service "web" is out of order.`));
t.true(errors[1].message.includes(`Key "volumes" in service "web" is out of order.`));
const expectedMessages = [
'Key "ports" in service "web" is out of order.',
'Key "environment" in service "web" is out of order.',
'Key "volumes" in service "web" is out of order.',
'Key "cpu_rt_period" in service "web" is out of order.',
];

errors.forEach((error, index) => {
t.true(error.message.includes(expectedMessages[index]));
});
});

test('ServiceKeysOrderRule: should not return warnings when service keys are in the correct order', (t) => {
Expand Down

0 comments on commit 336723d

Please sign in to comment.