Skip to content

Commit

Permalink
Merge pull request #6 from Bandwidth/DX-2822
Browse files Browse the repository at this point in the history
DX-2822 Add Optional Local Ruleset Arg `-r`
  • Loading branch information
ajrice6713 authored Sep 19, 2022
2 parents c03bc5e + 0a11485 commit 90fa839
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ yargs(hideBin(process.argv))
.alias({ v: 'version' })
.alias({ s: 'save' })
.alias({ j: 'json'})
.alias({ r: 'ruleset'})
.strict()
.argv;
31 changes: 18 additions & 13 deletions src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,28 @@ function deleteRemoteRuleset() {

export const handler = async (argv: Arguments<Options>): Promise<void> => {
// 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
Expand Down
26 changes: 23 additions & 3 deletions tests/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"));
Expand All @@ -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");
});
});

0 comments on commit 90fa839

Please sign in to comment.