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: do not generate non-existing paths #459

Merged
merged 6 commits into from
Aug 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
5 changes: 3 additions & 2 deletions karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = (config: any) => {
frameworks: ['jasmine', 'karma-typescript'],

// list of files / patterns to load in the browser
files: ['./karma-jest.ts', 'src/**/*.ts'],
files: ['./karma-jest.ts', './setupKarma.ts', 'src/**/*.ts'],

// list of files / patterns to exclude
// unit tests are excluded because most of them make use of jest.mock
Expand All @@ -22,10 +22,11 @@ module.exports = (config: any) => {
preprocessors: {
'src/**/*.ts': ['karma-typescript'],
'./karma-jest.ts': ['karma-typescript'],
'./setupKarma.ts': ['karma-typescript'],
},

karmaTypescriptConfig: {
...require('./tsconfig.json'),
...require('./tsconfig.karma.json'),
bundlerOptions: {
resolve: {
alias: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"test.prod": "yarn build && yarn lint && yarn test.karma && yarn test --coverage --maxWorkers=2",
"test.update": "yarn test --updateSnapshot",
"test.watch": "yarn test --watch",
"pretest.karma": "yarn build",
"test.karma": "karma start",
"inline-version": "./scripts/inline-version.js",
"schema.update": "yarn typescript-json-schema --id \"http://stoplight.io/schemas/rule.schema.json\" --required tsconfig.json IRule --out ./src/meta/rule.schema.json"
Expand Down
44 changes: 44 additions & 0 deletions setupKarma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { FetchMockSandbox } from 'fetch-mock';

const oasRuleset = JSON.parse(JSON.stringify(require('./rulesets/oas/index.json')));
const oas2Ruleset = JSON.parse(JSON.stringify(require('./rulesets/oas2/index.json')));
const oas2Schema = JSON.parse(JSON.stringify(require('./rulesets/oas2/schemas/main.json')));
const oas3Ruleset = JSON.parse(JSON.stringify(require('./rulesets/oas3/index.json')));
const oas3Schema = JSON.parse(JSON.stringify(require('./rulesets/oas3/schemas/main.json')));

const { fetch } = window;
let fetchMock: FetchMockSandbox;

beforeEach(() => {
fetchMock = require('fetch-mock').sandbox();
window.fetch = fetchMock;

fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas/index.json', {
status: 200,
body: JSON.parse(JSON.stringify(oasRuleset)),
});

fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas2/index.json', {
status: 200,
body: JSON.parse(JSON.stringify(oas2Ruleset)),
});

fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas3/index.json', {
status: 200,
body: JSON.parse(JSON.stringify(oas3Ruleset)),
});

fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas2/schemas/main.json', {
status: 200,
body: JSON.parse(JSON.stringify(oas2Schema)),
});

fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas3/schemas/main.json', {
status: 200,
body: JSON.parse(JSON.stringify(oas3Schema)),
});
});

