Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class ExecutionEnvironment implements AsyncDisposable {
* up when this object is disposed.
*
* If `markSuccessful()` is called, the writer lock is converted to a reader lock
* and temporary directories will not be cleaned up anymore.
* and temporary directories will not be cleaned up by this object anymore.
*/
public static async create(services: ToolkitServices, options: ExecutionEnvironmentOptions) {
let outDirIsTemporary = false;
Expand Down Expand Up @@ -79,16 +79,14 @@ export class ExecutionEnvironment implements AsyncDisposable {
}

/**
* Should the outdir be disposed of.
* The directory the cloud assembly will be written to.
*/
public get shouldDisposeOutDir(): boolean {
return this.shouldClean;
}
public readonly outdir: string;

/**
* The directory the cloud assembly will be written to.
* Whether `outdir` was created as a temporary directory for this execution.
*/
public readonly outdir: string;
public readonly outDirIsTemporary: boolean;

private readonly options: Required<ExecutionEnvironmentOptions>;
private readonly ioHelper: IoHelper;
Expand All @@ -109,6 +107,7 @@ export class ExecutionEnvironment implements AsyncDisposable {
this.sdkProvider = services.sdkProvider;
this.debugFn = (msg: string) => this.ioHelper.defaults.debug(msg);
this.lock = lock;
this.outDirIsTemporary = outDirIsTemporary;
this.shouldClean = outDirIsTemporary;
this.outdir = options.outdir;
this.options = options;
Expand All @@ -117,7 +116,7 @@ export class ExecutionEnvironment implements AsyncDisposable {
public async [Symbol.asyncDispose]() {
await this.lock?.release();

if (this.shouldDisposeOutDir) {
if (this.shouldClean) {
await fs.rm(this.outdir, { recursive: true, force: true });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ export abstract class CloudAssemblySourceBuilder {
});

const success = await execution.markSuccessful();
const deleteOnDispose = options.disposeOutdir ?? execution.shouldDisposeOutDir;
const deleteOnDispose = options.disposeOutdir ?? execution.outDirIsTemporary;
return new ReadableCloudAssembly(asm, success.readLock, { deleteOnDispose });
} finally {
await cleanupContextOverflow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,92 @@ describe('fromAssemblyBuilder', () => {
expect(await fs.pathExists(asm.cloudAssembly.directory)).toBeFalsy();
});

test('disposes temporary outdir by default when outdir is omitted', async () => {
// GIVEN
const builder = builderFunctionFromFixture('two-empty-stacks');
const cx = await toolkit.fromAssemblyBuilder(builder, {
resolveDefaultEnvironment: false,
});

// WHEN
const asm = await toolkit.synth(cx);
const outdir = asm.cloudAssembly.directory;
expect(await fs.pathExists(outdir)).toBeTruthy();

try {
await asm.dispose();

// THEN
expect(await fs.pathExists(outdir)).toBeFalsy();
} finally {
await fs.remove(outdir);
}
});

test('preserves temporary outdir when disposeOutdir is false', async () => {
// GIVEN
const builder = builderFunctionFromFixture('two-empty-stacks');
const cx = await toolkit.fromAssemblyBuilder(builder, {
disposeOutdir: false,
resolveDefaultEnvironment: false,
});

// WHEN
const asm = await toolkit.synth(cx);
const outdir = asm.cloudAssembly.directory;

try {
await asm.dispose();

// THEN
expect(await fs.pathExists(outdir)).toBeTruthy();
} finally {
await fs.remove(outdir);
}
});

test('preserves explicit outdir by default when disposeOutdir is omitted', async () => {
// GIVEN
await using synthDir = autoCleanOutDir();
const explicitDir = path.join(synthDir.dir, 'explicit.dir');
const builder = builderFunctionFromFixture('two-empty-stacks');
const cx = await toolkit.fromAssemblyBuilder(builder, {
outdir: explicitDir,
resolveDefaultEnvironment: false,
});

// WHEN
const asm = await toolkit.synth(cx);
await asm.dispose();

// THEN
expect(await fs.pathExists(explicitDir)).toBeTruthy();
});

test('cleans up temporary outdir when builder fails', async () => {
// GIVEN
let tempOutdir: string | undefined;
const cx = await toolkit.fromAssemblyBuilder(async (props) => {
tempOutdir = props.outdir;
throw new Error('builder failure');
}, {
resolveDefaultEnvironment: false,
});

// WHEN
try {
await expect(cx.produce()).rejects.toThrow(/Assembly builder failed/);

// THEN
expect(tempOutdir).toBeDefined();
expect(await fs.pathExists(tempOutdir!)).toBeFalsy();
} finally {
if (tempOutdir) {
await fs.remove(tempOutdir);
}
}
});

test('fromAssemblyBuilder can successfully loop', async () => {
// GIVEN
const provideContextValues = jest.spyOn(contextproviders, 'provideContextValues').mockImplementation(async (
Expand Down
Loading