Skip to content

Commit a7206f2

Browse files
committed
CI: Add tests for git line handlers
Three new tests to check serial execution, sequence of execution and slow git command processing. The first two are similar but slightly different. Serial checks to ensure other promises do not start before the current function has returned. Sequencing checks to ensure the output is processed in the proper order. The slow processing tests verifies the command output is handled okay even when the command completes earlier. Signed-off-by: Chris. Webster <chris@webstech.net>
1 parent be508a4 commit a7206f2

File tree

1 file changed

+91
-2
lines changed

1 file changed

+91
-2
lines changed

tests/git.test.ts

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,95 @@
1-
import { expect, test } from "@jest/globals";
2-
import { gitConfig } from "../lib/git";
1+
import { expect, jest, test } from "@jest/globals";
2+
import { git, gitConfig } from "../lib/git";
3+
import logger from "./logger";
4+
5+
jest.setTimeout(180000);
6+
7+
// init config in env
8+
const configCount = 20;
9+
process.env.GIT_CONFIG_COUNT = `${configCount}`;
10+
for (let i = 0; i < configCount; i++) {
11+
process.env[`GIT_CONFIG_KEY_${i}`] = `TEST.TS.case${i}`;
12+
process.env[`GIT_CONFIG_VALUE_${i}`] = `test.case${i} value`;
13+
}
14+
15+
const sleep = async (ms: number) => {
16+
await new Promise<void>((resolve) => {
17+
setTimeout(() => { resolve(); }, ms);
18+
});
19+
};
320

421
test("finds core.bare", async () => {
522
expect(await gitConfig("core.bare")).toMatch(/true|false/);
623
});
24+
25+
test("serialization", async () => {
26+
let waitTime = 10;
27+
type vars = { myWait: number; waitTime: number };
28+
const times = new Array<vars>();
29+
30+
const lineHandler = async (): Promise<void> => {
31+
if (waitTime) {
32+
const myWait = --waitTime;
33+
logger(`waiting ${myWait}`);
34+
await sleep(waitTime*50+waitTime%2*60); // odd/even have different waits
35+
logger(`waiting ${myWait} done`);
36+
// track waitTime before and after wait
37+
times.push( {myWait, waitTime});
38+
}
39+
};
40+
41+
expect(await git(["config", "--get-regexp", "TEST"], {lineHandler})).toMatch("");
42+
times.map((el) => {
43+
logger(el.waitTime, el.myWait);
44+
});
45+
times.map((el) => {
46+
expect(el.waitTime).toEqual(el.myWait);
47+
});
48+
});
49+
50+
test("sequencing", async () => {
51+
let waitTime = configCount;
52+
let buffer = "";
53+
54+
const lineHandler = async (line: string): Promise<void> => {
55+
waitTime--;
56+
await sleep(waitTime*50+waitTime%2*60); // odd/even have different waits
57+
buffer += `${line}\n`;
58+
};
59+
60+
expect(await git(["config", "--get-regexp", "TEST"], {lineHandler})).toMatch("");
61+
expect(await git(["config", "--get-regexp", "TEST"], { trimTrailingNewline: false })).toEqual(buffer);
62+
});
63+
64+
test("slow stdout", async () => {
65+
// ready for syntax checking
66+
const codeModel = `
67+
{
68+
let i = 30;
69+
const o = setInterval(() => {
70+
process.stdout.write('Printing line ' + i + '$nl');
71+
if (!--i) {
72+
clearInterval(o);
73+
}
74+
}, 100);
75+
}
76+
`;
77+
78+
// compact spaces/newlines and add one as needed
79+
const code = codeModel.replace(/ +/g, " ").replace(/\n/g, "").replace(/\$nl/, "\\n");
80+
81+
let buffer = "";
82+
83+
// eslint-disable-next-line @typescript-eslint/require-await
84+
const lineHandler = async (line: string): Promise<void> => {
85+
buffer += `${line}\n`;
86+
};
87+
88+
// set config (using "-c", `alias.node="!node"`, does not work - ! is escaped)
89+
process.env.GIT_CONFIG_COUNT = `1`;
90+
process.env.GIT_CONFIG_KEY_0 = `alias.node`;
91+
process.env.GIT_CONFIG_VALUE_0 = `!node`;
92+
93+
expect(await git([`node`, `-e`, `${code}`], {lineHandler})).toEqual("");
94+
expect(await git([`node`, `-e`, `${code}`])).toEqual(buffer);
95+
});

0 commit comments

Comments
 (0)