Skip to content

Commit

Permalink
feat: changes based on code reviews and meetings
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Feb 5, 2021
1 parent 4b013ad commit 550ce79
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 166 deletions.
30 changes: 18 additions & 12 deletions src/execCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ export interface ExecCmdResult {
execCmdDuration: Duration;
}

const DEFAULT_SHELL_OPTIONS = {
timeout: 300000, // 5 minutes
cwd: process.cwd(),
env: Object.assign({}, process.env),
silent: true,
const buildCmdOptions = (options?: ExecCmdOptions): ExecCmdOptions => {
const defaults = {
env: Object.assign({}, process.env),
cwd: process.cwd(),
timeout: 300000, // 5 minutes
silent: true,
};
return { ...defaults, ...options };
};

// Create a Duration instance from process.hrtime
Expand All @@ -68,8 +71,9 @@ const addJsonOutput = (cmd: string, result: ExecCmdResult): ExecCmdResult => {
return result;
};

const getExitCodeError = (expectedCode: number, actualCode: number, cmd: string) => {
return Error(`Unexpected exit code for command: ${cmd}. Expected: ${expectedCode} Actual: ${actualCode}`);
const getExitCodeError = (cmd: string, expectedCode: number, output: ShellString) => {
const io = cmd.includes('--json') ? output.stdout : output.stderr;
return Error(`Unexpected exit code for command: ${cmd}. Expected: ${expectedCode} Actual: ${output.code}\n${io}`);
};

/**
Expand Down Expand Up @@ -107,8 +111,7 @@ const execCmdSync = (cmd: string, options?: ExecCmdOptions): ExecCmdResult => {

// Add on the bin path
cmd = buildCmd(cmd);

const cmdOptions = { ...DEFAULT_SHELL_OPTIONS, ...options };
const cmdOptions = buildCmdOptions(options);

debug(`Running cmd: ${cmd}`);
debug(`Cmd options: ${inspect(cmdOptions)}`);
Expand All @@ -125,7 +128,7 @@ const execCmdSync = (cmd: string, options?: ExecCmdOptions): ExecCmdResult => {
debug(`Command completed with exit code: ${result.shellOutput.code}`);

if (isNumber(cmdOptions.ensureExitCode) && result.shellOutput.code !== cmdOptions.ensureExitCode) {
throw getExitCodeError(cmdOptions.ensureExitCode, result.shellOutput.code, cmd);
throw getExitCodeError(cmd, cmdOptions.ensureExitCode, result.shellOutput);
}

return addJsonOutput(cmd, result);
Expand All @@ -138,7 +141,7 @@ const execCmdAsync = async (cmd: string, options: ExecCmdOptions): Promise<ExecC
cmd = buildCmd(cmd);

const resultPromise = new Promise<ExecCmdResult>((resolve, reject) => {
const cmdOptions = { ...DEFAULT_SHELL_OPTIONS, ...options };
const cmdOptions = buildCmdOptions(options);

debug(`Running cmd: ${cmd}`);
debug(`Cmd options: ${inspect(cmdOptions)}`);
Expand All @@ -148,7 +151,10 @@ const execCmdAsync = async (cmd: string, options: ExecCmdOptions): Promise<ExecC
debug(`Command completed with exit code: ${code}`);

if (isNumber(cmdOptions.ensureExitCode) && code !== cmdOptions.ensureExitCode) {
reject(getExitCodeError(cmdOptions.ensureExitCode, code, cmd));
const output = new ShellString(stdout);
output.code = code;
output.stderr = stderr;
reject(getExitCodeError(cmd, cmdOptions.ensureExitCode, output));
}

const result: ExecCmdResult = {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

export * from './genUniqueString';
export * from './execCmd';
export * from './testProject';
export * from './testSession';
export { Duration } from '@salesforce/kit';
141 changes: 0 additions & 141 deletions src/session.ts

This file was deleted.

26 changes: 15 additions & 11 deletions src/project.ts → src/testProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

import * as path from 'path';
import { inspect } from 'util';
import { debug, Debugger } from 'debug';
import * as shell from 'shelljs';
import { genUniqueString } from './genUniqueString';
import { execCmd } from './execCmd';
import { zipDir } from './zip';

export interface TestProjectConfig {
Expand All @@ -27,23 +27,24 @@ export interface TestProjectConfig {
*/
export class TestProject {
public createdDate: Date;
public path: string;
public dir: string;
private debug: Debugger;

public constructor(options: TestProjectConfig) {
this.debug = debug('testkit:project');
this.debug(`Creating TestProject with options: ${inspect(options)}`);
this.createdDate = new Date();

const dir = options.destinationDir || path.join(process.cwd(), 'tmp');

// Copy a dir containing a SFDX project to a dir for testing.
if (options?.sourceDir) {
const rv = shell.cp(options.sourceDir, dir);
const rv = shell.cp('-r', options.sourceDir, dir);
this.debug('project copy result=', rv);
if (rv.code !== 0) {
throw new Error(`project copy failed \n${rv.stderr}`);
}
this.path = path.join(dir, path.dirname(options.sourceDir));
this.dir = path.join(dir, path.basename(options.sourceDir));
}
// Clone a git repo containing a SFDX project in a dir for testing.
else if (options?.gitClone) {
Expand All @@ -52,15 +53,18 @@ export class TestProject {
if (rv.code !== 0) {
throw new Error(`git clone failed \n${rv.stderr}`);
}
this.path = path.join(dir, 'changeme');
this.dir = path.join(dir, 'changeme');
}
// Create a new project using the command.
else {
const name = options.name || genUniqueString('project_%s');
execCmd(`force:project:create -n ${name}`, { ensureExitCode: 0 });
this.path = path.join(dir, name);
const rv = shell.exec(`sfdx force:project:create -n ${name} -d ${dir}`, { silent: true });
if (rv.code !== 0) {
throw new Error(`force:project:create failed \n${rv.stderr}`);
}
this.dir = path.join(dir, name);
}
this.debug(`Created test project: ${this.path}`);
this.debug(`Created test project: ${this.dir}`);
}

/**
Expand All @@ -71,8 +75,8 @@ export class TestProject {
* @returns The created zip file path.
*/
public async zip(name?: string, destDir?: string): Promise<string> {
name ??= path.dirname(this.path);
destDir ??= process.cwd();
return zipDir({ name, sourceDir: this.path, destDir });
name ??= `${path.basename(this.dir)}.zip`;
destDir ??= path.dirname(this.dir);
return zipDir({ name, sourceDir: this.dir, destDir });
}
}
Loading

0 comments on commit 550ce79

Please sign in to comment.