Skip to content

Commit

Permalink
chore(karma): improve fixtures generation
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Oct 8, 2019
1 parent 9876ac2 commit 4681e65
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/binaries
/rulesets
/src/__tests__/__fixtures__/oas-functions.json
/__karma__/__fixtures__/
/test-harness/tmp/

# testing
Expand Down
24 changes: 16 additions & 8 deletions scripts/generate-karma-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
const path = require('path');
const fs = require('fs');

const target = path.join(__dirname, '../src/__tests__/__fixtures__/oas-functions.json');
const baseDir = path.join(__dirname, '../__karma__/__fixtures__/');

const fnsPath = path.join(__dirname, '../rulesets/oas/functions')
if (!fs.existsSync(baseDir)) {
fs.mkdirSync(baseDir);
}

const files = fs.readdirSync(fnsPath);
for (const spec of ['', '2', '3']) {
const target = path.join(baseDir, `oas${spec}-functions.json`);
const fnsPath = path.join(__dirname, `../rulesets/oas${spec}/functions`);
const bundledFns = {};

const bundledFns = {};
if (fs.existsSync(fnsPath)) {
const files = fs.readdirSync(fnsPath);

for (const file of files) {
bundledFns[file] = fs.readFileSync(path.join(fnsPath, file), 'utf-8')
}
for (const file of files) {
bundledFns[file] = fs.readFileSync(path.join(fnsPath, file), 'utf-8');
}
}

fs.writeFileSync(target, JSON.stringify(bundledFns, null, 2))
fs.writeFileSync(target, JSON.stringify(bundledFns, null, 2));
}
19 changes: 13 additions & 6 deletions setupKarma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ const oas2Schema = JSON.parse(JSON.stringify(require('./rulesets/oas2/schemas/ma
const oas3Ruleset = JSON.parse(JSON.stringify(require('./rulesets/oas3/index.json')));
const oas3Schema = JSON.parse(JSON.stringify(require('./rulesets/oas3/schemas/main.json')));

const oasFunctions = JSON.parse(JSON.stringify(require('./src/__tests__/__fixtures__/oas-functions.json')));
const oasFunctions = {
// import path used for require must be deterministic here at compile-time (not run-time), hence no loop can be used
'': JSON.parse(JSON.stringify(require('./__karma__/__fixtures__/oas-functions.json'))),
'2': JSON.parse(JSON.stringify(require('./__karma__/__fixtures__/oas2-functions.json'))),
'3': JSON.parse(JSON.stringify(require('./__karma__/__fixtures__/oas3-functions.json'))),
};

const { fetch } = window;
let fetchMock: FetchMockSandbox;
Expand Down Expand Up @@ -40,11 +45,13 @@ beforeEach(() => {
body: JSON.parse(JSON.stringify(oas3Schema)),
});

for (const [name, fn] of Object.entries<string>(oasFunctions)) {
fetchMock.get(`https://unpkg.com/@stoplight/spectral/rulesets/oas/functions/${name}`, {
status: 200,
body: fn,
});
for (const [spec, fns] of Object.entries(oasFunctions)) {
for (const [name, fn] of Object.entries<string>(fns)) {
fetchMock.get(`https://unpkg.com/@stoplight/spectral/rulesets/oas${spec}/functions/${name}`, {
status: 200,
body: fn,
});
}
}
});

Expand Down

0 comments on commit 4681e65

Please sign in to comment.