diff --git a/src/cli.ts b/src/cli.ts index 5970176..f2abccf 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -8,5 +8,6 @@ yargs(hideBin(process.argv)) .alias({ v: 'version' }) .alias({ s: 'save' }) .alias({ j: 'json'}) + .alias({ r: 'ruleset'}) .strict() .argv; diff --git a/src/commands/lint.ts b/src/commands/lint.ts index 4539308..1a40bf2 100644 --- a/src/commands/lint.ts +++ b/src/commands/lint.ts @@ -76,23 +76,28 @@ function deleteRemoteRuleset() { export const handler = async (argv: Arguments): Promise => { // Open the provided API Spec - const { specPath, save, json } = argv; + const { specPath, save, json, ruleset } = argv; const specFile = fs.readFileSync(specPath, "utf8"); const spec = YAML.parse(specFile); var specName = path.basename(specPath,path.extname(specPath)); - // attempt to download the ruleset - let downloadSuccess = true; - try { - await downloadRuleset(rulesetUrl, rulesetFilepath); - } catch (error) { - // Error downloading the remote ruleset - use the bundled local copy - console.warn(chalk.yellow.bold("Failed to download remote ruleset. Using Local Copy.")); - console.log("Note that lint results may vary from production ruleset."); - rulesetFilename = "./static/.local.spectral.yaml"; - rulesetFilepath = path.join(__dirname, "..", rulesetFilename); - console.log(rulesetFilepath); - downloadSuccess = false; + // attempt to download the ruleset if no local file was provided + var downloadSuccess; + if(!ruleset){ + downloadSuccess = true; + try { + await downloadRuleset(rulesetUrl, rulesetFilepath); + } catch (error) { + // Error downloading the remote ruleset - use the bundled local copy + console.warn(chalk.yellow.bold("Failed to download remote ruleset. Using Local Copy.")); + console.log("Note that lint results may vary from production ruleset."); + rulesetFilename = "./static/.local.spectral.yaml"; + rulesetFilepath = path.join(__dirname, "..", rulesetFilename); + console.log(rulesetFilepath); + downloadSuccess = false; + } + } else { + rulesetFilepath = path.join(__dirname, "..", ruleset); } // Setup Spectral and load ruleset diff --git a/tests/cli.test.js b/tests/cli.test.js index dded993..4349b5a 100644 --- a/tests/cli.test.js +++ b/tests/cli.test.js @@ -9,6 +9,10 @@ const testLint = (args) => { return execSync(`node build/cli.js lint ${args} -j`).toString(); }; +const testLintWithLocalRuleset = (args) => { + return execSync(`node build/cli.js lint ${args} -j -r ../src/static/.local.spectral.yaml`).toString(); +}; + describe("cli", () => { let originalArgv; @@ -27,9 +31,9 @@ describe("cli", () => { process.argv = originalArgv; }); - it("should run lint command using a valid spec", async () => { - expect(true).toBe(true) - }); + // it("should run lint command using a valid spec", async () => { + // expect(true).toBe(true) + // }); it("should run lint command using a spec with errors", async () => { result = JSON.parse(testLint("./tests/fixtures/testSpec.yaml")); @@ -46,4 +50,20 @@ describe("cli", () => { expect(typeof testObj.range.end.line).toBe("number"); expect(typeof testObj.range.end.character).toBe("number"); }); + + it("should run lint command using a spec with errors against a local ruleset file", async () => { + result = JSON.parse(testLintWithLocalRuleset("./tests/fixtures/testSpec.yaml")); + testObj = result[1]; + expect(typeof testObj.code).toBe("string"); + expect(typeof testObj.message).toBe("string"); + expect(typeof testObj.path).toBe("object"); + expect(typeof testObj.severity).toBe("number"); + expect(typeof testObj.range).toBe("object"); + expect(typeof testObj.range.start).toBe("object"); + expect(typeof testObj.range.start.line).toBe("number"); + expect(typeof testObj.range.start.character).toBe("number"); + expect(typeof testObj.range.end).toBe("object"); + expect(typeof testObj.range.end.line).toBe("number"); + expect(typeof testObj.range.end.character).toBe("number"); + }); });