afterEach(() => {
window.fetch = fetch;
});
10 changes: 5 additions & 5 deletions src/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('linter', () => {
code: 'rule1',
message,
severity: DiagnosticSeverity.Warning,
path: ['responses', '404', 'description'],
path: ['responses', '404'],
range: {
end: {
line: 6,
Expand Down Expand Up @@ -565,8 +565,8 @@ responses:: !!foo
}),
expect.objectContaining({
code: 'valid-example',
message: '"foo" property type should be number',
path: ['components', 'schemas', 'foo'],
message: '"foo.example" property type should be number',
path: ['components', 'schemas', 'foo', 'example'],
}),
expect.objectContaining({
code: 'oas3-schema',
Expand All @@ -586,8 +586,8 @@ responses:: !!foo
expect.arrayContaining([
expect.objectContaining({
code: 'valid-example',
message: '"schema" property can\'t resolve reference #/parameters/missing from id #',
path: ['paths', '/todos/{todoId}', 'put', 'parameters', '1', 'schema'],
message: '"schema.example" property can\'t resolve reference #/parameters/missing from id #',
path: ['paths', '/todos/{todoId}', 'put', 'parameters', '1', 'schema', 'example'],
}),
]),
);
Expand Down
47 changes: 0 additions & 47 deletions src/__tests__/spectral.jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import * as path from 'path';
import { Spectral } from '../spectral';

const oasRuleset = require('../rulesets/oas/index.json');
const oas2Ruleset = require('../rulesets/oas2/index.json');
const oas3Ruleset = require('../rulesets/oas3/index.json');
const customOASRuleset = require('./__fixtures__/custom-oas-ruleset.json');

describe('Spectral', () => {
Expand All @@ -14,51 +12,6 @@ describe('Spectral', () => {
});

describe('loadRuleset', () => {
test('should support loading built-in rulesets', async () => {
const s = new Spectral();
await s.loadRuleset('spectral:oas2');

expect(s.rules).toEqual(
[...Object.entries(oasRuleset.rules), ...Object.entries(oas2Ruleset.rules)].reduce<Dictionary<unknown>>(
(oasRules, [name, rule]) => {
oasRules[name] = {
name,
...rule,
formats: expect.arrayContaining([expect.any(String)]),
severity: expect.any(Number),
then: expect.any(Object),
};

return oasRules;
},
{},
),
);
});

test('should support loading multiple built-in rulesets', async () => {
const s = new Spectral();
await s.loadRuleset('spectral:oas2', 'spectral:oas3');

expect(s.rules).toEqual(
[
...Object.entries(oasRuleset.rules),
...Object.entries(oas2Ruleset.rules),
...Object.entries(oas3Ruleset.rules),
].reduce<Dictionary<unknown>>((oasRules, [name, rule]) => {
oasRules[name] = {
name,
...rule,
formats: expect.arrayContaining([expect.any(String)]),
severity: expect.any(Number),
then: expect.any(Object),
};

return oasRules;
}, {}),
);
});

test('should support loading rulesets from filesystem', async () => {
const s = new Spectral();
await s.loadRuleset(path.join(__dirname, '__fixtures__/custom-oas-ruleset.json'));
Expand Down
98 changes: 10 additions & 88 deletions src/__tests__/spectral.karma.test.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,20 @@
import { DiagnosticSeverity, Dictionary } from '@stoplight/types';
import { DiagnosticSeverity } from '@stoplight/types';
import { FetchMockSandbox } from 'fetch-mock';
import { Spectral } from '../spectral';

const { fetch } = window;

const oasRuleset = require('../rulesets/oas/index.json');
const oas2Ruleset = require('../rulesets/oas2/index.json');
const oas2Schema = require('../rulesets/oas2/schemas/main.json');
const oas3Ruleset = require('../rulesets/oas3/index.json');
const oas3Schema = require('../rulesets/oas3/schemas/main.json');

describe('Spectral', () => {
describe('loadRuleset', () => {
let fetchMock: FetchMockSandbox;

beforeEach(() => {
fetchMock = require('fetch-mock').sandbox();
window.fetch = fetchMock;

fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas/index.json', {
status: 200,
body: oasRuleset,
});

fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas2/index.json', {
status: 200,
body: oas2Ruleset,
});

fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas3/index.json', {
status: 200,
body: oas3Ruleset,
});

fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas2/schemas/main.json', {
status: 200,
body: oas2Schema,
});

fetchMock.get('https://unpkg.com/@stoplight/spectral/rulesets/oas3/schemas/main.json', {
status: 200,
body: oas3Schema,
});
});

afterEach(() => {
window.fetch = fetch;
});
let fetchMock: FetchMockSandbox;

test('should support loading built-in rulesets', async () => {
const s = new Spectral();
await s.loadRuleset('spectral:oas2');

expect(s.rules).toEqual(
[...Object.entries(oasRuleset.rules), ...Object.entries(oas2Ruleset.rules)].reduce<Dictionary<unknown>>(
(oasRules, [name, rule]) => {
oasRules[name] = {
name,
...rule,
formats: expect.arrayContaining([expect.any(String)]),
severity: expect.any(Number),
then: expect.any(Object),
};

return oasRules;
},
{},
),
);
});

test('should support loading multiple built-in rulesets', async () => {
const s = new Spectral();
await s.loadRuleset('spectral:oas2', 'spectral:oas3');

expect(s.rules).toEqual(
[
...Object.entries(oasRuleset.rules),
...Object.entries(oas2Ruleset.rules),
...Object.entries(oas3Ruleset.rules),
].reduce<Dictionary<unknown>>((oasRules, [name, rule]) => {
oasRules[name] = {
name,
...rule,
formats: expect.arrayContaining([expect.any(String)]),
severity: expect.any(Number),
then: expect.any(Object),
};
beforeEach(() => {
fetchMock = require('fetch-mock').sandbox();
window.fetch = fetchMock;
});

return oasRules;
}, {}),
);
});
afterEach(() => {
window.fetch = fetch;
});

describe('loadRuleset', () => {
test('should support loading rulesets over http', async () => {
const ruleset = {
rules: {
Expand Down
53 changes: 52 additions & 1 deletion src/__tests__/spectral.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { DiagnosticSeverity, Dictionary } from '@stoplight/types';
import { isParsedResult, Spectral } from '../spectral';
import { IParsedResult, RuleFunction } from '../types';

const merge = require('lodash/merge');

const oasRuleset = JSON.parse(JSON.stringify(require('../rulesets/oas/index.json')));
const oas2Ruleset = JSON.parse(JSON.stringify(require('../rulesets/oas2/index.json')));
const oas3Ruleset = JSON.parse(JSON.stringify(require('../rulesets/oas3/index.json')));

describe('spectral', () => {
describe('loadRuleset', () => {
test('should support loading built-in rulesets', async () => {
const s = new Spectral();
await s.loadRuleset('spectral:oas2');

expect(s.rules).toEqual(
[...Object.entries(oasRuleset.rules), ...Object.entries(oas2Ruleset.rules)].reduce<Dictionary<unknown>>(
(oasRules, [name, rule]) => {
oasRules[name] = {
name,
...rule,
formats: expect.arrayContaining([expect.any(String)]),
severity: expect.any(Number),
then: expect.any(Object),
};

return oasRules;
},
{},
),
);
});

test('should support loading multiple built-in rulesets', async () => {
const s = new Spectral();
await s.loadRuleset('spectral:oas2', 'spectral:oas3');

expect(s.rules).toEqual(
[
...Object.entries(oasRuleset.rules),
...Object.entries(oas2Ruleset.rules),
...Object.entries(oas3Ruleset.rules),
].reduce<Dictionary<unknown>>((oasRules, [name, rule]) => {
oasRules[name] = {
name,
...rule,
formats: expect.arrayContaining([expect.any(String)]),
severity: expect.any(Number),
then: expect.any(Object),
};

return oasRules;
}, {}),
);
});
});

describe('addRules & mergeRules', () => {
test('should not mutate the passing in rules object', () => {
const givenCustomRuleSet = {
Expand Down
6 changes: 3 additions & 3 deletions src/cli/commands/__tests__/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ describe('lint', () => {
expect.objectContaining({
code: 'info-description',
message: 'OpenAPI object info `description` must be present and non-empty string.',
path: ['info', 'description'], // todo: relative path or absolute path? there is no such path in linted ref, but there is such in spec when working on resolved file
path: ['info', 'description'], // todo: relative path or absolute path? there is no such path in linted file, but there is such in spec when working on resolved file
range: {
end: {
character: 22,
Expand All @@ -357,7 +357,7 @@ describe('lint', () => {
expect.objectContaining({
code: 'api-schemes',
message: 'OpenAPI host `schemes` must be present and non-empty array.',
path: ['schemes'],
path: [],
range: expect.any(Object),
source: expect.stringContaining('src/cli/commands/__tests__/__fixtures__/draft-ref.oas2.json'),
}),
Expand Down Expand Up @@ -404,7 +404,7 @@ describe('lint', () => {
expect.objectContaining({
code: 'api-schemes',
message: 'OpenAPI host `schemes` must be present and non-empty array.',
path: ['schemes'],
path: [],
range: expect.any(Object),
source: expect.stringContaining('src/cli/commands/__tests__/__fixtures__/draft-nested-ref.oas2.json'),
}),
Expand Down
8 changes: 4 additions & 4 deletions src/functions/__tests__/schema-path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ describe('schema', () => {
};
expect(runSchemaPath(target, fieldToCheck, path)).toEqual([
{
path: [],
message: "should have required property 'url'",
path: ['example'],
message: `"example" property should have required property 'url'`,
},
]);
});
Expand All @@ -78,8 +78,8 @@ describe('schema', () => {

expect(runSchemaPath(target, fieldToCheck, path)).toEqual([
{
message: 'format should match format "url"',
path: [],
message: '"example" property format should match format "url"',
path: ['example'],
},
]);
});
Expand Down
Loading