@salesforce/cli-plugins-testkit 3.2.19 → 3.2.20

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.
Files changed (2) hide show
  1. package/lib/execCmd.js +43 -14
  2. package/package.json +2 -2
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,49 @@ 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)(process.cwd(), stdoutFile);
99
+ const stderrFileLocation = (0, path_1.join)(process.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);
102
- result.shellOutput.stdout = stripAnsi(result.shellOutput.stdout);
103
- result.shellOutput.stderr = stripAnsi(result.shellOutput.stderr);
106
+ const code = shelljs.exec(`${cmd} 1> ${stdoutFile} 2> ${stderrFile} `, cmdOptions).code;
107
+ if (code === 0) {
108
+ result.shellOutput = new shelljs_1.ShellString(stripAnsi(fs.readFileSync(stdoutFileLocation, 'utf-8')));
109
+ result.shellOutput.stdout = stripAnsi(result.shellOutput.stdout);
110
+ }
111
+ else {
112
+ result.shellOutput = new shelljs_1.ShellString(stripAnsi(fs.readFileSync(stderrFileLocation, 'utf-8')));
113
+ // The ShellString constructor sets the argument as stdout, so we strip 'stdout' and set as stderr
114
+ result.shellOutput.stderr = stripAnsi(result.shellOutput.stdout);
115
+ }
116
+ result.shellOutput.code = code;
104
117
  result.execCmdDuration = hrtimeToMillisDuration(process.hrtime(startTime));
105
118
  debug(`Command completed with exit code: ${result.shellOutput.code}`);
106
119
  if ((0, ts_types_1.isNumber)(cmdOptions.ensureExitCode) && result.shellOutput.code !== cmdOptions.ensureExitCode) {
107
120
  throw getExitCodeError(cmd, cmdOptions.ensureExitCode, result.shellOutput);
108
121
  }
109
- return addJsonOutput(cmd, result);
122
+ const withJson = addJsonOutput(cmd, result, stdoutFileLocation);
123
+ fs.rmSync(stderrFileLocation);
124
+ fs.rmSync(stdoutFileLocation);
125
+ return withJson;
110
126
  };
111
127
  const execCmdAsync = async (cmd, options) => {
112
128
  const debug = (0, debug_1.default)('testkit:execCmdAsync');
113
129
  // Add on the bin path
114
130
  cmd = buildCmd(cmd, options);
115
- const resultPromise = new Promise((resolve, reject) => {
131
+ return new Promise((resolve, reject) => {
116
132
  const cmdOptions = buildCmdOptions(options);
117
133
  debug(`Running cmd: ${cmd}`);
118
134
  debug(`Cmd options: ${(0, util_1.inspect)(cmdOptions)}`);
135
+ const stdoutFile = `${(0, genUniqueString_1.genUniqueString)('stdout')}.txt`;
136
+ const stderrFile = `${(0, genUniqueString_1.genUniqueString)('stderr')}.txt`;
137
+ const stdoutFileLocation = (0, path_1.join)(process.cwd(), stdoutFile);
138
+ const stderrFileLocation = (0, path_1.join)(process.cwd(), stderrFile);
119
139
  const callback = (code, stdout, stderr) => {
120
140
  const execCmdDuration = hrtimeToMillisDuration(process.hrtime(startTime));
121
141
  debug(`Command completed with exit code: ${code}`);
@@ -126,19 +146,28 @@ const execCmdAsync = async (cmd, options) => {
126
146
  reject(getExitCodeError(cmd, cmdOptions.ensureExitCode, output));
127
147
  }
128
148
  const result = {
129
- shellOutput: new shelljs_1.ShellString(stdout),
149
+ shellOutput: new shelljs_1.ShellString(fs.readFileSync(stdoutFileLocation, 'utf-8')),
130
150
  execCmdDuration,
131
151
  };
132
152
  result.shellOutput.code = code;
133
- result.shellOutput.stdout = stripAnsi(stdout);
134
- result.shellOutput.stderr = stripAnsi(stderr);
135
- resolve(addJsonOutput(cmd, result));
153
+ if (code === 0) {
154
+ result.shellOutput = new shelljs_1.ShellString(stripAnsi(fs.readFileSync(stdoutFileLocation, 'utf-8')));
155
+ result.shellOutput.stdout = stripAnsi(result.shellOutput.stdout);
156
+ }
157
+ else {
158
+ result.shellOutput = new shelljs_1.ShellString(stripAnsi(fs.readFileSync(stderrFileLocation, 'utf-8')));
159
+ // The ShellString constructor sets the argument as stdout, so we strip 'stdout' and set as stderr
160
+ result.shellOutput.stderr = stripAnsi(result.shellOutput.stdout);
161
+ }
162
+ const addJson = addJsonOutput(cmd, result, stdoutFileLocation);
163
+ fs.rmSync(stdoutFileLocation);
164
+ fs.rmSync(stderrFileLocation);
165
+ resolve(addJson);
136
166
  };
137
167
  // Execute the command async in a child process
138
168
  const startTime = process.hrtime();
139
- shelljs.exec(cmd, cmdOptions, callback);
169
+ shelljs.exec(`${cmd} 1> ${stdoutFile} 2> ${stderrFile}`, cmdOptions, callback);
140
170
  });
141
- return resultPromise;
142
171
  };
143
172
  function execCmd(cmd, options) {
144
173
  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.20",
5
5
  "author": "Salesforce",
6
6
  "license": "BSD-3-Clause",
7
7
  "main": "lib/index.js",
@@ -62,7 +62,7 @@
62
62
  "@typescript-eslint/eslint-plugin": "^5.45.0",
63
63
  "@typescript-eslint/parser": "^5.48.2",
64
64
  "chai": "^4.3.7",
65
- "eslint": "^8.31.0",
65
+ "eslint": "^8.32.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",