Skip to content

Commit 4f1ac5a

Browse files
committed
Fix tests
1 parent d3749c1 commit 4f1ac5a

File tree

2 files changed

+78
-56
lines changed

2 files changed

+78
-56
lines changed

src/processors.test.ts

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,93 @@
1-
import * as child_process from "child_process";
1+
jest.mock("./git", () => ({
2+
...jest.requireActual<typeof git>("./git"),
3+
getUntrackedFileList: jest.fn(),
4+
getDiffFileList: jest.fn(),
5+
getDiffForFile: jest.fn(),
6+
hasCleanIndex: jest.fn(),
7+
}));
8+
29
import type { Linter } from "eslint";
3-
import { mocked } from "jest-mock";
4-
import { diff, diffConfig, staged, stagedConfig } from "./processors";
10+
import * as git from "./git";
511
import {
612
diff as fixtureDiff,
713
staged as fixtureStaged,
814
} from "./__fixtures__/diff";
915
import { postprocessArguments } from "./__fixtures__/postprocessArguments";
1016

11-
jest.mock("child_process");
12-
const mockedChildProcess = mocked(child_process, true);
13-
mockedChildProcess.execFileSync.mockReturnValue(
14-
Buffer.from('/mock filename ", ; .js')
15-
);
16-
jest.mock("./git", (): unknown => ({
17-
...jest.requireActual("./git"),
18-
getDiffFileList: jest
19-
.fn()
20-
.mockReturnValue(['/mock filename ", ; .js', "README.md"]),
21-
}));
22-
2317
const [messages, filename] = postprocessArguments;
2418

