Skip to content

Commit 90fa839

Browse files
authored
Merge pull request #6 from Bandwidth/DX-2822
DX-2822 Add Optional Local Ruleset Arg `-r`
2 parents c03bc5e + 0a11485 commit 90fa839

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

src/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ yargs(hideBin(process.argv))
88
.alias({ v: 'version' })
99
.alias({ s: 'save' })
1010
.alias({ j: 'json'})
11+
.alias({ r: 'ruleset'})
1112
.strict()
1213
.argv;

src/commands/lint.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,28 @@ function deleteRemoteRuleset() {
7676

7777
export const handler = async (argv: Arguments<Options>): Promise<void> => {
7878
// Open the provided API Spec
79-
const { specPath, save, json } = argv;
79+
const { specPath, save, json, ruleset } = argv;
8080
const specFile = fs.readFileSync(specPath, "utf8");
8181
const spec = YAML.parse(specFile);
8282
var specName = path.basename(specPath,path.extname(specPath));
8383

84-
// attempt to download the ruleset
85-
let downloadSuccess = true;
86-
try {
87-
await downloadRuleset(rulesetUrl, rulesetFilepath);
88-
} catch (error) {
89-
// Error downloading the remote ruleset - use the bundled local copy
90-
console.warn(chalk.yellow.bold("Failed to download remote ruleset. Using Local Copy."));
91-
console.log("Note that lint results may vary from production ruleset.");
92-
rulesetFilename = "./static/.local.spectral.yaml";
93-
rulesetFilepath = path.join(__dirname, "..", rulesetFilename);
94-
console.log(rulesetFilepath);
95-
downloadSuccess = false;
84+
// attempt to download the ruleset if no local file was provided
85+
var downloadSuccess;
86+
if(!ruleset){
87+
downloadSuccess = true;
88+
try {
89+
await downloadRuleset(rulesetUrl, rulesetFilepath);
90+
} catch (error) {
91+
// Error downloading the remote ruleset - use the bundled local copy
92+
console.warn(chalk.yellow.bold("Failed to download remote ruleset. Using Local Copy."));
93+
console.log("Note that lint results may vary from production ruleset.");
94+
rulesetFilename = "./static/.local.spectral.yaml";
95+
rulesetFilepath = path.join(__dirname, "..", rulesetFilename);
96+
console.log(rulesetFilepath);
97+
downloadSuccess = false;
98+
}
99+
} else {
100+
rulesetFilepath = path.join(__dirname, "..", ruleset);
96101
}
97102

98103
// Setup Spectral and load ruleset

tests/cli.test.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const testLint = (args) => {
99
return execSync(`node build/cli.js lint ${args} -j`).toString();
1010
};
1111

12+
const testLintWithLocalRuleset = (args) => {
13+
return execSync(`node build/cli.js lint ${args} -j -r ../src/static/.local.spectral.yaml`).toString();
14+
};
15+
1216
describe("cli", () => {
1317
let originalArgv;
1418

@@ -27,9 +31,9 @@ describe("cli", () => {
2731
process.argv = originalArgv;
2832
});
2933

30-
it("should run lint command using a valid spec", async () => {
31-
expect(true).toBe(true)
32-
});
34+
// it("should run lint command using a valid spec", async () => {
35+
// expect(true).toBe(true)
36+
// });
3337

3438
it("should run lint command using a spec with errors", async () => {
3539
result = JSON.parse(testLint("./tests/fixtures/testSpec.yaml"));
@@ -46,4 +50,20 @@ describe("cli", () => {
4650
expect(typeof testObj.range.end.line).toBe("number");
4751
expect(typeof testObj.range.end.character).toBe("number");
4852
});
53+
54+
it("should run lint command using a spec with errors against a local ruleset file", async () => {
55+
result = JSON.parse(testLintWithLocalRuleset("./tests/fixtures/testSpec.yaml"));
56+
testObj = result[1];
57+
expect(typeof testObj.code).toBe("string");
58+
expect(typeof testObj.message).toBe("string");
59+
expect(typeof testObj.path).toBe("object");
60+
expect(typeof testObj.severity).toBe("number");
61+
expect(typeof testObj.range).toBe("object");
62+
expect(typeof testObj.range.start).toBe("object");
63+
expect(typeof testObj.range.start.line).toBe("number");
64+
expect(typeof testObj.range.start.character).toBe("number");
65+
expect(typeof testObj.range.end).toBe("object");
66+
expect(typeof testObj.range.end.line).toBe("number");
67+
expect(typeof testObj.range.end.character).toBe("number");
68+
});
4969
});

0 commit comments

Comments
 (0)