Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve error "key already set" in Service Keys Order Rule #11

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading