Skip to content

Commit 181bdb8

Browse files
committed
Improve tests
1 parent fd9f87c commit 181bdb8

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/git.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ describe("getDiffFileList", () => {
106106
jest.mock("child_process").resetAllMocks();
107107
mockedChildProcess.execSync.mockReturnValueOnce(Buffer.from(diffFileList));
108108
const staged = false;
109+
expect(mockedChildProcess.execSync).toHaveBeenCalledTimes(0);
109110
const fileListA = getDiffFileList(staged);
110111
const fileListB = getDiffFileList(staged);
111112

112-
expect(mockedChildProcess.execSync).toHaveBeenCalled();
113+
expect(mockedChildProcess.execSync).toHaveBeenCalledTimes(1);
113114
expect(fileListA).toEqual(
114115
["file1", "file2", "file3"].map((p) => path.resolve(p))
115116
);
@@ -119,6 +120,7 @@ describe("getDiffFileList", () => {
119120

120121
describe("getGitFileList", () => {
121122
it("should get the list of committed files", () => {
123+
mockedChildProcess.execSync.mockReturnValueOnce(Buffer.from(diffFileList));
122124
expect(getGitFileList()).toEqual(
123125
["file1", "file2", "file3"].map((p) => path.resolve(p))
124126
);

src/git.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@ const hasCleanIndex = (filePath: string): boolean => {
102102
return result;
103103
};
104104

105+
let untrackedFileListCache: string[] | undefined;
106+
const getUntrackedFileList = (staged = false): string[] => {
107+
if (untrackedFileListCache === undefined) {
108+
const command = ["git", "ls-files", "--exclude-standard", "--others"]
109+
.filter(Boolean)
110+
.join(" ");
111+
112+
if (staged === false) {
113+
untrackedFileListCache = child_process
114+
.execSync(command)
115+
.toString()
116+
.trim()
117+
.split("\n")
118+
.map((filePath) => path.resolve(filePath));
119+
} else {
120+
untrackedFileListCache = [];
121+
}
122+
}
123+
return untrackedFileListCache;
124+
};
125+
105126
const isHunkHeader = (input: string) => {
106127
const hunkHeaderRE = /^@@ [^@]* @@/u;
107128
return hunkHeaderRE.exec(input);
@@ -157,10 +178,11 @@ const getRangesForDiff = (diff: string): Range[] =>
157178
}, []);
158179

159180
export {
181+
getDiffFileList,
160182
getDiffForFile,
161183
getRangesForDiff,
162-
getDiffFileList,
163184
getGitFileList,
185+
getUntrackedFileList,
164186
hasCleanIndex,
165187
};
166188
export type { Range };

0 commit comments

Comments
 (0)