Skip to content

Commit

Permalink
chore(formatters): avoid a breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Sep 14, 2023
1 parent 5d8c06e commit f20cfd3
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 136 deletions.
19 changes: 0 additions & 19 deletions packages/formatters/src/__tests__/__fixtures__/sairf-rules.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const results: IRuleResult[] = [

describe('GitHub Actions formatter', () => {
test('should be formatted correctly', () => {
expect(githubActions(results, { failSeverity: DiagnosticSeverity.Error }, null).split('\n')).toEqual([
expect(githubActions(results, { failSeverity: DiagnosticSeverity.Error }).split('\n')).toEqual([
'::warning title=operation-description,file=__tests__/fixtures/petstore.oas2.yaml,col=9,endColumn=61,line=61,endLine=72::paths./pets.get.description is not truthy%0AMessage can have%0Amultiple lines',
'::warning title=operation-tags,file=__tests__/fixtures/petstore.oas2.yaml,col=9,endColumn=61,line=61,endLine=72::paths./pets.get.tags is not truthy',
]);
Expand Down
2 changes: 1 addition & 1 deletion packages/formatters/src/__tests__/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import mixedErrors from './__fixtures__/mixed-errors.json';

describe('HTML formatter', () => {
test('should display proper severity levels', () => {
const result = parse(html(mixedErrors, { failSeverity: DiagnosticSeverity.Error }, null));
const result = parse(html(mixedErrors, { failSeverity: DiagnosticSeverity.Error }));
const table = result.querySelector('table tbody');
expect(table.innerHTML.trim()).toEqual(`<tr class="bg-error" data-group="f-0">
<th colspan="4">
Expand Down
4 changes: 2 additions & 2 deletions packages/formatters/src/__tests__/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const results: IRuleResult[] = [

describe('JSON formatter', () => {
test('should include ranges', () => {
expect(JSON.parse(json(results, { failSeverity: DiagnosticSeverity.Error }, null))).toEqual([
expect(JSON.parse(json(results, { failSeverity: DiagnosticSeverity.Error }))).toEqual([
expect.objectContaining({
range: {
start: {
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('JSON formatter', () => {
});

test('should include message', () => {
expect(JSON.parse(json(results, { failSeverity: DiagnosticSeverity.Error }, null))).toEqual([
expect(JSON.parse(json(results, { failSeverity: DiagnosticSeverity.Error }))).toEqual([
expect.objectContaining({
message: 'paths./pets.get.description is not truthy',
}),
Expand Down
8 changes: 4 additions & 4 deletions packages/formatters/src/__tests__/junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('JUnit formatter', () => {
});

test('should produce valid report', async () => {
const result = await parse(junit(oas3SchemaErrors, { failSeverity: DiagnosticSeverity.Error }, null));
const result = await parse(junit(oas3SchemaErrors, { failSeverity: DiagnosticSeverity.Error }));
expect(result).toEqual({
testsuites: {
testsuite: [
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('JUnit formatter', () => {
});

test('given failSeverity set to error, should filter out non-error validation results', async () => {
const result = await parse(junit(mixedErrors, { failSeverity: DiagnosticSeverity.Error }, null));
const result = await parse(junit(mixedErrors, { failSeverity: DiagnosticSeverity.Error }));
expect(result).toEqual({
testsuites: {
testsuite: [
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('JUnit formatter', () => {
});

test('given failSeverity set to other value than error, should filter treat all validation results matching the severity as errors', async () => {
const result = await parse(junit(mixedErrors, { failSeverity: DiagnosticSeverity.Warning }, null));
const result = await parse(junit(mixedErrors, { failSeverity: DiagnosticSeverity.Warning }));
expect(result).toEqual({
testsuites: {
testsuite: [
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('JUnit formatter', () => {
});

test('handles special XML strings properly', async () => {
const result = await parse(junit(specialXmlStrings, { failSeverity: DiagnosticSeverity.Error }, null));
const result = await parse(junit(specialXmlStrings, { failSeverity: DiagnosticSeverity.Error }));
expect(result).toEqual({
testsuites: {
testsuite: [
Expand Down
2 changes: 1 addition & 1 deletion packages/formatters/src/__tests__/pretty.jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function forceWrapped(s: string, wrapType: number): string {
describe('Pretty formatter', () => {
test('should not wrap when terminal width is wide enough', () => {
setColumnWidth(185, function (): void {
const result = pretty(oas3SchemaErrors, { failSeverity: DiagnosticSeverity.Error }, null);
const result = pretty(oas3SchemaErrors, { failSeverity: DiagnosticSeverity.Error });
expect(result).toContain(
`${chalk.red('36:22')} ${chalk.red.inverse('ERROR')} ${chalk.red.bold(
'oas3-schema',
Expand Down
184 changes: 108 additions & 76 deletions packages/formatters/src/__tests__/sarif.jest.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { DiagnosticSeverity } from '@stoplight/types';
import type { IRuleResult } from '@stoplight/spectral-core';
import { sarif, sarifToolVersion } from '../sarif';
import { getRuleset } from '@stoplight/spectral-cli/src/services/linter/utils';
import { Ruleset } from '@stoplight/spectral-core';
import { sarif } from '../sarif';

const cwd = process.cwd();
const results: IRuleResult[] = [
{
code: 'operation-description',
message: 'paths./pets.get.description is not truthy\nMessages can differ from the rule description',
path: ['paths', '/pets', 'get', 'description'],
severity: 1,
severity: DiagnosticSeverity.Warning,
source: `${cwd}/__tests__/fixtures/petstore.oas2.yaml`,
range: {
start: {
Expand All @@ -26,7 +26,7 @@ const results: IRuleResult[] = [
code: 'operation-tags',
message: 'paths./pets.get.tags is not truthy',
path: ['paths', '/pets', 'get', 'tags'],
severity: 0,
severity: DiagnosticSeverity.Error,
source: `${cwd}/__tests__/fixtures/petstore.oas2.yaml`,
range: {
start: {
Expand All @@ -43,97 +43,129 @@ const results: IRuleResult[] = [

describe('Sarif formatter', () => {
test('should be formatted correctly', async () => {
const ruleset = await getRuleset(`${__dirname}/__fixtures__/sairf-rules.yml`);
const output = sarif(results, { failSeverity: DiagnosticSeverity.Error }, ruleset);
const sarifToolVersion = '6.11';
const ruleset = new Ruleset({
rules: {
'operation-description': {
description: 'paths./pets.get.description is not truthy',
message: 'paths./pets.get.description is not truthy\nMessages can differ from the rule description',
severity: DiagnosticSeverity.Error,
given: '$.paths[*][*]',
then: {
field: 'description',
function: function truthy() {
return false;
},
},
},
'operation-tags': {
description: 'paths./pets.get.tags is not truthy',
message: 'paths./pets.get.tags is not truthy\nMessages can differ from the rule description',
severity: DiagnosticSeverity.Error,
given: '$.paths[*][*]',
then: {
field: 'description',
function: function truthy() {
return false;
},
},
},
},
});

const output = sarif(
results,
{ failSeverity: DiagnosticSeverity.Error },
{ ruleset, spectralVersion: sarifToolVersion },
);

const outputObject = JSON.parse(output);
const expectedObject = JSON.parse(`
{
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.6.json",
"version": "2.1.0",
"runs": [
expect(outputObject).toStrictEqual({
$schema: 'http://json.schemastore.org/sarif-2.1.0-rtm.6.json',
version: '2.1.0',
runs: [
{
"tool": {
"driver": {
"name": "spectral",
"rules": [
tool: {
driver: {
name: 'spectral',
rules: [
{
"id": "operation-description",
"shortDescription": {
"text": "paths./pets.get.description is not truthy"
}
id: 'operation-description',
shortDescription: {
text: 'paths./pets.get.description is not truthy',
},
},
{
"id": "operation-tags",
"shortDescription": {
"text": "paths./pets.get.tags is not truthy"
}
}
id: 'operation-tags',
shortDescription: {
text: 'paths./pets.get.tags is not truthy',
},
},
],
"version": "${sarifToolVersion}",
"informationUri": "https://github.com/stoplightio/spectral"
}
version: sarifToolVersion,
informationUri: 'https://github.com/stoplightio/spectral',
},
},
"results": [
results: [
{
"level": "warning",
"message": {
"text": "paths./pets.get.description is not truthy\\nMessages can differ from the rule description"
level: 'warning',
message: {
text: 'paths./pets.get.description is not truthy\nMessages can differ from the rule description',
},
"ruleId": "operation-description",
"locations": [
ruleId: 'operation-description',
locations: [
{
"physicalLocation": {
"artifactLocation": {
"uri": "__tests__/fixtures/petstore.oas2.yaml",
"index": 0
physicalLocation: {
artifactLocation: {
uri: '__tests__/fixtures/petstore.oas2.yaml',
index: 0,
},
"region": {
"startLine": 61,
"startColumn": 8,
"endLine": 72,
"endColumn": 60
}
}
}
region: {
startLine: 61,
startColumn: 9,
endLine: 72,
endColumn: 61,
},
},
},
],
"ruleIndex": 0
ruleIndex: 0,
},
{
"level": "error",
"message": {
"text": "paths./pets.get.tags is not truthy"
level: 'error',
message: {
text: 'paths./pets.get.tags is not truthy',
},
"ruleId": "operation-tags",
"locations": [
ruleId: 'operation-tags',
locations: [
{
"physicalLocation": {
"artifactLocation": {
"uri": "__tests__/fixtures/petstore.oas2.yaml",
"index": 0
physicalLocation: {
artifactLocation: {
uri: '__tests__/fixtures/petstore.oas2.yaml',
index: 0,
},
"region": {
"startLine": 61,
"startColumn": 8,
"endLine": 72,
"endColumn": 60
}
}
}
region: {
startLine: 61,
startColumn: 9,
endLine: 72,
endColumn: 61,
},
},
},
],
"ruleIndex": 1
}
ruleIndex: 1,
},
],
"artifacts": [
artifacts: [
{
"sourceLanguage": "YAML",
"location": {
"uri": "__tests__/fixtures/petstore.oas2.yaml"
}
}
]
}
]
}`);
expect(outputObject).toEqual(expectedObject);
sourceLanguage: 'YAML',
location: {
uri: '__tests__/fixtures/petstore.oas2.yaml',
},
},
],
},
],
});
});
});
4 changes: 2 additions & 2 deletions packages/formatters/src/__tests__/stylish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import mixedErrors from './__fixtures__/mixed-errors.json';

describe('Stylish formatter', () => {
test('should prefer message for oas-schema errors', () => {
const result = stylish(oas3SchemaErrors, { failSeverity: DiagnosticSeverity.Error }, null);
const result = stylish(oas3SchemaErrors, { failSeverity: DiagnosticSeverity.Error });
expect(result).toContain('oas3-schema should NOT have additional properties: type');
expect(result).toContain('oas3-schema should match exactly one schema in oneOf');
expect(result).toContain("oas3-schema should have required property '$ref'");
});

test('should display proper severity level', () => {
const result = stylish(mixedErrors, { failSeverity: DiagnosticSeverity.Error }, null);
const result = stylish(mixedErrors, { failSeverity: DiagnosticSeverity.Error });
expect(result).toContain(`
3:10 ${chalk.white(
'hint',
Expand Down
2 changes: 1 addition & 1 deletion packages/formatters/src/__tests__/teamcity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mixedErrors from './__fixtures__/mixed-errors.json';

describe('Teamcity formatter', () => {
test('should format messages', () => {
const result = teamcity(mixedErrors, { failSeverity: DiagnosticSeverity.Error }, null);
const result = teamcity(mixedErrors, { failSeverity: DiagnosticSeverity.Error });
expect(result)
.toContain(`##teamcity[inspectionType category='openapi' id='info-contact' name='info-contact' description='hint -- Info object should contain \`contact\` object.']
##teamcity[inspection typeId='info-contact' file='/home/Stoplight/spectral/src/__tests__/__fixtures__/petstore.oas3.json' line='3' message='hint -- Info object should contain \`contact\` object.']
Expand Down
2 changes: 1 addition & 1 deletion packages/formatters/src/__tests__/text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mixedErrors from './__fixtures__/mixed-errors.json';

describe('Text formatter', () => {
test('should format messages', () => {
const result = text(mixedErrors, { failSeverity: DiagnosticSeverity.Error }, null);
const result = text(mixedErrors, { failSeverity: DiagnosticSeverity.Error });
expect(result)
.toContain(`/home/Stoplight/spectral/src/__tests__/__fixtures__/petstore.oas3.json:3:10 hint info-contact "Info object should contain \`contact\` object."
/home/Stoplight/spectral/src/__tests__/__fixtures__/petstore.oas3.json:3:10 warning info-description "OpenAPI object info \`description\` must be present and non-empty string."
Expand Down
Loading

0 comments on commit f20cfd3

Please sign in to comment.