Skip to content

Commit

Permalink
fix: delete existing orgs when before retrying setupCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Mar 16, 2021
1 parent b77c9f0 commit 89db509
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/testProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class TestProject {
throw new Error('git executable not found for creating a project from a git clone');
}
this.debug(`Cloning git repo: ${options.gitClone} to: ${destDir}`);
const rv = shell.exec(`git clone ${options.gitClone}`, { cwd: destDir });
const rv = shell.exec(`git clone ${options.gitClone}`, { cwd: destDir, silent: true });
if (rv.code !== 0) {
throw new Error(`git clone failed with error:\n${rv.stderr}`);
}
Expand Down
65 changes: 35 additions & 30 deletions src/testSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import * as path from 'path';
import { debug, Debugger } from 'debug';
import { fs as fsCore } from '@salesforce/core';
import { Duration, env, parseJson, sleep } from '@salesforce/kit';
import { Duration, env, parseJson } from '@salesforce/kit';
import { AnyJson, getString, Optional } from '@salesforce/ts-types';
import { createSandbox, SinonStub } from 'sinon';
import * as shell from 'shelljs';
Expand Down Expand Up @@ -87,14 +87,12 @@ export class TestSession {
private sandbox = createSandbox();
private orgs: string[] = [];
private zipDir;
private sleep;
private setupRetries: number;

private constructor(options: TestSessionOptions = {}) {
this.sandbox.resetBehavior();
this.debug = debug('testkit:session');
this.zipDir = zipDir;
this.sleep = sleep;
this.createdDate = new Date();
this.id = genUniqueString(`${this.createdDate.valueOf()}%s`);
this.setupRetries = env.getNumber('TESTKIT_SETUP_RETRIES', options.retries) || 0;
Expand Down Expand Up @@ -194,36 +192,11 @@ export class TestSession {
// Always restore the sandbox
this.sandbox.restore();

const rmSessionDir = async () => {
// Delete the test session unless they overrode the test session dir
if (!this.overriddenDir) {
this.debug(`Deleting test session dir: ${this.dir}`);
// Processes can hang on to files within the test session dir, preventing
// removal so we wait a bit before trying.
await this.sleep(Duration.seconds(2));
const rv = shell.rm('-rf', this.dir);
if (rv.code !== 0) {
throw Error(`Deleting the test session failed due to: ${rv.stderr}`);
}
}
};

if (!env.getBoolean('TESTKIT_SAVE_ARTIFACTS')) {
// Delete the orgs created by the tests unless pointing to a specific org
if (!env.getString('TESTKIT_ORG_USERNAME') && this.orgs?.length) {
for (const org of this.orgs) {
this.debug(`Deleting test org: ${org}`);
const rv = shell.exec(`sfdx force:org:delete -u ${org} -p`, { silent: true });
if (rv.code !== 0) {
// Must still delete the session dir if org:delete fails
await rmSessionDir();
throw Error(`Deleting org ${org} failed due to: ${rv.stderr}`);
}
this.debug('Deleted org result=', rv.stdout);
}
}
this.deleteOrgs();
// Delete the session dir
await rmSessionDir();
this.rmSessionDir();
}
}

Expand All @@ -243,6 +216,37 @@ export class TestSession {
}
}

private deleteOrgs(): void {
if (!env.getString('TESTKIT_ORG_USERNAME') && this.orgs?.length) {
const orgs = this.orgs.slice();
for (const org of orgs) {
this.debug(`Deleting test org: ${org}`);
const rv = shell.exec(`sfdx force:org:delete -u ${org} -p`, { silent: true });
this.orgs = this.orgs.filter((o) => o !== org);
if (rv.code !== 0) {
// Must still delete the session dir if org:delete fails
this.rmSessionDir();
throw Error(`Deleting org ${org} failed due to: ${rv.stderr}`);
}
this.debug('Deleted org result=', rv.stdout);
}
}
}

private rmSessionDir(): void {
// Delete the test session unless they overrode the test session dir
if (!this.overriddenDir) {
this.debug(`Deleting test session dir: ${this.dir}`);
// Processes can hang on to files within the test session dir, preventing
// removal so we wait a bit before trying.
this.sleepSync(Duration.seconds(2).milliseconds);
const rv = shell.rm('-rf', this.dir);
if (rv.code !== 0) {
throw Error(`Deleting the test session failed due to: ${rv.stderr}`);
}
}
}

// Executes commands and keeps track of any orgs created.
// Throws if any commands return a non-zero exitCode.
private setupCommands(cmds?: string[]): void {
Expand Down Expand Up @@ -316,6 +320,7 @@ export class TestSession {
throw err;
}
dbug(`Setup failed. waiting ${timeout.seconds} seconds before next attempt...`);
this.deleteOrgs();
this.sleepSync(timeout.milliseconds);
}
}
Expand Down

0 comments on commit 89db509

Please sign in to comment.