Skip to content

Commit

Permalink
feat: migrate validate command
Browse files Browse the repository at this point in the history
  • Loading branch information
Shurtu-gal committed May 1, 2024
1 parent 7e80488 commit ab3348a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
32 changes: 29 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { AvroSchemaParser } from '@asyncapi/avro-schema-parser';
import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';
import { Parser, convertToOldAPI } from '@asyncapi/parser/cjs';
import { DiagnosticSeverity, Parser, convertToOldAPI } from '@asyncapi/parser/cjs';
import { RamlDTSchemaParser } from '@asyncapi/raml-dt-schema-parser';
import { Flags } from '@oclif/core';
import { ProtoBuffSchemaParser } from '@asyncapi/protobuf-schema-parser';
import { getDiagnosticSeverity } from '@stoplight/spectral-core';
import { OutputFormat } from '@stoplight/spectral-cli/dist/services/config';
import { html, json, junit, pretty, stylish, teamcity, text } from '@stoplight/spectral-formatters';
import { red, yellow, green, cyan } from 'chalk';

import type { Diagnostic } from '@asyncapi/parser/cjs';
import type Command from './base';
Expand Down Expand Up @@ -102,9 +103,10 @@ function logDiagnostics(diagnostics: Diagnostic[], command: Command, specFile: S
}

export function formatOutput(diagnostics: Diagnostic[], format: `${OutputFormat}`, failSeverity: SeverityKind) {
const options = { failSeverity: getDiagnosticSeverity(failSeverity) };
const diagnosticSeverity = getDiagnosticSeverity(failSeverity);
const options = { failSeverity: diagnosticSeverity !== -1 ? diagnosticSeverity : DiagnosticSeverity.Error };
switch (format) {
case 'stylish': return stylish(diagnostics, options);
case 'stylish': return formatStylish(diagnostics, options);
case 'json': return json(diagnostics, options);
case 'junit': return junit(diagnostics, options);
case 'html': return html(diagnostics, options);
Expand All @@ -115,6 +117,30 @@ export function formatOutput(diagnostics: Diagnostic[], format: `${OutputFormat}
}
}

function formatStylish(diagnostics: Diagnostic[], options: { failSeverity: DiagnosticSeverity }) {
const groupedDiagnostics = diagnostics.reduce((acc, diagnostic) => {
const severity = diagnostic.severity;
if (!acc[severity as DiagnosticSeverity]) {
acc[severity as DiagnosticSeverity] = [];
}
acc[severity as DiagnosticSeverity].push(diagnostic);
return acc;
}, {} as Record<DiagnosticSeverity, Diagnostic[]>);

return Object.entries(groupedDiagnostics).map(([severity, diagnostics]) => {
return `${getSeverityTitle(Number(severity))} ${stylish(diagnostics, options)}`;
}).join('\n');
}

function getSeverityTitle(severity: DiagnosticSeverity) {
switch (severity) {
case DiagnosticSeverity.Error: return red('Errors');
case DiagnosticSeverity.Warning: return yellow('Warnings');
case DiagnosticSeverity.Information: return cyan('Information');
case DiagnosticSeverity.Hint: return green('Hints');
}
}

function hasFailSeverity(diagnostics: Diagnostic[], failSeverity: SeverityKind) {
const diagnosticSeverity = getDiagnosticSeverity(failSeverity);
return diagnostics.some(diagnostic => diagnostic.severity <= diagnosticSeverity);
Expand Down
10 changes: 5 additions & 5 deletions test/integration/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('validate', () => {
.stdout()
.command(['validate', './test/fixtures/specification.yml'])
.it('works when file path is passed', (ctx, done) => {
expect(ctx.stdout).to.match(/File .\/test\/fixtures\/specification.yml is valid but has \(itself and\/or referenced documents\) governance issues.\n\ntest\/fixtures\/specification.yml/);
expect(ctx.stdout).to.contain('File ./test/fixtures/specification.yml is valid but has (itself and/or referenced documents) governance issues.\n');
expect(ctx.stderr).to.equal('');
done();
});
Expand All @@ -41,7 +41,7 @@ describe('validate', () => {
.stdout()
.command(['validate', './test/fixtures/specification-avro.yml'])
.it('works when file path is passed and schema is avro', (ctx, done) => {
expect(ctx.stdout).to.match(/File .\/test\/fixtures\/specification-avro.yml is valid but has \(itself and\/or referenced documents\) governance issues.\n/);
expect(ctx.stdout).to.contain('File ./test/fixtures/specification-avro.yml is valid but has (itself and/or referenced documents) governance issues.\n');
expect(ctx.stderr).to.equal('');
done();
});
Expand All @@ -61,7 +61,7 @@ describe('validate', () => {
.stdout()
.command(['validate', 'http://localhost:8080/dummySpec.yml'])
.it('works when url is passed', (ctx, done) => {
expect(ctx.stdout).to.match(/URL http:\/\/localhost:8080\/dummySpec.yml is valid but has \(itself and\/or referenced documents\) governance issues.\n\nhttp:\/\/localhost:8080\/dummySpec.yml/);
expect(ctx.stdout).to.contain('URL http://localhost:8080/dummySpec.yml is valid but has (itself and/or referenced documents) governance issues.\n');
expect(ctx.stderr).to.equal('');
done();
});
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('validate', () => {
.stdout()
.command(['validate', './test/fixtures/specification.yml', '--log-diagnostics'])
.it('works with --log-diagnostics', (ctx, done) => {
expect(ctx.stdout).to.match(/File .\/test\/fixtures\/specification.yml is valid but has \(itself and\/or referenced documents\) governance issues.\n\ntest\/fixtures\/specification.yml/);
expect(ctx.stdout).to.contain('File ./test/fixtures/specification.yml is valid but has (itself and/or referenced documents) governance issues.\n');
expect(ctx.stderr).to.equal('');
done();
});
Expand Down Expand Up @@ -240,7 +240,7 @@ describe('validate', () => {
.stdout()
.command(['validate', './test/fixtures/specification.yml', '--fail-severity=warn'])
.it('works with --fail-severity', (ctx, done) => {
expect(ctx.stderr).to.include('\nFile ./test/fixtures/specification.yml and/or referenced documents have governance issues.\n\ntest/fixtures/specification.yml');
expect(ctx.stderr).to.contain('File ./test/fixtures/specification.yml and/or referenced documents have governance issues.');
expect(process.exitCode).to.equal(1);
done();
});
Expand Down

0 comments on commit ab3348a

Please sign in to comment.