@oclif/test 4.0.1-beta.0 → 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 +37 -51
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -9,47 +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
- stderr: process.stderr.write,
18
- stdout: process.stdout.write,
19
- };
20
- const output = {
21
- stderr: [],
22
- stdout: [],
23
- };
24
- function mockedStdout(str, encoding, cb) {
25
- output.stdout.push(str);
26
- if (!RECORD_OPTIONS.print)
27
- return true;
28
- if (typeof encoding === 'string') {
29
- return originals.stdout.bind(process.stdout)(str, encoding, cb);
30
- }
31
- return originals.stdout.bind(process.stdout)(str, cb);
32
- }
33
- function mockedStderr(str, encoding, cb) {
34
- output.stderr.push(str);
35
- if (!RECORD_OPTIONS.print)
36
- return true;
37
- if (typeof encoding === 'string') {
38
- return originals.stdout.bind(process.stderr)(str, encoding, cb);
39
- }
40
- return originals.stdout.bind(process.stderr)(str, cb);
41
- }
42
- const restore = () => {
43
- process.stderr.write = originals.stderr;
44
- process.stdout.write = originals.stdout;
45
- };
46
- const reset = () => {
47
- output.stderr = [];
48
- output.stdout = [];
49
- };
50
- const toString = (str) => RECORD_OPTIONS.stripAnsi ? ansis_1.default.strip(str.toString()) : str.toString();
51
- const getStderr = () => output.stderr.map((b) => toString(b)).join('');
52
- const getStdout = () => output.stdout.map((b) => toString(b)).join('');
53
12
  function traverseFilePathUntil(filename, predicate) {
54
13
  let current = filename;
55
14
  while (!predicate(current)) {
@@ -69,10 +28,36 @@ function makeLoadOptions(loadOpts) {
69
28
  return loadOpts ?? { root: findRoot() };
70
29
  }
71
30
  async function captureOutput(fn, opts) {
72
- RECORD_OPTIONS.print = opts?.print ?? false;
73
- RECORD_OPTIONS.stripAnsi = opts?.stripAnsi ?? true;
74
- process.stderr.write = mockedStderr;
75
- 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');
60
+ process.env.NODE_ENV = 'test';
76
61
  try {
77
62
  const result = await fn();
78
63
  return {
@@ -83,15 +68,16 @@ async function captureOutput(fn, opts) {
83
68
  }
84
69
  catch (error) {
85
70
  return {
86
- ...(error instanceof core_1.Errors.CLIError && { error }),
87
- ...(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) } }),
88
73
  stderr: getStderr(),
89
74
  stdout: getStdout(),
90
75
  };
91
76
  }
92
77
  finally {
93
- restore();
94
- reset();
78
+ process.stderr.write = originals.stderr;
79
+ process.stdout.write = originals.stdout;
80
+ process.env.NODE_ENV = originals.NODE_ENV;
95
81
  }
96
82
  }
97
83
  exports.captureOutput = captureOutput;
@@ -100,14 +86,14 @@ async function runCommand(args, loadOpts, captureOpts) {
100
86
  const argsArray = (Array.isArray(args) ? args : [args]).join(' ').split(' ');
101
87
  const [id, ...rest] = argsArray;
102
88
  const finalArgs = id === '.' ? rest : argsArray;
103
- debug('loadOpts: %O', loadOpts);
89
+ debug('loadOpts: %O', loadOptions);
104
90
  debug('args: %O', finalArgs);
105
91
  return captureOutput(async () => (0, core_1.run)(finalArgs, loadOptions), captureOpts);
106
92
  }
107
93
  exports.runCommand = runCommand;
108
94
  async function runHook(hook, options, loadOpts, recordOpts) {
109
95
  const loadOptions = makeLoadOptions(loadOpts);
110
- debug('loadOpts: %O', loadOpts);
96
+ debug('loadOpts: %O', loadOptions);
111
97
  return captureOutput(async () => {
112
98
  const config = await core_1.Config.load(loadOptions);
113
99
  return config.runHook(hook, options);
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.0",
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",