@salesforce/cli-plugins-testkit 3.2.19 → 3.2.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/execCmd.d.ts CHANGED
@@ -18,7 +18,11 @@ type BaseExecOptions = {
18
18
  */
19
19
  cli?: CLI;
20
20
  };
21
- export type ExecCmdOptions = ExecOptions & BaseExecOptions;
21
+ export type ExecCmdOptions = ExecOptions & BaseExecOptions & ({
22
+ cwd: string;
23
+ } | {
24
+ cwd?: never;
25
+ });
22
26
  type ExcludeMethods<T> = Pick<T, NonNullable<{
23
27
  [K in keyof T]: T[K] extends (_: any) => any ? never : K;
24
28
  }[keyof T]>>;
package/lib/execCmd.js CHANGED
@@ -17,6 +17,7 @@ const debug_1 = require("debug");
17
17
  const shelljs = require("shelljs");
18
18
  const shelljs_1 = require("shelljs");
19
19
  const stripAnsi = require("strip-ansi");
20
+ const genUniqueString_1 = require("./genUniqueString");
20
21
  const buildCmdOptions = (options) => {
21
22
  const defaults = {
22
23
  env: { ...process.env, ...options?.env },
@@ -34,10 +35,10 @@ const buildCmdOptions = (options) => {
34
35
  // Create a Duration instance from process.hrtime
35
36
  const hrtimeToMillisDuration = (hrTime) => kit_1.Duration.milliseconds(hrTime[0] * kit_1.Duration.MILLIS_IN_SECONDS + hrTime[1] / 1e6);
36
37
  // Add JSON output if json flag is set
37
- const addJsonOutput = (cmd, result) => {
38
+ const addJsonOutput = (cmd, result, file) => {
38
39
  if (cmd.includes('--json')) {
39
40
  try {
40
- result.jsonOutput = (0, kit_1.parseJson)(stripAnsi(result.shellOutput.stdout));
41
+ result.jsonOutput = (0, kit_1.parseJson)(stripAnsi(fs.readFileSync(file, 'utf-8')));
41
42
  }
42
43
  catch (parseErr) {
43
44
  result.jsonError = parseErr;
@@ -92,30 +93,45 @@ const execCmdSync = (cmd, options) => {
92
93
  const cmdOptions = buildCmdOptions(options);
93
94
  debug(`Running cmd: ${cmd}`);
94
95
  debug(`Cmd options: ${(0, util_1.inspect)(cmdOptions)}`);
96
+ const stdoutFile = `${(0, genUniqueString_1.genUniqueString)('stdout')}.txt`;
97
+ const stderrFile = `${(0, genUniqueString_1.genUniqueString)('stderr')}.txt`;
98
+ const stdoutFileLocation = (0, path_1.join)(cmdOptions.cwd, stdoutFile);
99
+ const stderrFileLocation = (0, path_1.join)(cmdOptions.cwd, stderrFile);
95
100
  const result = {
96
- shellOutput: '',
101
+ shellOutput: new shelljs_1.ShellString(''),
97
102
  execCmdDuration: kit_1.Duration.seconds(0),
98
103
  };
99
104
  // Execute the command in a synchronous child process
100
105
  const startTime = process.hrtime();
101
- result.shellOutput = shelljs.exec(cmd, cmdOptions);
106
+ const code = shelljs.exec(`${cmd} 1> ${stdoutFile} 2> ${stderrFile} `, cmdOptions).code;
107
+ // capture the output for both stdout and stderr
108
+ result.shellOutput = new shelljs_1.ShellString(stripAnsi(fs.readFileSync(stdoutFileLocation, 'utf-8')));
102
109
  result.shellOutput.stdout = stripAnsi(result.shellOutput.stdout);
103
- result.shellOutput.stderr = stripAnsi(result.shellOutput.stderr);
110
+ const shellStringForStderr = new shelljs_1.ShellString(stripAnsi(fs.readFileSync(stderrFileLocation, 'utf-8')));
111
+ // The ShellString constructor sets the argument as stdout, so we strip 'stdout' and set as stderr
112
+ result.shellOutput.stderr = stripAnsi(shellStringForStderr.stdout);
113
+ result.shellOutput.code = code;
104
114
  result.execCmdDuration = hrtimeToMillisDuration(process.hrtime(startTime));
105
115
  debug(`Command completed with exit code: ${result.shellOutput.code}`);
106
116
  if ((0, ts_types_1.isNumber)(cmdOptions.ensureExitCode) && result.shellOutput.code !== cmdOptions.ensureExitCode) {
107
117
  throw getExitCodeError(cmd, cmdOptions.ensureExitCode, result.shellOutput);
108
118
  }
109
- return addJsonOutput(cmd, result);
119
+ const withJson = addJsonOutput(cmd, result, stdoutFileLocation);
120
+ fs.rmSync(stderrFileLocation);
121
+ fs.rmSync(stdoutFileLocation);
122
+ return withJson;
110
123
  };
111
124
  const execCmdAsync = async (cmd, options) => {
112
125
  const debug = (0, debug_1.default)('testkit:execCmdAsync');
113
126
  // Add on the bin path
114
127
  cmd = buildCmd(cmd, options);
115
- const resultPromise = new Promise((resolve, reject) => {
128
+ return new Promise((resolve, reject) => {
116
129
  const cmdOptions = buildCmdOptions(options);
117
130
  debug(`Running cmd: ${cmd}`);
118
131
  debug(`Cmd options: ${(0, util_1.inspect)(cmdOptions)}`);
132
+ // buildCmdOptions will always
133
+ const stdoutFileLocation = (0, path_1.join)(cmdOptions.cwd, `${(0, genUniqueString_1.genUniqueString)('stdout')}.txt`);
134
+ const stderrFileLocation = (0, path_1.join)(cmdOptions.cwd, `${(0, genUniqueString_1.genUniqueString)('stderr')}.txt`);
119
135
  const callback = (code, stdout, stderr) => {
120
136
  const execCmdDuration = hrtimeToMillisDuration(process.hrtime(startTime));
121
137
  debug(`Command completed with exit code: ${code}`);
@@ -126,19 +142,28 @@ const execCmdAsync = async (cmd, options) => {
126
142
  reject(getExitCodeError(cmd, cmdOptions.ensureExitCode, output));
127
143
  }
128
144
  const result = {
129
- shellOutput: new shelljs_1.ShellString(stdout),
145
+ shellOutput: new shelljs_1.ShellString(fs.readFileSync(stdoutFileLocation, 'utf-8')),
130
146
  execCmdDuration,
131
147
  };
132
148
  result.shellOutput.code = code;
133
- result.shellOutput.stdout = stripAnsi(stdout);
134
- result.shellOutput.stderr = stripAnsi(stderr);
135
- resolve(addJsonOutput(cmd, result));
149
+ if (code === 0) {
150
+ result.shellOutput = new shelljs_1.ShellString(stripAnsi(fs.readFileSync(stdoutFileLocation, 'utf-8')));
151
+ result.shellOutput.stdout = stripAnsi(result.shellOutput.stdout);
152
+ }
153
+ else {
154
+ result.shellOutput = new shelljs_1.ShellString(stripAnsi(fs.readFileSync(stderrFileLocation, 'utf-8')));
155
+ // The ShellString constructor sets the argument as stdout, so we strip 'stdout' and set as stderr
156
+ result.shellOutput.stderr = stripAnsi(result.shellOutput.stdout);
157
+ }
158
+ const addJson = addJsonOutput(cmd, result, stdoutFileLocation);
159
+ fs.rmSync(stdoutFileLocation);
160
+ fs.rmSync(stderrFileLocation);
161
+ resolve(addJson);
136
162
  };
137
163
  // Execute the command async in a child process
138
164
  const startTime = process.hrtime();
139
- shelljs.exec(cmd, cmdOptions, callback);
165
+ shelljs.exec(`${cmd} 1> ${stdoutFileLocation} 2> ${stderrFileLocation}`, cmdOptions, callback);
140
166
  });
141
- return resultPromise;
142
167
  };
143
168
  function execCmd(cmd, options) {
144
169
  if (options?.async) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@salesforce/cli-plugins-testkit",
3
3
  "description": "Provides test utilities to assist Salesforce CLI plug-in authors with writing non-unit tests (NUT).",
4
- "version": "3.2.19",
4
+ "version": "3.2.21",
5
5
  "author": "Salesforce",
6
6
  "license": "BSD-3-Clause",
7
7
  "main": "lib/index.js",
@@ -54,30 +54,30 @@
54
54
  },
55
55
  "devDependencies": {
56
56
  "@salesforce/dev-config": "^3.0.0",
57
- "@salesforce/dev-scripts": "^3.1.0",
57
+ "@salesforce/dev-scripts": "^3.1.1",
58
58
  "@salesforce/prettier-config": "^0.0.2",
59
- "@salesforce/ts-sinon": "^1.4.4",
59
+ "@salesforce/ts-sinon": "^1.4.5",
60
60
  "@types/archiver": "^5.1.0",
61
61
  "@types/debug": "^4.1.5",
62
62
  "@typescript-eslint/eslint-plugin": "^5.45.0",
63
- "@typescript-eslint/parser": "^5.48.2",
63
+ "@typescript-eslint/parser": "^5.50.0",
64
64
  "chai": "^4.3.7",
65
- "eslint": "^8.31.0",
65
+ "eslint": "^8.33.0",
66
66
  "eslint-config-prettier": "^8.6.0",
67
67
  "eslint-config-salesforce": "^1.1.0",
68
68
  "eslint-config-salesforce-license": "^0.2.0",
69
69
  "eslint-config-salesforce-typescript": "^1.1.1",
70
70
  "eslint-plugin-header": "^3.1.1",
71
71
  "eslint-plugin-import": "2.27.5",
72
- "eslint-plugin-jsdoc": "^39.6.9",
72
+ "eslint-plugin-jsdoc": "^39.8.0",
73
73
  "husky": "^7.0.4",
74
74
  "mocha": "^9.1.3",
75
75
  "nyc": "^15.1.0",
76
- "prettier": "^2.8.1",
76
+ "prettier": "^2.8.4",
77
77
  "pretty-quick": "^3.1.0",
78
78
  "sinon": "10.0.0",
79
79
  "ts-node": "^10.0.0",
80
- "typescript": "^4.9.4"
80
+ "typescript": "^4.9.5"
81
81
  },
82
82
  "config": {},
83
83
  "publishConfig": {