@oclif/test 4.0.1-beta.1 → 4.0.1-beta.2

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/index.js +34 -51
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -9,49 +9,6 @@ const ansis_1 = __importDefault(require("ansis"));
9
9
  const debug_1 = __importDefault(require("debug"));
10
10
  const node_path_1 = require("node:path");
11
11
  const debug = (0, debug_1.default)('oclif-test');
12
- const RECORD_OPTIONS = {
13
- print: false,
14
- stripAnsi: true,
15
- };
16
- const originals = {
17
- NODE_ENV: process.env.NODE_ENV,
18
- stderr: process.stderr.write,
19
- stdout: process.stdout.write,
20
- };
21
- const output = {
22
- stderr: [],
23
- stdout: [],
24
- };
25
- function mockedStdout(str, encoding, cb) {
26
- output.stdout.push(str);
27
- if (!RECORD_OPTIONS.print)
28
- return true;
29
- if (typeof encoding === 'string') {
30
- return originals.stdout.bind(process.stdout)(str, encoding, cb);
31
- }
32
- return originals.stdout.bind(process.stdout)(str, cb);
33
- }
34
- function mockedStderr(str, encoding, cb) {
35
- output.stderr.push(str);
36
- if (!RECORD_OPTIONS.print)
37
- return true;
38
- if (typeof encoding === 'string') {
39
- return originals.stdout.bind(process.stderr)(str, encoding, cb);
40
- }
41
- return originals.stdout.bind(process.stderr)(str, cb);
42
- }
43
- const restore = () => {
44
- process.stderr.write = originals.stderr;
45
- process.stdout.write = originals.stdout;
46
- process.env.NODE_ENV = originals.NODE_ENV;
47
- };
48
- const reset = () => {
49
- output.stderr = [];
50
- output.stdout = [];
51
- };
52
- const toString = (str) => RECORD_OPTIONS.stripAnsi ? ansis_1.default.strip(str.toString()) : str.toString();
53
- const getStderr = () => output.stderr.map((b) => toString(b)).join('');
54
- const getStdout = () => output.stdout.map((b) => toString(b)).join('');
55
12
  function traverseFilePathUntil(filename, predicate) {
56
13
  let current = filename;
57
14
  while (!predicate(current)) {
@@ -71,10 +28,35 @@ function makeLoadOptions(loadOpts) {
71
28
  return loadOpts ?? { root: findRoot() };
72
29
  }
73
30
  async function captureOutput(fn, opts) {
74
- RECORD_OPTIONS.print = opts?.print ?? false;
75
- RECORD_OPTIONS.stripAnsi = opts?.stripAnsi ?? true;
76
- process.stderr.write = mockedStderr;
77
- process.stdout.write = mockedStdout;
31
+ const print = opts?.print ?? false;
32
+ const stripAnsi = opts?.stripAnsi ?? true;
33
+ const originals = {
34
+ NODE_ENV: process.env.NODE_ENV,
35
+ stderr: process.stderr.write,
36
+ stdout: process.stdout.write,
37
+ };
38
+ const output = {
39
+ stderr: [],
40
+ stdout: [],
41
+ };
42
+ const toString = (str) => (stripAnsi ? ansis_1.default.strip(str.toString()) : str.toString());
43
+ const getStderr = () => output.stderr.map((b) => toString(b)).join('');
44
+ const getStdout = () => output.stdout.map((b) => toString(b)).join('');
45
+ const mock = (std) => (str, encoding, cb) => {
46
+ output[std].push(str);
47
+ if (print) {
48
+ if (encoding !== null && typeof encoding === 'function') {
49
+ cb = encoding;
50
+ encoding = undefined;
51
+ }
52
+ originals[std].apply(process[std], [str, encoding, cb]);
53
+ }
54
+ else if (typeof cb === 'function')
55
+ cb();
56
+ return true;
57
+ };
58
+ process.stdout.write = mock('stdout');
59
+ process.stderr.write = mock('stderr');
78
60
  process.env.NODE_ENV = 'test';
79
61
  try {
80
62
  const result = await fn();
@@ -86,15 +68,16 @@ async function captureOutput(fn, opts) {
86
68
  }
87
69
  catch (error) {
88
70
  return {
89
- ...(error instanceof core_1.Errors.CLIError && { error }),
90
- ...(error instanceof Error && { error }),
71
+ ...(error instanceof core_1.Errors.CLIError && { error: { ...error, message: toString(error.message) } }),
72
+ ...(error instanceof Error && { error: { ...error, message: toString(error.message) } }),
91
73
  stderr: getStderr(),
92
74
  stdout: getStdout(),
93
75
  };
94
76
  }
95
77
  finally {
96
- restore();
97
- reset();
78
+ process.stderr.write = originals.stderr;
79
+ process.stdout.write = originals.stdout;
80
+ process.env.NODE_ENV = originals.NODE_ENV;
98
81
  }
99
82
  }
100
83
  exports.captureOutput = captureOutput;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oclif/test",
3
3
  "description": "test helpers for oclif components",
4
- "version": "4.0.1-beta.1",
4
+ "version": "4.0.1-beta.2",
5
5
  "author": "Salesforce",
6
6
  "bugs": "https://github.com/oclif/test/issues",
7
7
  "dependencies": {
@@ -9,7 +9,7 @@
9
9
  "debug": "^4.3.4"
10
10
  },
11
11
  "peerDependencies": {
12
- "@oclif/core": "^4.0.0-beta.6"
12
+ "@oclif/core": "^4.0.0-beta.7"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@commitlint/config-conventional": "^18.6.3",