Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit f69ce7b

Browse files
feat: added tests and removed unused files
1 parent 0d30723 commit f69ce7b

File tree

8 files changed

+259
-80
lines changed

8 files changed

+259
-80
lines changed

graphql/queries.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,3 @@ export const GET_RULESETS_FOR_CLIENT = gql`
2727
}
2828
}
2929
`;
30-
31-
export const GET_RULESET = gql`
32-
query getRuleset($name: String!) {
33-
ruleSet(name: $name) {
34-
id
35-
name
36-
}
37-
}
38-
`;

tests/__mocks__/inquirer.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ exports.prompt = (prompts) => {
4242
a.choices.forEach((c, i) => {
4343
const expected = a.choices[i];
4444
if (expected) {
45-
expect(prompt.choices[i].name).toContain(expected);
45+
expect(prompt.choices[i]).toEqual(expected);
4646
}
4747
});
4848
}
@@ -54,12 +54,20 @@ exports.prompt = (prompts) => {
5454

5555
if (a.choose != null) {
5656
expect(prompt.type).toBe("list");
57-
setValue(prompt.choices[a.choose].value);
57+
setValue(
58+
prompt.choices.find((choice) => choice.value === a.choose)?.value
59+
);
5860
}
5961

6062
if (a.check != null) {
6163
expect(prompt.type).toBe("checkbox");
62-
setValue(a.check.map((i) => prompt.choices[i].value));
64+
setValue(
65+
a.check
66+
.map((check) => {
67+
return prompt.choices.find((choice) => choice.name === check)?.name;
68+
})
69+
.filter((c) => c)
70+
);
6371
}
6472

6573
if (a.confirm != null) {

tests/add-ruleset-prompts.test.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import {
2+
CATEGORY_CHOICES,
3+
CATEGORY_PYTHON_GENERAL_VALUE,
4+
RULESET_CHOICES,
5+
} from "../utils/constants";
6+
7+
jest.mock("inquirer");
8+
const { prompt, expectPrompts } = require("inquirer");
9+
10+
describe("`codiga ruleset-add` interactive prompts", () => {
11+
test("invalid category", async () => {
12+
// what does the category prompt expect
13+
expectPrompts([
14+
{
15+
type: "list",
16+
name: "category",
17+
pageSize: CATEGORY_CHOICES.length,
18+
message: "Please select one category:",
19+
choices: CATEGORY_CHOICES,
20+
choose: "invalid-category",
21+
},
22+
]);
23+
24+
// mock the prompt with our real world setup
25+
const { category } = await prompt([
26+
{
27+
type: "list",
28+
name: "category",
29+
pageSize: CATEGORY_CHOICES.length,
30+
message: "Please select one category:",
31+
choices: CATEGORY_CHOICES,
32+
},
33+
]);
34+
35+
expect(category).toEqual(undefined);
36+
});
37+
38+
test("valid category; invalid rulesets", async () => {
39+
// what does the category prompt expect
40+
expectPrompts([
41+
{
42+
type: "list",
43+
name: "category",
44+
pageSize: CATEGORY_CHOICES.length,
45+
message: "Please select one category:",
46+
choices: CATEGORY_CHOICES,
47+
choose: CATEGORY_PYTHON_GENERAL_VALUE,
48+
},
49+
]);
50+
51+
// mock the prompt with our real world setup
52+
const { category } = await prompt([
53+
{
54+
type: "list",
55+
name: "category",
56+
pageSize: CATEGORY_CHOICES.length,
57+
message: "Please select one category:",
58+
choices: CATEGORY_CHOICES,
59+
},
60+
]);
61+
62+
// ensure the category is correct
63+
expect(category).toEqual(CATEGORY_PYTHON_GENERAL_VALUE);
64+
65+
// what does the rulesets prompt expect
66+
expectPrompts([
67+
{
68+
type: "checkbox",
69+
name: "rulesets",
70+
message: "Please select all the rulesets you'd like to add",
71+
choices: RULESET_CHOICES[category].map((ruleset) => ({
72+
type: "checkbox",
73+
name: ruleset,
74+
})),
75+
check: ["invalid-ruleset", "also-invalid"],
76+
},
77+
]);
78+
79+
// mock the prompt with our real world setup
80+
const { rulesets } = await prompt([
81+
{
82+
type: "checkbox",
83+
name: "rulesets",
84+
message: "Please select all the rulesets you'd like to add",
85+
choices: RULESET_CHOICES[category].map((ruleset) => ({
86+
type: "checkbox",
87+
name: ruleset,
88+
})),
89+
},
90+
]);
91+
92+
// ensure we receive no rulesets
93+
expect(rulesets.length).toBe(0);
94+
});
95+
96+
test("valid category; valid rulesets", async () => {
97+
// what does the category prompt expect
98+
expectPrompts([
99+
{
100+
type: "list",
101+
name: "category",
102+
pageSize: CATEGORY_CHOICES.length,
103+
message: "Please select one category:",
104+
choices: CATEGORY_CHOICES,
105+
choose: CATEGORY_PYTHON_GENERAL_VALUE,
106+
},
107+
]);
108+
109+
// mock the prompt with our real world setup
110+
const { category } = await prompt([
111+
{
112+
type: "list",
113+
name: "category",
114+
pageSize: CATEGORY_CHOICES.length,
115+
message: "Please select one category:",
116+
choices: CATEGORY_CHOICES,
117+
},
118+
]);
119+
120+
// ensure the category is correct
121+
expect(category).toEqual(CATEGORY_PYTHON_GENERAL_VALUE);
122+
123+
// what does the rulesets prompt expect
124+
expectPrompts([
125+
{
126+
type: "checkbox",
127+
name: "rulesets",
128+
message: "Please select all the rulesets you'd like to add",
129+
choices: RULESET_CHOICES[category].map((ruleset) => ({
130+
type: "checkbox",
131+
name: ruleset,
132+
})),
133+
check: ["python-security"],
134+
},
135+
]);
136+
137+
// mock the prompt with our real world setup
138+
const { rulesets } = await prompt([
139+
{
140+
type: "checkbox",
141+
name: "rulesets",
142+
message: "Please select all the rulesets you'd like to add",
143+
choices: RULESET_CHOICES[category].map((ruleset) => ({
144+
type: "checkbox",
145+
name: ruleset,
146+
})),
147+
},
148+
]);
149+
150+
// ensure the category is correct
151+
expect(rulesets.length).toBe(1);
152+
expect(rulesets).toContain("python-security");
153+
});
154+
});

tests/add-ruleset.test.js

Lines changed: 92 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,103 @@
11
import { ACTION_RULESET_ADD } from "../utils/constants";
22
import { executeCommand } from "./test-utils";
33

4-
describe("codiga ruleset-add", () => {
5-
test("an invalid ruleset name should throw", async () => {
4+
describe("codiga ruleset-add with parameters", () => {
5+
test("1 invalid, 0 valid ruleset; show proper notices", async () => {
66
// run the command
7-
await executeCommand([ACTION_RULESET_ADD, "invalid-ruleset"])
8-
.then((output) => {
9-
expect(output).toBeUndefined();
10-
})
11-
.catch(({ stdout }) => {
7+
await executeCommand([ACTION_RULESET_ADD, "invalid-ruleset"]).catch(
8+
({ stdout }) => {
9+
// check that there are notices for the invalid ruleset
1210
expect(stdout).toMatch(
13-
/That ruleset either doesn't exist or you lack the permissions to access it/
11+
/The following rulesets either don't exist or you lack the permissions to access it/
1412
);
15-
});
13+
expect(stdout).toMatch(/- invalid-ruleset/);
14+
// check that there are no notices for used rulesets
15+
expect(stdout).not.toMatch(
16+
/The following rulesets already exists in your/
17+
);
18+
// check that there are notices for valid rulesets
19+
expect(stdout).toMatch(/No valid rulesets were found to continue/);
20+
}
21+
);
22+
});
23+
24+
test("1 invalid, 1 used ruleset; show proper notices", async () => {
25+
// run the command
26+
await executeCommand([
27+
ACTION_RULESET_ADD,
28+
"invalid-ruleset",
29+
"testing-ruleset-1",
30+
]).then((stdout) => {
31+
// check that there are notices for the invalid ruleset
32+
expect(stdout).toMatch(
33+
/The following rulesets either don't exist or you lack the permissions to access it/
34+
);
35+
expect(stdout).toMatch(/- invalid-ruleset/);
36+
// check that there are notices for the used ruleset
37+
expect(stdout).toMatch(/The following rulesets already exists in your/);
38+
expect(stdout).toMatch(/- testing-ruleset-1/);
39+
// check that there are notices for new rulesets being added
40+
expect(stdout).toMatch(/We added 0 rulesets to your codiga.yml file/);
41+
});
42+
});
43+
44+
test("1 invalid, 1 new ruleset; show proper notices", async () => {
45+
// run the command
46+
await executeCommand([
47+
ACTION_RULESET_ADD,
48+
"invalid-ruleset",
49+
"testing-ruleset-2",
50+
]).then((stdout) => {
51+
// check that there are notices for the invalid ruleset
52+
expect(stdout).toMatch(
53+
/The following rulesets either don't exist or you lack the permissions to access it/
54+
);
55+
expect(stdout).toMatch(/- invalid-ruleset/);
56+
// check that there are no notices for used rulesets
57+
expect(stdout).not.toMatch(
58+
/The following rulesets already exists in your/
59+
);
60+
// check that there are notices for the new ruleset
61+
expect(stdout).toMatch(/We added 1 ruleset to your codiga.yml file/);
62+
});
63+
});
64+
65+
test("1 used, 1 new ruleset; show proper notices", async () => {
66+
// run the command
67+
await executeCommand([
68+
ACTION_RULESET_ADD,
69+
"testing-ruleset-1",
70+
"testing-ruleset-2",
71+
]).then((stdout) => {
72+
// check that there are no notices for invalid rulesets
73+
expect(stdout).not.toMatch(
74+
/The following rulesets either don't exist or you lack the permissions to access it/
75+
);
76+
// check that there are notices for the used ruleset
77+
expect(stdout).toMatch(/The following rulesets already exists in your/);
78+
expect(stdout).toMatch(/- testing-ruleset-1/);
79+
// check that there are notices for the new ruleset
80+
expect(stdout).toMatch(/We added 1 ruleset to your codiga.yml file/);
81+
});
1682
});
1783

18-
test("a ruleset that's already present should throw", async () => {
84+
test("0 used, 2 new rulesets; show proper notices", async () => {
1985
// run the command
20-
await executeCommand([ACTION_RULESET_ADD, "great-ruleset"])
21-
.then((output) => {
22-
expect(output).toBeUndefined();
23-
})
24-
.catch(({ stdout }) => {
25-
console.log(stdout);
26-
expect(stdout).toMatch(/already exists in your/);
27-
});
86+
await executeCommand([
87+
ACTION_RULESET_ADD,
88+
"testing-ruleset-2",
89+
"testing-ruleset-3",
90+
]).then((stdout) => {
91+
// check that there are no notices for invalid rulesets
92+
expect(stdout).not.toMatch(
93+
/The following rulesets either don't exist or you lack the permissions to access it/
94+
);
95+
// check that there are no notices for used rulesets
96+
expect(stdout).not.toMatch(
97+
/The following rulesets already exists in your/
98+
);
99+
// check that there are notices for the new rulesets
100+
expect(stdout).toMatch(/We added 2 rulesets to your codiga.yml file/);
101+
});
28102
});
29103
});

tests/fixtures/codiga.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
rulesets:
22
- react-best-practices
33
- jsx-a11y
4-
- great-ruleset
4+
- testing-ruleset-1
55

tests/fixtures/rulesetMock.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/yaml.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parseYamlFile } from "../utils/file";
2-
import { convertRulesetsToString } from "../utils/ruleset";
2+
import { convertRulesetsToString } from "../utils/rulesets";
33
import {
44
CODIGA_CONFIG_MISSING_RULESETS,
55
CODIGA_CONFIG_MISSING_RULESETS_LIST,

utils/ruleset.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)