Skip to content

Commit

Permalink
Support fresh linters.
Browse files Browse the repository at this point in the history
Create new linter instances for each run, avoiding pile of up failures.

Fixes #1.

See Mermade/oas-kit#179
See Mermade/oas-kit#177
  • Loading branch information
elyobo committed Jun 14, 2019
1 parent 32f47f4 commit 9a39562
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
default: toTestResult
} = require('create-jest-runner/build/toTestResult')
const validator = require('oas-validator')
const linter = require('oas-linter')
const oasLinter = require('oas-linter')
const pkgDir = require('pkg-dir')

const DEFAULT_CONFIG = {
Expand Down Expand Up @@ -35,21 +35,6 @@ const exists = path =>
)
)

// Apply config to the linter; we only want to do this once, as there's only
// one linter at the moment (https://github.com/Mermade/oas-kit/issues/177)
const applyConfig = (() => {
let applied = false

return config => {
if (applied) return
applied = true

const { loadDefaultRules, rules = [] } = config
if (loadDefaultRules) linter.loadDefaultRules()
if (rules.length) linter.applyRules({ rules })
}
})()

// Load config from .oaslint.json or from package.json oaslintConfig field
const loadConfig = testPath =>
pkgDir(path.dirname(testPath)).then(dir => {
Expand All @@ -68,7 +53,6 @@ const loadConfig = testPath =>
.then(config =>
config ? { ...DEFAULT_CONFIG, ...config } : DEFAULT_CONFIG
)
.then(applyConfig)
})

// Run tests on a given file
Expand Down Expand Up @@ -99,8 +83,15 @@ const run = ({ testPath, config, globalConfig }) => {
}

return loadConfig(testPath).then(
() =>
config =>
new Promise((resolve, reject) => {
const { loadDefaultRules, rules = [] } = config
const linter = oasLinter.getLinter()
console.log(linter === oasLinter.getLinter())

if (loadDefaultRules) linter.loadDefaultRules()
if (rules.length) linter.applyRules({ rules })

validator.validate(
schema,
{
Expand Down Expand Up @@ -129,10 +120,12 @@ const run = ({ testPath, config, globalConfig }) => {
}

// Schema is valid, but warnings (lint failures) were present
const tests = warnings.map(
warningToTest(result.end - result.start, testPath)
)
resolve(
toTestResult({
errorMessage:
'Schema is valid, but linting errors were present.',
errorMessage: tests[0].errorMessage,
stats: {
failures: warnings.length,
pending: 0,
Expand All @@ -141,9 +134,7 @@ const run = ({ testPath, config, globalConfig }) => {
start: result.start,
end: result.end
},
tests: warnings.map(
warningToTest(result.end - result.start, testPath)
),
tests,
jestTestPath: testPath
})
)
Expand Down

0 comments on commit 9a39562

Please sign in to comment.