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

feat(cli): use Content-Type header to detect ruleset format #2272

Merged
merged 2 commits into from
Oct 24, 2022
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 packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@stoplight/spectral-parsers": "^1.0.1",
"@stoplight/spectral-ref-resolver": "1.0.2",
"@stoplight/spectral-ruleset-bundler": "^1.0.0",
"@stoplight/spectral-ruleset-migrator": "^1.5.0",
"@stoplight/spectral-ruleset-migrator": "^1.9.0",
"@stoplight/spectral-rulesets": ">=1",
"@stoplight/spectral-runtime": "^1.1.0",
"@stoplight/types": "12.3.0",
Expand Down
41 changes: 40 additions & 1 deletion packages/cli/src/services/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ describe('Linter service', () => {
.persist()
.get('/ruleset.json')
.replyWithFile(200, join(__dirname, '__fixtures__/ruleset.json'), {
'Content-Type': 'application/yaml',
'Content-Type': 'application/json',
});

const output = await run(`lint ${validOas3SpecPath} -r http://foo.local/ruleset.json`);
Expand All @@ -326,6 +326,45 @@ describe('Linter service', () => {
]),
);
});

it('fallbacks to Content-Type', async () => {
nock('http://foo.local')
.persist()
.get('/ruleset')
.replyWithFile(200, join(__dirname, '__fixtures__/ruleset.json'), {
'Content-Type': 'application/json',
});

const output = await run(`lint ${validOas3SpecPath} -r http://foo.local/ruleset`);
expect(output).toEqual(expect.arrayContaining([expect.objectContaining({ code: 'info-matches-stoplight' })]));
expect(output).toEqual(
expect.not.arrayContaining([
expect.objectContaining({
message: 'Info object should contain `contact` object',
}),
]),
);
});

it('ignores query parameters', async () => {
nock('http://foo.local')
.persist()
.get('/ruleset.json')
.query({ token: 'bar' })
.replyWithFile(200, join(__dirname, '__fixtures__/ruleset.json'), {
'Content-Type': 'text/plain', // GitHub raw like
});

const output = await run(`lint ${validOas3SpecPath} -r http://foo.local/ruleset.json?token=bar`);
expect(output).toEqual(expect.arrayContaining([expect.objectContaining({ code: 'info-matches-stoplight' })]));
expect(output).toEqual(
expect.not.arrayContaining([
expect.objectContaining({
message: 'Info object should contain `contact` object',
}),
]),
);
});
});
});

Expand Down
8 changes: 2 additions & 6 deletions packages/cli/src/services/linter/utils/getRuleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from '@stoplight/path';
import * as process from 'process';
import { createRequire } from 'module';
import { fetch } from '@stoplight/spectral-runtime';
import { migrateRuleset } from '@stoplight/spectral-ruleset-migrator';
import { migrateRuleset, isBasicRuleset } from '@stoplight/spectral-ruleset-migrator';
import { bundleRuleset } from '@stoplight/spectral-ruleset-bundler';
import { node } from '@stoplight/spectral-ruleset-bundler/presets/node';
import { stdin } from '@stoplight/spectral-ruleset-bundler/plugins/stdin';
Expand All @@ -25,10 +25,6 @@ async function getDefaultRulesetFile(): Promise<Optional<string>> {
return;
}

function isBasicRuleset(filepath: string): boolean {
return /\.(json|ya?ml)$/.test(path.extname(filepath));
}

function isErrorWithCode(error: Error | (Error & { code: unknown })): error is Error & { code: string } {
return 'code' in error && typeof error.code === 'string';
}
Expand All @@ -49,7 +45,7 @@ export async function getRuleset(rulesetFile: Optional<string>): Promise<Ruleset
let ruleset: string;

try {
if (isBasicRuleset(rulesetFile)) {
if (await isBasicRuleset(rulesetFile)) {
const migratedRuleset = await migrateRuleset(rulesetFile, {
format: 'esm',
fs,
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2514,7 +2514,7 @@ __metadata:
"@stoplight/spectral-parsers": ^1.0.1
"@stoplight/spectral-ref-resolver": 1.0.2
"@stoplight/spectral-ruleset-bundler": ^1.0.0
"@stoplight/spectral-ruleset-migrator": ^1.5.0
"@stoplight/spectral-ruleset-migrator": ^1.9.0
"@stoplight/spectral-rulesets": ">=1"
"@stoplight/spectral-runtime": ^1.1.0
"@stoplight/types": 12.3.0
Expand Down Expand Up @@ -2660,7 +2660,7 @@ __metadata:
languageName: unknown
linkType: soft

"@stoplight/spectral-ruleset-migrator@^1.5.0, @stoplight/spectral-ruleset-migrator@^1.7.4, @stoplight/spectral-ruleset-migrator@workspace:packages/ruleset-migrator":
"@stoplight/spectral-ruleset-migrator@^1.7.4, @stoplight/spectral-ruleset-migrator@^1.9.0, @stoplight/spectral-ruleset-migrator@workspace:packages/ruleset-migrator":
version: 0.0.0-use.local
resolution: "@stoplight/spectral-ruleset-migrator@workspace:packages/ruleset-migrator"
dependencies:
Expand Down