Skip to content

Commit ed1e04c

Browse files
committed
Add a utility function to merge test context
1 parent 118e02e commit ed1e04c

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

tests/test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,46 @@ export interface TestContext {
5959
env?: Record<string, string>;
6060
}
6161

62+
// Allow to merge test contexts in Tests helpers
63+
export function mergeTestContexts(baseContext: TestContext, additionalContext?: TestContext): TestContext {
64+
if (!additionalContext) {
65+
return baseContext;
66+
}
67+
68+
return {
69+
// override name if provided
70+
name: additionalContext.name || baseContext.name,
71+
// combine prereq conditions
72+
prereq: async () => {
73+
const baseResult = !baseContext.prereq || await baseContext.prereq();
74+
const additionalResult = !additionalContext.prereq || await additionalContext.prereq();
75+
return baseResult && additionalResult;
76+
},
77+
// run teardowns in reverse order
78+
teardown: async () => {
79+
if (baseContext.teardown) await baseContext.teardown();
80+
if (additionalContext.teardown) await additionalContext.teardown();
81+
},
82+
// run setups in order
83+
setup: async () => {
84+
if (additionalContext.setup) await additionalContext.setup();
85+
if (baseContext.setup) await baseContext.setup();
86+
},
87+
// override cwd if provided
88+
cwd: additionalContext.cwd || baseContext.cwd,
89+
// merge santize options
90+
santize: {
91+
resources: additionalContext.santize?.resources ?? baseContext.santize?.resources,
92+
ops: additionalContext.santize?.ops ?? baseContext.santize?.ops,
93+
exit: additionalContext.santize?.exit ?? baseContext.santize?.exit,
94+
},
95+
// override ignore if provided
96+
ignore: additionalContext.ignore ?? baseContext.ignore,
97+
// merge env with additional context taking precedence
98+
env: { ...baseContext.env, ...additionalContext.env },
99+
};
100+
}
101+
62102
export function testQuartoCmd(
63103
cmd: string,
64104
args: string[],

0 commit comments

Comments
 (0)