Skip to content

Commit bf3f207

Browse files
committed
Increase coverage
1 parent b6ab945 commit bf3f207

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { base } = require("@paleite/jest-config");
44
/** @type {import('@jest/types').Config.InitialOptions} */
55
module.exports = {
66
...base,
7-
coveragePathIgnorePatterns: [".test-d.ts"],
7+
coveragePathIgnorePatterns: [".test-d.ts", "/__fixtures__/"],
88
coverageThreshold: {
99
global: {
1010
statements: 80,

src/git.test.ts

+42-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,46 @@ describe("getDiffForFile", () => {
6666
expect(diffFromFile).toContain("diff --git");
6767
expect(diffFromFile).toContain("@@");
6868
});
69+
70+
it("should work when using staged = false", () => {
71+
mockedChildProcess.execFileSync.mockReturnValueOnce(Buffer.from(hunks));
72+
process.env.ESLINT_PLUGIN_DIFF_COMMIT = "1234567";
73+
74+
const diffFromFile = getDiffForFile("./mockfile.js", false);
75+
76+
const expectedCommand = "git";
77+
const expectedArgs =
78+
"diff --diff-algorithm=histogram --diff-filter=ACM --find-renames=100% --no-ext-diff --relative --unified=0 1234567";
79+
80+
const lastCall = mockedChildProcess.execFileSync.mock.calls.at(-1);
81+
const [command, argsIncludingFile = []] = lastCall ?? [""];
82+
const args = argsIncludingFile.slice(0, -2);
83+
84+
expect(command).toBe(expectedCommand);
85+
expect(args.join(" ")).toEqual(expectedArgs);
86+
expect(diffFromFile).toContain("diff --git");
87+
expect(diffFromFile).toContain("@@");
88+
});
89+
90+
it("should use HEAD when no commit was defined", () => {
91+
mockedChildProcess.execFileSync.mockReturnValueOnce(Buffer.from(hunks));
92+
process.env.ESLINT_PLUGIN_DIFF_COMMIT = undefined;
93+
94+
const diffFromFile = getDiffForFile("./mockfile.js", false);
95+
96+
const expectedCommand = "git";
97+
const expectedArgs =
98+
"diff --diff-algorithm=histogram --diff-filter=ACM --find-renames=100% --no-ext-diff --relative --unified=0 HEAD";
99+
100+
const lastCall = mockedChildProcess.execFileSync.mock.calls.at(-1);
101+
const [command, argsIncludingFile = []] = lastCall ?? [""];
102+
const args = argsIncludingFile.slice(0, -2);
103+
104+
expect(command).toBe(expectedCommand);
105+
expect(args.join(" ")).toEqual(expectedArgs);
106+
expect(diffFromFile).toContain("diff --git");
107+
expect(diffFromFile).toContain("@@");
108+
});
69109
});
70110

71111
describe("hasCleanIndex", () => {
@@ -93,7 +133,7 @@ describe("getDiffFileList", () => {
93133
Buffer.from(diffFileList)
94134
);
95135
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(0);
96-
const fileListA = getDiffFileList();
136+
const fileListA = getDiffFileList(false);
97137

98138
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(1);
99139
expect(fileListA).toEqual(
@@ -109,7 +149,7 @@ describe("getUntrackedFileList", () => {
109149
Buffer.from(diffFileList)
110150
);
111151
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(0);
112-
const fileListA = getUntrackedFileList();
152+
const fileListA = getUntrackedFileList(false);
113153
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(1);
114154

115155
mockedChildProcess.execFileSync.mockReturnValueOnce(

src/git.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Range } from "./Range";
55
const COMMAND = "git";
66
const OPTIONS = { maxBuffer: 1024 * 1024 * 100 };
77

8-
const getDiffForFile = (filePath: string, staged = false): string => {
8+
const getDiffForFile = (filePath: string, staged: boolean): string => {
99
const args = [
1010
"diff",
1111
"--diff-algorithm=histogram",
@@ -26,7 +26,7 @@ const getDiffForFile = (filePath: string, staged = false): string => {
2626
return child_process.execFileSync(COMMAND, args, OPTIONS).toString();
2727
};
2828

29-
const getDiffFileList = (staged = false): string[] => {
29+
const getDiffFileList = (staged: boolean): string[] => {
3030
const args = [
3131
"diff",
3232
"--diff-algorithm=histogram",
@@ -79,7 +79,7 @@ const fetchFromOrigin = (branch: string) => {
7979

8080
let untrackedFileListCache: string[] | undefined;
8181
const getUntrackedFileList = (
82-
staged = false,
82+
staged: boolean,
8383
shouldRefresh = false
8484
): string[] => {
8585
if (staged) {

src/processors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const getUnstagedChangesError = (filename: string): [Linter.LintMessage] => {
7272
};
7373

7474
const getPostProcessor =
75-
(staged = false) =>
75+
(staged: boolean) =>
7676
(
7777
messages: Linter.LintMessage[][],
7878
filename: string

0 commit comments

Comments
 (0)