Skip to content

Commit 3673750

Browse files
committed
Add Env-backed ActionsEnv implementation for tests
1 parent 30c33c9 commit 3673750

1 file changed

Lines changed: 47 additions & 19 deletions

File tree

src/testing-utils.ts

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,32 @@ export function getTestEnv(testEnv: NodeJS.ProcessEnv = {}): Env {
185185
return getEnv(testEnv);
186186
}
187187

188+
/** An implementation of `ActionsEnv` for use in tests. */
189+
class TestActionsEnv implements ActionsEnv {
190+
constructor(private readonly env: Env) {}
191+
192+
public clone(env: Env): this {
193+
return Object.create(this, { env: { value: env } }) as this;
194+
}
195+
196+
public getRequiredInput(name: string): string {
197+
throw new Error(`Input required and not supplied: ${name}`);
198+
}
199+
200+
public getOptionalInput(_name: string): string | undefined {
201+
return undefined;
202+
}
203+
204+
public exportVariable(name: string, value: string): void {
205+
this.env.set(name, value);
206+
}
207+
}
208+
188209
/**
189210
* Gets an `ActionsEnv` instance for use in tests.
190211
*/
191-
export function getTestActionsEnv(): ActionsEnv {
192-
return {
193-
getRequiredInput: (name) => {
194-
throw new Error(`Input required and not supplied: ${name}`);
195-
},
196-
getOptionalInput: () => undefined,
197-
exportVariable: () => {},
198-
};
212+
export function getTestActionsEnv(env: Env): TestActionsEnv {
213+
return new TestActionsEnv(env);
199214
}
200215

201216
/** For testing purposes, we make all available state features accessible in `TestEnv`. */
@@ -213,12 +228,13 @@ type AllState = [
213228
export function initAllState(
214229
overrides?: Partial<ActionState<AllState>>,
215230
): ActionState<AllState> {
231+
const env = getTestEnv();
216232
return {
217233
name: ActionName.Init,
218234
startedAt: new Date(),
219235
logger: new RecordingLogger(),
220-
env: getTestEnv(),
221-
actions: getTestActionsEnv(),
236+
env,
237+
actions: getTestActionsEnv(env),
222238
apiClient: github.getOctokit("123"),
223239
features: createFeatures([]),
224240
...overrides,
@@ -247,6 +263,7 @@ abstract class BaseEnvBuilder<
247263
> {
248264
protected readonly fn: (state: ActionState<Fs>, ...args: Args) => R;
249265
private logger: RecordingLogger;
266+
private actions: TestActionsEnv;
250267
protected state: ActionState<AllState>;
251268
protected checks: Array<DelayedCheck<Args, R, Fs>>;
252269

@@ -256,15 +273,26 @@ abstract class BaseEnvBuilder<
256273
) {
257274
this.fn = fn;
258275
this.logger = new RecordingLogger();
259-
this.state =
260-
cloneFrom !== undefined
261-
? ({
262-
...cloneFrom.state,
263-
env: cloneFrom.state.env.clone(),
264-
actions: Object.create(cloneFrom.state.actions),
265-
logger: this.logger,
266-
} satisfies ActionState<AllState>)
267-
: initAllState({ logger: this.logger });
276+
277+
if (cloneFrom !== undefined) {
278+
const env = cloneFrom.state.env.clone();
279+
this.actions = cloneFrom.actions.clone(env);
280+
this.state = {
281+
...cloneFrom.state,
282+
env,
283+
actions: this.actions,
284+
logger: this.logger,
285+
} satisfies ActionState<AllState>;
286+
} else {
287+
const env = getTestEnv();
288+
this.actions = getTestActionsEnv(env);
289+
this.state = initAllState({
290+
logger: this.logger,
291+
env,
292+
actions: this.actions,
293+
});
294+
}
295+
268296
this.checks = [...(cloneFrom?.checks ?? [])];
269297
}
270298

0 commit comments

Comments
 (0)