-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathrunner.runOnce.test.ts
More file actions
139 lines (126 loc) · 3.77 KB
/
Copy pathrunner.runOnce.test.ts
File metadata and controls
139 lines (126 loc) · 3.77 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Pool } from "pg";
import { makeWorkerPresetWorkerOptions } from "../src/config";
import { RunnerOptions } from "../src/interfaces";
import { WorkerPreset } from "../src/preset";
import { runOnce } from "../src/runner";
import {
PGDATABASE,
PGHOST,
TEST_CONNECTION_STRING,
withPgPool,
} from "./helpers";
delete process.env.DATABASE_URL;
delete process.env.PGDATABASE;
function setEnvvars(env: { [key: string]: string | undefined }) {
Object.entries(env).forEach(([key, val]) => {
if (val === undefined) {
delete process.env[key];
} else {
process.env[key] = val;
}
});
}
async function withEnv<T>(
envOverrides: { [key: string]: string | undefined },
callback: () => Promise<T>,
): Promise<T> {
const old = Object.keys(envOverrides).reduce((memo, key) => {
memo[key] = process.env[key];
return memo;
}, {} as { [key: string]: string | undefined });
setEnvvars(envOverrides);
const prev = WorkerPreset.worker;
WorkerPreset.worker = makeWorkerPresetWorkerOptions();
try {
return await callback();
} finally {
setEnvvars(old);
WorkerPreset.worker = prev;
}
}
async function runOnceErrorAssertion(
options: RunnerOptions,
message: RegExp | string,
) {
expect.assertions(1);
try {
await runOnce(options);
} catch (e) {
expect(e.message).toMatch(message);
}
}
test("either a list of tasks or a existent task directory must be provided", async () => {
const options: RunnerOptions = {
connectionString: TEST_CONNECTION_STRING,
};
await runOnceErrorAssertion(
options,
/^Could not find tasks to execute - taskDirectory '[^']+\/tasks' does not exist$/,
);
});
test("taskList and taskDirectory cannot be provided a the same time", async () => {
const options: RunnerOptions = {
connectionString: TEST_CONNECTION_STRING,
taskDirectory: "foo",
taskList: { task: () => {} },
};
await runOnceErrorAssertion(
options,
"Exactly one of either `taskDirectory` or `taskList` should be set",
);
});
test("at least a connectionString, a pgPool, the DATABASE_URL or PGDATABASE envvars must be provided", async () => {
const options: RunnerOptions = {
taskList: { task: () => {} },
};
await runOnceErrorAssertion(
options,
"You must either specify `pgPool` or `connectionString`, or you must make the `DATABASE_URL` or `PG*` environmental variables available.",
);
});
test("connectionString and a pgPool cannot provided a the same time", async () => {
const options: RunnerOptions = {
taskList: { task: () => {} },
connectionString: TEST_CONNECTION_STRING,
pgPool: new Pool(),
};
await runOnceErrorAssertion(
options,
"Both `pgPool` and `connectionString` are set, at most one of these options should be provided",
);
});
test("providing just a DATABASE_URL is possible", async () => {
return withEnv({ DATABASE_URL: TEST_CONNECTION_STRING }, async () => {
const options: RunnerOptions = {
taskList: { task: () => {} },
};
expect.assertions(0);
await runOnce(options);
});
});
test("providing just PGHOST and PGDATABASE is possible", async () => {
return withEnv({ PGHOST, PGDATABASE }, async () => {
const options: RunnerOptions = {
taskList: { task: () => {} },
};
expect.assertions(0);
await runOnce(options);
});
});
test("providing just a connectionString is possible", async () => {
const options: RunnerOptions = {
taskList: { task: () => {} },
connectionString: TEST_CONNECTION_STRING,
};
expect.assertions(0);
await runOnce(options);
});
test("providing just a pgPool is possible", async () =>
withPgPool(async (pgPool) => {
const options: RunnerOptions = {
taskList: { task: () => {} },
pgPool: pgPool,
};
expect.assertions(0);
await runOnce(options);
}));