25-
describe("processors", () => {
26-
it("diff preprocess", () => {
27-
const validFilename = filename;
28-
const sourceCode = "/** Some source code */";
29-
30-
mockedChildProcess.execFileSync.mockReturnValue(Buffer.from(fixtureDiff));
19+
const gitMocked: jest.MockedObjectDeep<typeof git> = jest.mocked(git);
20+
gitMocked.getDiffFileList.mockReturnValue([filename]);
21+
gitMocked.getUntrackedFileList.mockReturnValue([]);
3122

32-
expect(diff.preprocess(sourceCode, validFilename)).toEqual([sourceCode]);
33-
});
34-
35-
it("staged preprocess", () => {
23+
describe("processors", () => {
24+
it("preprocess (diff and staged)", async () => {
25+
// The preprocessor does not depend on `staged` being true or false, so it's
26+
// sufficient to only test one of them.
3627
const validFilename = filename;
3728
const sourceCode = "/** Some source code */";
3829

39-
mockedChildProcess.execFileSync.mockReturnValue(Buffer.from(fixtureDiff));
30+
const { diff: diffProcessors } = await import("./processors");
4031

41-
expect(diff.preprocess(sourceCode, validFilename)).toEqual([sourceCode]);
32+
expect(diffProcessors.preprocess(sourceCode, validFilename)).toEqual([
33+
sourceCode,
34+
]);
4235
});
4336

44-
it("diff postprocess", () => {
45-
mockedChildProcess.execFileSync.mockReturnValue(Buffer.from(fixtureDiff));
37+
it("diff postprocess", async () => {
38+
gitMocked.getDiffForFile.mockReturnValue(fixtureDiff);
4639

47-
expect(diff.postprocess(messages, filename)).toMatchSnapshot();
40+
const { diff: diffProcessors } = await import("./processors");
4841

49-
expect(mockedChildProcess.execFileSync).toHaveBeenCalled();
42+
expect(diffProcessors.postprocess(messages, filename)).toMatchSnapshot();
5043
});
5144

52-
it("staged postprocess", () => {
53-
mockedChildProcess.execFileSync.mockReturnValue(Buffer.from(fixtureStaged));
45+
it("staged postprocess", async () => {
46+
gitMocked.hasCleanIndex.mockReturnValueOnce(true);
47+
gitMocked.getDiffForFile.mockReturnValueOnce(fixtureStaged);
5448

55-
expect(staged.postprocess(messages, filename)).toMatchSnapshot();
49+
const { staged: stagedProcessors } = await import("./processors");
5650

57-
expect(mockedChildProcess.execFileSync).toHaveBeenCalled();
51+
expect(stagedProcessors.postprocess(messages, filename)).toMatchSnapshot();
5852
});
5953

60-
it("should report fatal errors", () => {
61-
mockedChildProcess.execFileSync.mockReturnValue(Buffer.from(fixtureDiff));
62-
54+
it("should report fatal errors", async () => {
55+
gitMocked.getDiffForFile.mockReturnValue(fixtureDiff);
6356
const [[firstMessage, ...restMessage], ...restMessageArray] = messages;
6457
const messagesWithFatal: Linter.LintMessage[][] = [
6558
[{ ...firstMessage, fatal: true }, ...restMessage],
6659
...restMessageArray,
6760
];
6861

69-
expect(diff.postprocess(messages, filename)).toHaveLength(2);
70-
expect(diff.postprocess(messagesWithFatal, filename)).toHaveLength(3);
62+
const { diff: diffProcessors } = await import("./processors");
7163

72-
expect(mockedChildProcess.execFileSync).toHaveBeenCalled();
64+
expect(diffProcessors.postprocess(messages, filename)).toHaveLength(2);
65+
expect(
66+
diffProcessors.postprocess(messagesWithFatal, filename)
67+
).toHaveLength(3);
7368
});
7469
});
7570

7671
describe("configs", () => {
77-
it("diff", () => {
72+
it("diff", async () => {
73+
const { diffConfig } = await import("./processors");
7874
expect(diffConfig).toMatchSnapshot();
7975
});
8076

81-
it("staged", () => {
77+
it("staged", async () => {
78+
const { stagedConfig } = await import("./processors");
8279
expect(stagedConfig).toMatchSnapshot();
8380
});
8481
});
82+
83+
describe("fatal error-message", () => {
84+
it("getUnstagedChangesError", async () => {
85+
const { getUnstagedChangesError } = await import("./processors");
86+
87+
const result = getUnstagedChangesError("mock filename.ts")[0];
88+
expect(result?.fatal).toBe(true);
89+
expect(result?.message).toMatchInlineSnapshot(
90+
`"mock filename.ts has unstaged changes. Please stage or remove the changes."`
91+
);
92+
});
93+
});

src/processors.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@ const STAGED = true;
1717
* them from being processed in the first place, as a performance optimization.
1818
* This is increasingly useful the more files there are in the repository.
1919
*/
20-
const getPreProcessor = (staged = false) => {
21-
const untrackedFileList = getUntrackedFileList(staged);
22-
const diffFileList = getDiffFileList(staged);
23-
20+
const getPreProcessor = (
21+
untrackedFileList: string[],
22+
diffFileList: string[]
23+
) => {
2424
return (text: string, filename: string) => {
2525
const shouldBeProcessed =
2626
process.env.VSCODE_CLI !== undefined ||
2727
diffFileList.includes(filename) ||
2828
untrackedFileList.includes(filename);
29+
// console.error({
30+
// untrackedFileList,
31+
// diffFileList,
32+
// text,
33+
// filename,
34+
// shouldBeProcessed,
35+
// });
2936

3037
return shouldBeProcessed ? [text] : [];
3138
};
@@ -34,6 +41,9 @@ const getPreProcessor = (staged = false) => {
3441
const isLineWithinRange = (line: number) => (range: Range) =>
3542
range.isWithinRange(line);
3643

44+
/**
45+
* @internal
46+
*/
3747
function getUnstagedChangesError(filename: string) {
3848
// When we only want to diff staged files, but the file is partially
3949
// staged, the ranges of the staged diff might not match the ranges of the
@@ -55,9 +65,7 @@ function getUnstagedChangesError(filename: string) {
5565
return [fatalError];
5666
}
5767

58-
const getPostProcessor = (staged = false) => {
59-
const untrackedFileList = getUntrackedFileList(staged);
60-
68+
const getPostProcessor = (untrackedFileList: string[], staged = false) => {
6169
return (
6270
messages: Linter.LintMessage[][],
6371
filename: string
@@ -97,11 +105,16 @@ const getPostProcessor = (staged = false) => {
97105
};
98106
};
99107

100-
const getProcessors = (staged = false): Required<Linter.Processor> => ({
101-
preprocess: getPreProcessor(staged),
102-
postprocess: getPostProcessor(staged),
103-
supportsAutofix: true,
104-
});
108+
const getProcessors = (staged = false): Required<Linter.Processor> => {
109+
const untrackedFileList = getUntrackedFileList(staged);
110+
const diffFileList = getDiffFileList(staged);
111+
112+
return {
113+
preprocess: getPreProcessor(untrackedFileList, diffFileList),
114+
postprocess: getPostProcessor(untrackedFileList, staged),
115+
supportsAutofix: true,
116+
};
117+
};
105118

106119
const diff = getProcessors();
107120
const staged = getProcessors(STAGED);
@@ -126,4 +139,4 @@ const stagedConfig: Linter.BaseConfig = {
126139
],
127140
};
128141

129-
export { diff, diffConfig, staged, stagedConfig };
142+
export { diff, diffConfig, staged, stagedConfig, getUnstagedChangesError };

0 commit comments

Comments
 (0)