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

Commit c318eee

Browse files
Merge pull request #1 from codiga/chore/workflow-test
feat: tests and workflows
2 parents 91f4aa3 + 6d84c47 commit c318eee

File tree

10 files changed

+125
-17
lines changed

10 files changed

+125
-17
lines changed
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jobs:
1414
uses: actions/setup-node@v3
1515
with:
1616
node-version: ${{ matrix.node }}
17-
- name: Install modules
18-
run: npm install
19-
- name: Run tests
20-
run: npm run test
17+
- run: npm install
18+
- run: npm link
19+
- run: npm run test
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ jobs:
1212
with:
1313
node-version: '16.x'
1414
registry-url: 'https://registry.npmjs.org'
15-
- name: Install modules
16-
run: npm install
17-
- name: Run tests
18-
run: npm run test
19-
- name: Publish the package
20-
run: npm publish
15+
- run: npm install
16+
- run: npm link
17+
- run: npm run test
18+
- run: npm publish
2119
env:
2220
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

src/checkPush.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
printViolation,
2020
} from "../utils/print";
2121
import { BLANK_SHA } from "../utils/constants";
22+
import { isTestMode } from "../tests/test-utils";
2223

2324
/**
2425
* Gets the changed file paths between two SHAs
@@ -43,7 +44,7 @@ function getChangedFilePaths(remoteSHA, localSHA) {
4344
* @param {string} localShaArg
4445
* @returns {{ remoteSha: string, localSha: string }} updated SHAs strings
4546
*/
46-
function checkSHAs(remoteShaArg, localShaArg) {
47+
export function checkSHAs(remoteShaArg, localShaArg) {
4748
let remoteSha = remoteShaArg;
4849
let localSha = localShaArg;
4950

@@ -69,6 +70,13 @@ function checkSHAs(remoteShaArg, localShaArg) {
6970
process.exit(0);
7071
}
7172

73+
if (isTestMode) {
74+
return {
75+
remoteSha: remoteShaArg,
76+
localSha: localShaArg,
77+
};
78+
}
79+
7280
return {
7381
remoteSha,
7482
localSha,

tests/check-push.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { ACTION_GIT_PUSH_HOOK, BLANK_SHA } from "../utils/constants";
2+
import { setToken } from "../utils/store";
3+
import { executeCommand, SAMPLE_TOKEN } from "./test-utils";
4+
5+
describe("codiga git-push-hook", () => {
6+
test("check for same SHAs", async () => {
7+
// run the command
8+
await executeCommand([ACTION_GIT_PUSH_HOOK, "1234", "1234"]).then(
9+
(output) => {
10+
expect(output).toMatch(/Remote and local SHA are the same/);
11+
}
12+
);
13+
});
14+
15+
test("check for closest SHA", async () => {
16+
// run the command
17+
await executeCommand([
18+
ACTION_GIT_PUSH_HOOK,
19+
"--remote-sha",
20+
BLANK_SHA,
21+
"--local-sha",
22+
"5678",
23+
]).then((output) => {
24+
expect(output).toMatch(
25+
/Tried to find closest SHA but did not find any; exiting with code 0/
26+
);
27+
});
28+
});
29+
30+
test("unable to fetch rulesets when no API token is set", async () => {
31+
// run the command
32+
await executeCommand([
33+
ACTION_GIT_PUSH_HOOK,
34+
"--remote-sha",
35+
"1234",
36+
"--local-sha",
37+
"5678",
38+
]).catch(({ stdout }) => {
39+
expect(stdout).toMatch(/We were unable to fetch your rulesets/);
40+
});
41+
});
42+
});

tests/fixtures/codiga.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rulesets:
2+
- react-best-practices
3+
- jsx-a11y

tests/test-utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ export const TEST_USER = {
1212
accountType: "Github",
1313
};
1414

15+
export const CODIGA_CONFIG_MISSING_RULESETS = `
16+
`;
17+
18+
export const CODIGA_CONFIG_MISSING_RULESETS_LIST = `
19+
rulesets:
20+
`;
21+
22+
export const CODIGA_CONFIG_MISSING_RULESETS_VALID = `
23+
rulesets:
24+
- react-best-practices
25+
- jsx-a11y
26+
`;
27+
1528
export const executeCommand = async (args) => {
1629
try {
1730
return child_process.execSync(`codiga ${args.join(" ")}`, {

tests/token.test.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,29 @@ import { executeCommand, deleteFile, SAMPLE_TOKEN } from "./test-utils";
1010
jest.mock("inquirer");
1111
const { prompt, expectPrompts } = require("inquirer");
1212

13-
beforeEach(async () => {
14-
// delete our testing config file before starting
15-
deleteFile(store.path);
16-
});
13+
function setup() {
14+
beforeEach(() => {
15+
// delete our testing config file before starting
16+
deleteFile(store.path);
17+
// suppress logs
18+
global.console = {
19+
log: jest.fn(),
20+
};
21+
});
22+
afterEach(() => {
23+
global.console = {
24+
log: console.log,
25+
};
26+
});
27+
}
1728

1829
test("ensure we're using a temp store folder", () => {
19-
expect(store.path).toMatch(/@codiga\/cli-testing/);
30+
expect(store.path).toMatch(/cli-testing/);
2031
});
2132

2233
describe("codiga token-add", () => {
34+
setup();
35+
2336
test("first notice shows", async () => {
2437
// run the command
2538
await executeCommand([ACTION_TOKEN_ADD]).catch(({ stdout }) => {
@@ -101,6 +114,8 @@ describe("codiga token-add", () => {
101114
});
102115

103116
describe("codiga token-check", () => {
117+
setup();
118+
104119
test("no token", async () => {
105120
// run the command
106121
await executeCommand([ACTION_TOKEN_CHECK]).catch(({ stdout }) => {
@@ -130,6 +145,8 @@ describe("codiga token-check", () => {
130145
});
131146

132147
describe("codiga token-delete", () => {
148+
setup();
149+
133150
test("no token found", async () => {
134151
// run the command
135152
await executeCommand([ACTION_TOKEN_DELETE]).catch(({ stdout }) => {

tests/yaml.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { parseYamlFile } from "../utils/file";
2+
import {
3+
CODIGA_CONFIG_MISSING_RULESETS,
4+
CODIGA_CONFIG_MISSING_RULESETS_LIST,
5+
CODIGA_CONFIG_MISSING_RULESETS_VALID,
6+
} from "./test-utils";
7+
8+
test("parse missing `rulesets:` correctly", async () => {
9+
expect(parseYamlFile(CODIGA_CONFIG_MISSING_RULESETS)).toBe(null);
10+
});
11+
12+
test("parse missing rulesets list correctly", async () => {
13+
expect(parseYamlFile(CODIGA_CONFIG_MISSING_RULESETS_LIST)).toEqual({
14+
rulesets: null,
15+
});
16+
});
17+
18+
test("parse a valid file accurately", async () => {
19+
expect(parseYamlFile(CODIGA_CONFIG_MISSING_RULESETS_VALID)).toEqual({
20+
rulesets: ["react-best-practices", "jsx-a11y"],
21+
});
22+
});

utils/git.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import child_process from "child_process";
22
import { printFailure } from "./print";
3+
import { isTestMode } from "../tests/test-utils";
34

45
/**
56
* Executes a git command with the given args in that order
@@ -22,6 +23,10 @@ export function executeGitCommand(args) {
2223
* @returns the directory string or exits if there isn't one
2324
*/
2425
export function getRootDirectory() {
26+
// if we're in test mode we'll skip this check
27+
if (isTestMode) return "./tests/fixtures";
28+
29+
// now look for a git repository
2530
const rootDirectory = executeGitCommand(["rev-parse", "--show-toplevel"]);
2631
if (rootDirectory) {
2732
return rootDirectory.split("\n").join("");
@@ -74,6 +79,7 @@ export function getMainBranch() {
7479
}
7580

7681
export function findClosestSha() {
82+
if (isTestMode) return null;
7783
const currentBranch = getCurrentBranch();
7884
const mainBranch = getMainBranch();
7985
let closestSha = null;

utils/rulesets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function getRulesetsFromCodigaFile() {
5959
// if there aren't any ruleset items under `rulesets:` in the codiga.yml file, throw an error
6060
if (!parsedFile.rulesets) {
6161
printFailure(
62-
"We can't look for violations is there are no rulesets listed in your `codiga.yml` file"
62+
"We can't look for violations if there are no rulesets listed in your `codiga.yml` file"
6363
);
6464
printSuggestion(
6565
" ↳ Ensure you have rulesets listed in: ",

0 commit comments

Comments
 (0)