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

test(oas): reduce the amount of boilerplate #1472

Merged
merged 4 commits into from
Jan 13, 2021
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
37 changes: 37 additions & 0 deletions src/rulesets/oas/__tests__/__helpers__/createWithRules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { STATIC_ASSETS } from '../../../../assets';
import { empty } from '../../../../utils';
import { IConstructorOpts, Spectral } from '../../../../spectral';
import { isOpenApiv2, isOpenApiv3 } from '../../../../formats';
import * as ruleset from '../../index.json';

export async function createWithRules(
rules: (keyof typeof ruleset['rules'])[],
opts?: IConstructorOpts,
): Promise<Spectral> {
try {
Object.assign(STATIC_ASSETS, await import('../../../../../rulesets/assets/assets.oas.json'), {
'my-ruleset': JSON.stringify({
extends: [['spectral:oas', 'off']],
rules: rules.reduce((obj, name) => {
obj[name] = true;
return obj;
}, {}),
}),
});

const s = new Spectral(opts);
s.registerFormat('oas2', isOpenApiv2);
s.registerFormat('oas3', isOpenApiv3);

await s.loadRuleset('my-ruleset');

for (const rule of rules) {
// let's make sure the rule is actually enabled
expect(s.rules[rule].severity).not.toEqual(-1);
}

return s;
} finally {
empty(STATIC_ASSETS);
}
}
14 changes: 6 additions & 8 deletions src/rulesets/oas/__tests__/contact-properties.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('contact-properties', () => {
const s = new Spectral();
s.setRules({
'contact-properties': Object.assign(ruleset.rules['contact-properties'], {
recommended: true,
type: RuleType[ruleset.rules['contact-properties'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['contact-properties']);
});

test('validate a correct object', async () => {
Expand Down
14 changes: 6 additions & 8 deletions src/rulesets/oas/__tests__/info-contact.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('info-contact', () => {
const s = new Spectral();
s.setRules({
'info-contact': Object.assign(ruleset.rules['info-contact'], {
recommended: true,
type: RuleType[ruleset.rules['info-contact'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['info-contact']);
});

test('validate a correct object', async () => {
Expand Down
14 changes: 6 additions & 8 deletions src/rulesets/oas/__tests__/info-description.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('info-description', () => {
const s = new Spectral();
s.setRules({
'info-description': Object.assign(ruleset.rules['info-description'], {
recommended: true,
type: RuleType[ruleset.rules['info-description'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['info-description']);
});

test('validate a correct object', async () => {
Expand Down
14 changes: 6 additions & 8 deletions src/rulesets/oas/__tests__/info-license.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('info-license', () => {
const s = new Spectral();
s.setRules({
'info-license': Object.assign(ruleset.rules['info-license'], {
recommended: true,
type: RuleType[ruleset.rules['info-license'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['info-license']);
});

test('validate a correct object', async () => {
Expand Down
14 changes: 6 additions & 8 deletions src/rulesets/oas/__tests__/license-url.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('license-url', () => {
const s = new Spectral();
s.setRules({
'license-url': Object.assign(ruleset.rules['license-url'], {
recommended: true,
type: RuleType[ruleset.rules['license-url'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['license-url']);
});

test('validate a correct object', async () => {
Expand Down
19 changes: 4 additions & 15 deletions src/rulesets/oas/__tests__/no-$ref-siblings.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import { isOpenApiv2, isOpenApiv3 } from '../../../formats';
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import refSiblings from '../functions/refSiblings';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('no-$ref-siblings', () => {
let s: Spectral;

beforeEach(() => {
s = new Spectral();
s.registerFormat('oas2', isOpenApiv2);
s.registerFormat('oas3', isOpenApiv3);
s.setFunctions({ refSiblings });
s.setRules({
'no-$ref-siblings': Object.assign({}, ruleset.rules['no-$ref-siblings'], {
recommended: true,
type: RuleType[ruleset.rules['no-$ref-siblings'].type],
}),
});
beforeEach(async () => {
s = await createWithRules(['no-$ref-siblings']);
});

test('reports ref siblings', async () => {
Expand Down
14 changes: 6 additions & 8 deletions src/rulesets/oas/__tests__/no-eval-in-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('no-eval-in-markdown', () => {
const s = new Spectral();
s.setRules({
'no-eval-in-markdown': Object.assign(ruleset.rules['no-eval-in-markdown'], {
recommended: true,
type: RuleType[ruleset.rules['no-eval-in-markdown'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['no-eval-in-markdown']);
});

test('validate a correct object', async () => {
Expand Down
14 changes: 6 additions & 8 deletions src/rulesets/oas/__tests__/no-script-tags-in-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('no-script-tags-in-markdown', () => {
const s = new Spectral();
s.setRules({
'no-script-tags-in-markdown': Object.assign(ruleset.rules['no-script-tags-in-markdown'], {
recommended: true,
type: RuleType[ruleset.rules['no-script-tags-in-markdown'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['no-script-tags-in-markdown']);
});

test('validate a correct object', async () => {
Expand Down
15 changes: 6 additions & 9 deletions src/rulesets/oas/__tests__/oas2-anyOf.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('oas2-anyOf', () => {
const s = new Spectral();
s.registerFormat('oas2', () => true);
s.setRules({
'oas2-anyOf': Object.assign(ruleset.rules['oas2-anyOf'], {
recommended: true,
type: RuleType[ruleset.rules['oas2-anyOf'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['oas2-anyOf']);
});

test('annotates with correct paths', async () => {
Expand Down
15 changes: 6 additions & 9 deletions src/rulesets/oas/__tests__/oas2-api-host.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('oas2-api-host', () => {
const s = new Spectral();
s.registerFormat('oas2', () => true);
s.setRules({
'oas2-api-host': Object.assign(ruleset.rules['oas2-api-host'], {
recommended: true,
type: RuleType[ruleset.rules['oas2-api-host'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['oas2-api-host']);
});

test('validate a correct object', async () => {
Expand Down
15 changes: 6 additions & 9 deletions src/rulesets/oas/__tests__/oas2-api-schemes.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('oas2-api-schemes', () => {
const s = new Spectral();
s.registerFormat('oas2', () => true);
s.setRules({
'oas2-api-schemes': Object.assign(ruleset.rules['oas2-api-schemes'], {
recommended: true,
type: RuleType[ruleset.rules['oas2-api-schemes'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['oas2-api-schemes']);
});

test('validate a correct object', async () => {
Expand Down
15 changes: 6 additions & 9 deletions src/rulesets/oas/__tests__/oas2-host-not-example.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('oas2-host-not-example', () => {
const s = new Spectral();
s.registerFormat('oas2', () => true);
s.setRules({
'oas2-host-not-example': Object.assign(ruleset.rules['oas2-host-not-example'], {
recommended: true,
type: RuleType[ruleset.rules['oas2-host-not-example'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['oas2-host-not-example']);
});

test('validate a correct object', async () => {
Expand Down
15 changes: 6 additions & 9 deletions src/rulesets/oas/__tests__/oas2-host-trailing-slash.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('oas2-host-trailing-slash', () => {
const s = new Spectral();
s.registerFormat('oas2', () => true);
s.setRules({
'oas2-host-trailing-slash': Object.assign(ruleset.rules['oas2-host-trailing-slash'], {
recommended: true,
type: RuleType[ruleset.rules['oas2-host-trailing-slash'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['oas2-host-trailing-slash']);
});

test('validate a correct object', async () => {
Expand Down
15 changes: 6 additions & 9 deletions src/rulesets/oas/__tests__/oas2-oneOf.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';
import type { Spectral } from '../../../spectral';
import { createWithRules } from './__helpers__/createWithRules';

describe('oas2-oneOf', () => {
const s = new Spectral();
s.registerFormat('oas2', () => true);
s.setRules({
'oas2-oneOf': Object.assign(ruleset.rules['oas2-oneOf'], {
recommended: true,
type: RuleType[ruleset.rules['oas2-oneOf'].type],
}),
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['oas2-oneOf']);
});

test('annotates with correct paths', async () => {
Expand Down
28 changes: 5 additions & 23 deletions src/rulesets/oas/__tests__/oas2-schema.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import { functions } from '../../../functions';
import { RuleType, Spectral } from '../../../spectral';
import { setFunctionContext } from '../../evaluators';
import oasDocumentSchema from '../functions/oasDocumentSchema';
import * as ruleset from '../index.json';
import * as oas2Schema from '../schemas/schema.oas2.json';
import { Spectral } from '../../../spectral';

import { createWithRules } from './__helpers__/createWithRules';

describe('oas2-schema', () => {
let s: Spectral;

beforeEach(() => {
s = new Spectral();
s.registerFormat('oas2', () => true);
s.setFunctions({ oasDocumentSchema: setFunctionContext({ functions }, oasDocumentSchema) });
s.setRules({
'oas2-schema': Object.assign({}, ruleset.rules['oas2-schema'], {
recommended: true,
type: RuleType[ruleset.rules['oas2-schema'].type],
then: {
...ruleset.rules['oas2-schema'].then,
functionOptions: {
...ruleset.rules['oas2-schema'].then.functionOptions,
schema: oas2Schema,
},
},
}),
});
beforeEach(async () => {
s = await createWithRules(['oas2-schema']);
});

test('annotates with correct paths', async () => {
Expand Down
Loading