Skip to content

Commit

Permalink
Adjust tests, throw if things go wrong in a successful step
Browse files Browse the repository at this point in the history
  • Loading branch information
sjchmiela committed Sep 26, 2024
1 parent a8dfd2e commit e45417e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
5 changes: 5 additions & 0 deletions packages/steps/src/BuildStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ export class BuildStep extends BuildStepOutputAccessor {
await this.collectAndUpdateEnvsAsync(this.envsDir);
this.ctx.logger.debug('Finished collecting output parameters');
} catch (error) {
// If the step succeeded, we expect the outputs to be collected successfully.
if (this.status === BuildStepStatus.SUCCESS) {
throw error;
}

this.ctx.logger.debug({ error }, 'Failed to collect output parameters');
}

Expand Down
1 change: 1 addition & 0 deletions packages/steps/src/__tests__/BuildStep-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe(BuildStep, () => {
it('calls ctx.registerStep with the new object', () => {
const mockCtx = mock<BuildStepGlobalContext>();
when(mockCtx.baseLogger).thenReturn(createMockLogger());
when(mockCtx.stepsInternalBuildDirectory).thenReturn('temp-dir');
const ctx = instance(mockCtx);

const id = 'test1';
Expand Down
18 changes: 3 additions & 15 deletions packages/steps/src/__tests__/BuildTemporaryFiles-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os from 'os';

import {
cleanUpStepTemporaryDirectoriesAsync,
createTemporaryOutputsDirectoryAsync,
getTemporaryOutputsDirPath,
saveScriptToTemporaryFileAsync,
} from '../BuildTemporaryFiles.js';

Expand All @@ -23,24 +23,12 @@ describe(saveScriptToTemporaryFileAsync, () => {
});
});

describe(createTemporaryOutputsDirectoryAsync, () => {
it('creates a temporary directory for output values', async () => {
const ctx = createGlobalContextMock();
const outputsPath = await createTemporaryOutputsDirectoryAsync(ctx, 'foo');
await expect(fs.stat(outputsPath)).resolves.not.toThrow();
});
it('creates a temporary directory inside os.tmpdir()', async () => {
const ctx = createGlobalContextMock();
const outputsPath = await createTemporaryOutputsDirectoryAsync(ctx, 'foo');
expect(outputsPath.startsWith(os.tmpdir())).toBe(true);
});
});

describe(cleanUpStepTemporaryDirectoriesAsync, () => {
it('removes the step temporary directories', async () => {
const ctx = createGlobalContextMock();
const scriptPath = await saveScriptToTemporaryFileAsync(ctx, 'foo', 'echo 123');
const outputsPath = await createTemporaryOutputsDirectoryAsync(ctx, 'foo');
const outputsPath = getTemporaryOutputsDirPath(ctx, 'foo');
await fs.mkdir(outputsPath, { recursive: true });
await expect(fs.stat(scriptPath)).resolves.toBeTruthy();
await expect(fs.stat(outputsPath)).resolves.toBeTruthy();
await cleanUpStepTemporaryDirectoriesAsync(ctx, 'foo');
Expand Down
11 changes: 7 additions & 4 deletions packages/steps/src/scripts/__tests__/runCustomFunction-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { BuildStepOutput } from '../../BuildStepOutput.js';
import { createStepContextMock } from '../../__tests__/utils/context.js';
import {
cleanUpStepTemporaryDirectoriesAsync,
createTemporaryEnvsDirectoryAsync,
createTemporaryOutputsDirectoryAsync,
getTemporaryEnvsDirPath,
getTemporaryOutputsDirPath,
} from '../../BuildTemporaryFiles.js';
import { BIN_PATH } from '../../utils/shell/bin.js';
import { createCustomFunctionCall } from '../../utils/customFunction.js';
Expand Down Expand Up @@ -67,8 +67,11 @@ describe('runCustomFunction', () => {
inputs.obj.set({ foo: 'bar' });

try {
const outputsDir = await createTemporaryOutputsDirectoryAsync(ctx.global, 'test');
const envsDir = await createTemporaryEnvsDirectoryAsync(ctx.global, 'test');
const outputsDir = getTemporaryOutputsDirPath(ctx.global, 'test');
const envsDir = getTemporaryEnvsDirPath(ctx.global, 'test');

await fs.mkdir(outputsDir, { recursive: true });
await fs.mkdir(envsDir, { recursive: true });

const currentPath = process.env.PATH;
const newPath = currentPath ? `${BIN_PATH}:${currentPath}` : BIN_PATH;
Expand Down

0 comments on commit e45417e

Please sign in to comment.