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

chore: extract a result sorting function #737

Merged
merged 1 commit into from
Nov 20, 2019
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
28 changes: 14 additions & 14 deletions src/cli/services/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,13 @@ describe('Linter service', () => {
range: expect.any(Object),
source: expect.stringContaining('__tests__/__fixtures__/draft-ref.oas2.json'),
}),
expect.objectContaining({
code: 'openapi-tags',
message: 'OpenAPI object should have non-empty `tags` array.',
path: [],
range: expect.any(Object),
source: expect.stringContaining('__tests__/__fixtures__/draft-ref.oas2.json'),
}),
expect.objectContaining({
code: 'oas2-schema',
message: '/info Property foo is not expected to be here',
Expand Down Expand Up @@ -595,13 +602,6 @@ describe('Linter service', () => {
},
source: expect.stringContaining('__tests__/__fixtures__/refs/info.json'),
}),
expect.objectContaining({
code: 'openapi-tags',
message: 'OpenAPI object should have non-empty `tags` array.',
path: [],
range: expect.any(Object),
source: expect.stringContaining('__tests__/__fixtures__/draft-ref.oas2.json'),
}),
]);
});

Expand All @@ -614,6 +614,13 @@ describe('Linter service', () => {
range: expect.any(Object),
source: expect.stringContaining('__tests__/__fixtures__/draft-nested-ref.oas2.json'),
}),
expect.objectContaining({
code: 'openapi-tags',
message: 'OpenAPI object should have non-empty `tags` array.',
path: [],
range: expect.any(Object),
source: expect.stringContaining('__tests__/__fixtures__/draft-nested-ref.oas2.json'),
}),
expect.objectContaining({
code: 'oas2-schema',
message: "/info should have required property 'title'",
Expand Down Expand Up @@ -646,13 +653,6 @@ describe('Linter service', () => {
},
source: expect.stringContaining('__tests__/__fixtures__/refs/contact.json'),
}),
expect.objectContaining({
code: 'openapi-tags',
message: 'OpenAPI object should have non-empty `tags` array.',
path: [],
range: expect.any(Object),
source: expect.stringContaining('__tests__/__fixtures__/draft-nested-ref.oas2.json'),
}),
expect.objectContaining({
code: 'operation-description',
message: 'Operation `description` must be present and non-empty string.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ describe('deduplicateResults util', () => {
it('deduplicate exact validation results', () => {
expect(deduplicateResults(duplicateValidationResults)).toEqual([
expect.objectContaining({
code: 'valid-schema-example-in-content',
code: 'valid-example-in-schemas',
}),
expect.objectContaining({
code: 'valid-example-in-schemas',
code: 'valid-schema-example-in-content',
}),
]);
});
Expand All @@ -22,11 +22,22 @@ describe('deduplicateResults util', () => {

expect(deduplicateResults(duplicateValidationResultsWithNoSource)).toEqual([
expect.objectContaining({
code: 'valid-schema-example-in-content',
code: 'valid-example-in-schemas',
}),
expect.objectContaining({
code: 'valid-example-in-schemas',
code: 'valid-schema-example-in-content',
}),
]);
});

it('deduplicate list of only duplicates', () => {
const onlyDuplicates = [
{ ...duplicateValidationResults[0] },
{ ...duplicateValidationResults[0] },
{ ...duplicateValidationResults[0] },
{ ...duplicateValidationResults[0] },
];

expect(deduplicateResults(onlyDuplicates).length).toBe(1);
});
});
34 changes: 17 additions & 17 deletions src/cli/services/linter/utils/deduplicateResults.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { IRange } from '@stoplight/types/dist';
import { compareResults } from '../../../../formatters/utils/sortResults';
import { IRuleResult } from '../../../../types';

type Dictionary<T, K extends PropertyKey> = { [key in K]: T };
export const deduplicateResults = (results: IRuleResult[]): IRuleResult[] => {
const filtered: IRuleResult[] = [];

const ARTIFICIAL_ROOT = Symbol('root');
const totalResults = results.length;

const serializeRange = ({ start, end }: IRange) => `${start.line}:${start.character}:${end.line}:${end.character}`;
const getIdentifier = (result: IRuleResult) => `${result.path.join('/')}${result.code}${serializeRange(result.range)}`;
if (totalResults < 2) {
return [...results];
}

export const deduplicateResults = (results: IRuleResult[]) => {
const seen: Dictionary<Dictionary<string, string>, symbol> = {};
const sorted = [...results].sort(compareResults);

return results.filter(result => {
const source = result.source === void 0 ? ARTIFICIAL_ROOT : result.source;
const identifier = getIdentifier(result);
if (!(source in seen)) {
seen[source] = {};
} else if (identifier in seen[source]) {
return false;
filtered.push(sorted[0]);

for (let i = 1; i < totalResults; i++) {
if (compareResults(sorted[i], sorted[i - 1]) === 0) {
continue;
}

seen[source][identifier] = true;
return true;
});
filtered.push(sorted[i]);
}

return filtered;
};
206 changes: 206 additions & 0 deletions src/formatters/__tests__/sortResults.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { IRuleResult } from '../../types';
import { compareResults, sortResults } from '../utils/sortResults';

const results: IRuleResult[] = [
{
code: 'code 01',
path: ['a', 'b', 'c', 'd'],
source: 'source 01',
range: {
start: { line: 1, character: 1 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
{
code: 'code 01',
path: ['a', 'b', 'c', 'd'],
source: 'source 02',
range: {
start: { line: 1, character: 1 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
{
code: 'code 01',
path: ['a', 'b', 'c', 'd'],
source: 'source 02',
range: {
start: { line: 2, character: 1 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
{
code: 'code 01',
path: ['a', 'b', 'c', 'd'],
source: 'source 02',
range: {
start: { line: 2, character: 2 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
{
code: 'code 02',
path: ['a', 'b', 'c', 'd'],
source: 'source 02',
range: {
start: { line: 2, character: 2 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
{
code: 'code 02',
path: ['a', 'b', 'c', 'e'],
source: 'source 02',
range: {
start: { line: 2, character: 2 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
{
code: 'code 02',
path: ['a', 'b', 'c', 'f'],
source: 'source 02',
range: {
start: { line: 2, character: 2 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
{
code: 'code 03',
path: ['a', 'b', 'c', 'f'],
source: 'source 02',
range: {
start: { line: 2, character: 2 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
{
code: 'code 03',
path: ['a', 'b', 'c', 'f'],
source: 'source 02',
range: {
start: { line: 2, character: 3 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
{
code: 'code 03',
path: ['a', 'b', 'c', 'f'],
source: 'source 02',
range: {
start: { line: 3, character: 3 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
{
code: 'code 03',
path: ['a', 'b', 'c', 'f'],
source: 'source 03',
range: {
start: { line: 3, character: 3 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
},
];

describe('sortResults', () => {
const shuffleBy = (arr: IRuleResult[], indices: number[]): IRuleResult[] => {
expect(indices).toHaveLength(arr.length);

const shuffled = results
.map((v, i) => ({ ...v, pos: indices[i] }))
.sort((a, b) => a.pos - b.pos)
.map((v, i) => {
delete v.pos;
return v;
});

return shuffled;
};

test('should properly order results', () => {
const randomlySortedIndices = [5, 4, 1, 10, 8, 6, 3, 9, 2, 0, 7];

const shuffled = shuffleBy(results, randomlySortedIndices);

expect(sortResults(shuffled)).toEqual(results);
});
});

describe('compareResults', () => {
test('should properly order results source', () => {
const input = {
code: 'code 01',
path: ['a', 'b', 'c', 'd'],
range: {
start: { line: 1, character: 1 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
};

[
{ one: undefined, another: undefined, expected: 0 },
{ one: 'a', another: undefined, expected: 1 },
{ one: undefined, another: 'a', expected: -1 },
{ one: 'a', another: 'a', expected: 0 },
{ one: 'a', another: 'b', expected: -1 },
].forEach(tc => {
expect(compareResults({ ...input, source: tc.one }, { ...input, source: tc.another })).toEqual(tc.expected);
});
});

test('should properly order results code', () => {
const input = {
source: 'somewhere',
path: ['a', 'b', 'c', 'd'],
range: {
start: { line: 1, character: 1 },
end: { line: 99, character: 99 },
},
message: '99',
severity: DiagnosticSeverity.Error, // or any other level, it's irrelevant
};

[
{ one: undefined, another: undefined, expected: 0 },
{ one: 'a', another: undefined, expected: 1 },
{ one: undefined, another: 'a', expected: -1 },
{ one: 'a', another: 'a', expected: 0 },
{ one: 'a', another: 'b', expected: -1 },
{ one: '2', another: '10', expected: -1 },
{ one: 1, another: undefined, expected: 1 },
{ one: undefined, another: 1, expected: -1 },
{ one: 1, another: 1, expected: 0 },
{ one: 1, another: 2, expected: -1 },
{ one: 1, another: '1', expected: 0 },
{ one: 2, another: '10', expected: -1 },
].forEach(tc => {
expect(compareResults({ ...input, code: tc.one }, { ...input, code: tc.another })).toEqual(tc.expected);
});
});
});
Loading