-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtests.ts
95 lines (85 loc) · 3.14 KB
/
tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Apply patches which fix generated test code. The tests are reverse-engineered from
* markdown commands, and un-flattening the commands is pretty complex. It's not user-
* facing, so these are mostly shortcuts to avoid the reverse engineering from being too
* "smart". It's already pretty complex (much more complex than the client generation),
* and making it _perfect_ isn't really worth it.
*/
export const fixupGeneratedTests = (filename: string) => {
const fixers = [
fixKeyWeightsOverlyComplexParsingIssue,
catchDecrOutOfRange(filename),
commentOutFutureReleaseFeatures(filename),
commentOutNonDeterministicCommands(filename),
];
return (code: string): string => {
fixers.forEach(fix => (code = fix(code)));
return code;
};
};
/**
* at time of writing, redis have only released 6.0.8 on dockerhub. These features
* aren't available in that image
*/
function commentOutFutureReleaseFeatures(filename: string) {
const unsupported = [
"lmove",
"lpos",
"smismember",
"zinter",
"zmscore",
"zunion",
"zdiff",
"zdiffstore",
"zintercard",
"sintercard",
"expiretime",
];
const match = unsupported.find(u => filename.endsWith(`${u}.md`));
if (match) {
return (code: string) => `// ${match} not supported by node_redis! ${code}`;
}
const v7Overloads = {
expire: [`"XX"`, `"NX"`],
pexpire: [`"XX"`, `"NX"`],
};
const entry = Object.entries(v7Overloads).find(e => filename.endsWith(`${e[0]}.md`));
if (entry) {
return (code: string) =>
entry[1].some(s => code.includes(s)) ? `// ${entry[1]} not supported in redis v6! ${code}` : code;
}
return (code: string) => code;
}
/**
* at time of writing, redis have only released 6.0.8 on dockerhub. These features
* aren't available in that image
*/
function commentOutNonDeterministicCommands(filename: string) {
const commands = ["zrandmember", "hrandfield"];
const match = commands.find(u => filename.endsWith(`${u}.md`));
if (match) {
return (code: string) => `// ${match} gives a non-deterministic output! ${code}`;
}
return (code: string) => code;
}
/**
* zunionstore and zinterstore use a special `numkeys 3 weights 1 2 3` format which the decoder
* isn't smart enough to handle. Just throw a ts-expect-error on there
*/
function fixKeyWeightsOverlyComplexParsingIssue(code: string) {
if (code.match(/(zunionstore|zinterstore).*WEIGHTS/)) {
return [`// @ts-expect-error (not smart enough to deal with numkeys)`, code].join("\n");
}
if (code.match(/(zunion|zinter|zdiff)\b.*"zset1","zset2"/)) {
return code.replace(`"zset1","zset2"`, `["zset1", "zset2"]`);
}
return code;
}
/** The `decr.md` example shows an error scenario. Explicitly handle it */
function catchDecrOutOfRange(filename: string) {
if (filename.endsWith("decr.md")) {
const catchable = `outputs.r3 = await client.decr("mykey")`;
return (code: string) => code.replace(catchable, catchable + `.catch(e => e)`);
}
return (code: string) => code;
}