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

Commit 6d84c47

Browse files
feat: added more tests
1 parent 6675ec7 commit 6d84c47

4 files changed

Lines changed: 53 additions & 8 deletions

File tree

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: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1-
import { ACTION_GIT_PUSH_HOOK } from "../utils/constants";
2-
import { executeCommand } from "./test-utils";
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";
34

45
describe("codiga git-push-hook", () => {
5-
test("no git repository", async () => {
6+
test("check for same SHAs", async () => {
67
// run the command
7-
await executeCommand([ACTION_GIT_PUSH_HOOK]).catch(({ stdout }) => {
8-
console.log("stdout: ", stdout);
9-
expect(stdout).toMatch(
10-
/Unable to execute a git command because you're not in a git repository/
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/
1126
);
1227
});
1328
});
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+
});
1442
});

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

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;

0 commit comments

Comments
 (0)