@oclif/test
Advanced tools
Comparing version 3.2.15 to 4.0.1-beta.0
@@ -1,117 +0,24 @@ | ||
/// <reference types="sinon" /> | ||
import * as fancyTest from 'fancy-test'; | ||
import { loadConfig } from './load-config'; | ||
export declare const test: fancyTest.FancyTypes.Base<fancyTest.FancyTypes.Context, { | ||
skip: { | ||
output: unknown; | ||
args: []; | ||
}; | ||
} & { | ||
only: { | ||
output: unknown; | ||
args: []; | ||
}; | ||
} & { | ||
retries: { | ||
output: unknown; | ||
args: any[]; | ||
}; | ||
} & { | ||
catch: { | ||
output: { | ||
error: Error; | ||
}; | ||
args: [arg: string | RegExp | ((err: Error) => any), opts?: { | ||
raiseIfNotThrown?: boolean | undefined; | ||
} | undefined]; | ||
}; | ||
} & { | ||
env: { | ||
output: unknown; | ||
args: [env: { | ||
[k: string]: string | null | undefined; | ||
}, opts?: fancyTest.FancyTypes.EnvOptions | undefined]; | ||
}; | ||
} & { | ||
stub: { | ||
output: { | ||
sandbox: import("sinon").SinonSandbox; | ||
}; | ||
args: [object: any, path: any, fn: (stub: import("sinon").SinonStub<any[], any>) => import("sinon").SinonStub<any[], any>]; | ||
}; | ||
} & { | ||
stdin: { | ||
output: unknown; | ||
args: [input: string, delay?: number | undefined]; | ||
}; | ||
} & { | ||
stderr: { | ||
output: { | ||
readonly stderr: string; | ||
}; | ||
args: [opts?: { | ||
print?: boolean | undefined; | ||
stripColor?: boolean | undefined; | ||
} | undefined]; | ||
}; | ||
} & { | ||
stdout: { | ||
output: { | ||
readonly stdout: string; | ||
}; | ||
args: [opts?: { | ||
print?: boolean | undefined; | ||
stripColor?: boolean | undefined; | ||
} | undefined]; | ||
}; | ||
} & { | ||
nock: { | ||
output: { | ||
nock: number; | ||
}; | ||
args: [host: string, options: fancyTest.FancyTypes.NockOptions | fancyTest.FancyTypes.NockCallback, cb?: fancyTest.FancyTypes.NockCallback | undefined]; | ||
}; | ||
} & { | ||
timeout: { | ||
output: { | ||
timeout: number; | ||
}; | ||
args: [timeout?: number | undefined]; | ||
}; | ||
} & { | ||
loadConfig: { | ||
output: { | ||
config: import("@oclif/core/lib/interfaces").Config; | ||
}; | ||
args: [opts?: loadConfig.Options | undefined]; | ||
}; | ||
} & { | ||
command: { | ||
output: { | ||
config: import("@oclif/core/lib/interfaces").Config; | ||
expectation: string; | ||
returned: unknown; | ||
}; | ||
args: [args: string | string[], opts?: loadConfig.Options | undefined]; | ||
}; | ||
} & { | ||
exit: { | ||
output: { | ||
error: import("@oclif/core/lib/errors").CLIError; | ||
}; | ||
args: [code?: number | undefined]; | ||
}; | ||
} & { | ||
hook: { | ||
output: { | ||
config: import("@oclif/core/lib/interfaces").Config; | ||
expectation: string; | ||
returned: unknown; | ||
}; | ||
args: [event: string, hookOpts?: Record<string, unknown> | undefined, options?: loadConfig.Options | undefined]; | ||
}; | ||
import { Errors, Interfaces } from '@oclif/core'; | ||
type CaptureOptions = { | ||
print?: boolean; | ||
stripAnsi?: boolean; | ||
}; | ||
export declare function captureOutput<T>(fn: () => Promise<unknown>, opts?: CaptureOptions): Promise<{ | ||
error?: Error & Partial<Errors.CLIError>; | ||
result?: T; | ||
stderr: string; | ||
stdout: string; | ||
}>; | ||
export default test; | ||
export { command } from './command'; | ||
export { Config } from '@oclif/core'; | ||
export { FancyTypes, expect } from 'fancy-test'; | ||
export declare function runCommand<T>(args: string | string[], loadOpts?: Interfaces.LoadOptions, captureOpts?: CaptureOptions): Promise<{ | ||
error?: Error & Partial<Errors.CLIError>; | ||
result?: T; | ||
stderr: string; | ||
stdout: string; | ||
}>; | ||
export declare function runHook<T>(hook: string, options: Record<string, unknown>, loadOpts?: Interfaces.LoadOptions, recordOpts?: CaptureOptions): Promise<{ | ||
error?: Error & Partial<Errors.CLIError>; | ||
result?: T; | ||
stderr: string; | ||
stdout: string; | ||
}>; | ||
export {}; |
151
lib/index.js
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -29,9 +6,49 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.expect = exports.FancyTypes = exports.Config = exports.command = exports.test = void 0; | ||
const fancyTest = __importStar(require("fancy-test")); | ||
exports.runHook = exports.runCommand = exports.captureOutput = void 0; | ||
const core_1 = require("@oclif/core"); | ||
const ansis_1 = __importDefault(require("ansis")); | ||
const debug_1 = __importDefault(require("debug")); | ||
const node_path_1 = require("node:path"); | ||
const command_1 = require("./command"); | ||
const exit_1 = __importDefault(require("./exit")); | ||
const hook_1 = __importDefault(require("./hook")); | ||
const load_config_1 = require("./load-config"); | ||
const debug = (0, debug_1.default)('oclif-test'); | ||
const RECORD_OPTIONS = { | ||
print: false, | ||
stripAnsi: true, | ||
}; | ||
const originals = { | ||
stderr: process.stderr.write, | ||
stdout: process.stdout.write, | ||
}; | ||
const output = { | ||
stderr: [], | ||
stdout: [], | ||
}; | ||
function mockedStdout(str, encoding, cb) { | ||
output.stdout.push(str); | ||
if (!RECORD_OPTIONS.print) | ||
return true; | ||
if (typeof encoding === 'string') { | ||
return originals.stdout.bind(process.stdout)(str, encoding, cb); | ||
} | ||
return originals.stdout.bind(process.stdout)(str, cb); | ||
} | ||
function mockedStderr(str, encoding, cb) { | ||
output.stderr.push(str); | ||
if (!RECORD_OPTIONS.print) | ||
return true; | ||
if (typeof encoding === 'string') { | ||
return originals.stdout.bind(process.stderr)(str, encoding, cb); | ||
} | ||
return originals.stdout.bind(process.stderr)(str, cb); | ||
} | ||
const restore = () => { | ||
process.stderr.write = originals.stderr; | ||
process.stdout.write = originals.stdout; | ||
}; | ||
const reset = () => { | ||
output.stderr = []; | ||
output.stdout = []; | ||
}; | ||
const toString = (str) => RECORD_OPTIONS.stripAnsi ? ansis_1.default.strip(str.toString()) : str.toString(); | ||
const getStderr = () => output.stderr.map((b) => toString(b)).join(''); | ||
const getStdout = () => output.stdout.map((b) => toString(b)).join(''); | ||
function traverseFilePathUntil(filename, predicate) { | ||
@@ -44,22 +61,58 @@ let current = filename; | ||
} | ||
/* eslint-disable unicorn/prefer-module */ | ||
load_config_1.loadConfig.root = | ||
process.env.OCLIF_TEST_ROOT ?? | ||
function findRoot() { | ||
return (process.env.OCLIF_TEST_ROOT ?? | ||
// eslint-disable-next-line unicorn/prefer-module | ||
Object.values(require.cache).find((m) => m?.children.includes(module))?.filename ?? | ||
traverseFilePathUntil(require.main?.path ?? module.path, (p) => !(p.includes('node_modules') || p.includes('.pnpm') || p.includes('.yarn'))); | ||
/* eslint-enable unicorn/prefer-module */ | ||
// Using a named export to import fancy causes this issue: https://github.com/oclif/test/issues/516 | ||
exports.test = fancyTest.fancy | ||
.register('loadConfig', load_config_1.loadConfig) | ||
.register('command', command_1.command) | ||
.register('exit', exit_1.default) | ||
.register('hook', hook_1.default) | ||
.env({ NODE_ENV: 'test' }); | ||
exports.default = exports.test; | ||
var command_2 = require("./command"); | ||
Object.defineProperty(exports, "command", { enumerable: true, get: function () { return command_2.command; } }); | ||
var core_1 = require("@oclif/core"); | ||
Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return core_1.Config; } }); | ||
var fancy_test_1 = require("fancy-test"); | ||
Object.defineProperty(exports, "FancyTypes", { enumerable: true, get: function () { return fancy_test_1.FancyTypes; } }); | ||
Object.defineProperty(exports, "expect", { enumerable: true, get: function () { return fancy_test_1.expect; } }); | ||
traverseFilePathUntil( | ||
// eslint-disable-next-line unicorn/prefer-module | ||
require.main?.path ?? module.path, (p) => !(p.includes('node_modules') || p.includes('.pnpm') || p.includes('.yarn')))); | ||
} | ||
function makeLoadOptions(loadOpts) { | ||
return loadOpts ?? { root: findRoot() }; | ||
} | ||
async function captureOutput(fn, opts) { | ||
RECORD_OPTIONS.print = opts?.print ?? false; | ||
RECORD_OPTIONS.stripAnsi = opts?.stripAnsi ?? true; | ||
process.stderr.write = mockedStderr; | ||
process.stdout.write = mockedStdout; | ||
try { | ||
const result = await fn(); | ||
return { | ||
result: result, | ||
stderr: getStderr(), | ||
stdout: getStdout(), | ||
}; | ||
} | ||
catch (error) { | ||
return { | ||
...(error instanceof core_1.Errors.CLIError && { error }), | ||
...(error instanceof Error && { error }), | ||
stderr: getStderr(), | ||
stdout: getStdout(), | ||
}; | ||
} | ||
finally { | ||
restore(); | ||
reset(); | ||
} | ||
} | ||
exports.captureOutput = captureOutput; | ||
async function runCommand(args, loadOpts, captureOpts) { | ||
const loadOptions = makeLoadOptions(loadOpts); | ||
const argsArray = (Array.isArray(args) ? args : [args]).join(' ').split(' '); | ||
const [id, ...rest] = argsArray; | ||
const finalArgs = id === '.' ? rest : argsArray; | ||
debug('loadOpts: %O', loadOpts); | ||
debug('args: %O', finalArgs); | ||
return captureOutput(async () => (0, core_1.run)(finalArgs, loadOptions), captureOpts); | ||
} | ||
exports.runCommand = runCommand; | ||
async function runHook(hook, options, loadOpts, recordOpts) { | ||
const loadOptions = makeLoadOptions(loadOpts); | ||
debug('loadOpts: %O', loadOpts); | ||
return captureOutput(async () => { | ||
const config = await core_1.Config.load(loadOptions); | ||
return config.runHook(hook, options); | ||
}, recordOpts); | ||
} | ||
exports.runHook = runHook; |
{ | ||
"name": "@oclif/test", | ||
"description": "test helpers for oclif components", | ||
"version": "3.2.15", | ||
"version": "4.0.1-beta.0", | ||
"author": "Salesforce", | ||
"bugs": "https://github.com/oclif/test/issues", | ||
"dependencies": { | ||
"@oclif/core": "^3.26.6", | ||
"chai": "^4.4.1", | ||
"fancy-test": "^3.0.15" | ||
"ansis": "^3.2.0", | ||
"debug": "^4.3.4" | ||
}, | ||
"peerDependencies": { | ||
"@oclif/core": "^4.0.0-beta.6" | ||
}, | ||
"devDependencies": { | ||
"@commitlint/config-conventional": "^18.6.3", | ||
"@oclif/core": "^4.0.0-beta.6", | ||
"@oclif/prettier-config": "^0.2.1", | ||
"@types/cli-progress": "^3.11.5", | ||
"@types/chai": "^4.3.16", | ||
"@types/debug": "^4.1.12", | ||
"@types/mocha": "^10", | ||
"@types/node": "^18", | ||
"chai": "^5.1.1", | ||
"commitlint": "^18.6.1", | ||
@@ -26,6 +31,5 @@ "eslint": "^8.57.0", | ||
"mocha": "^10", | ||
"nock": "^13.5.4", | ||
"prettier": "^3.2.5", | ||
"shx": "^0.3.3", | ||
"ts-node": "^10.9.2", | ||
"tsx": "^4.10.2", | ||
"typescript": "^5.4.5" | ||
@@ -32,0 +36,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13686
20
5
140
1
1
+ Addedansis@^3.2.0
+ Addeddebug@^4.3.4
+ Added@oclif/core@4.0.32(transitive)
+ Addedansis@3.3.2(transitive)
+ Addedcli-spinners@2.9.2(transitive)
+ Addedlilconfig@3.1.2(transitive)
+ Addedsemver@7.6.3(transitive)
- Removed@oclif/core@^3.26.6
- Removedchai@^4.4.1
- Removedfancy-test@^3.0.15
- Removed@oclif/core@3.27.0(transitive)
- Removed@sinonjs/commons@3.0.1(transitive)
- Removed@sinonjs/fake-timers@10.3.011.3.1(transitive)
- Removed@sinonjs/samsam@8.0.2(transitive)
- Removed@sinonjs/text-encoding@0.7.3(transitive)
- Removed@types/chai@5.0.1(transitive)
- Removed@types/cli-progress@3.11.6(transitive)
- Removed@types/deep-eql@4.0.2(transitive)
- Removed@types/lodash@4.17.13(transitive)
- Removed@types/node@22.9.0(transitive)
- Removed@types/sinon@17.0.3(transitive)
- Removed@types/sinonjs__fake-timers@8.1.5(transitive)
- Removedansicolors@0.3.2(transitive)
- Removedargparse@1.0.10(transitive)
- Removedassertion-error@1.1.0(transitive)
- Removedastral-regex@2.0.0(transitive)
- Removedcardinal@2.1.1(transitive)
- Removedchai@4.5.0(transitive)
- Removedcheck-error@1.0.3(transitive)
- Removedcli-progress@3.12.0(transitive)
- Removedcolor@4.2.3(transitive)
- Removedcolor-string@1.9.1(transitive)
- Removedcross-spawn@7.0.5(transitive)
- Removeddeep-eql@4.1.4(transitive)
- Removeddiff@5.2.0(transitive)
- Removedesprima@4.0.1(transitive)
- Removedfancy-test@3.0.16(transitive)
- Removedget-func-name@2.0.2(transitive)
- Removedhyperlinker@1.0.0(transitive)
- Removedis-arrayish@0.3.2(transitive)
- Removedisexe@2.0.0(transitive)
- Removedjs-yaml@3.14.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjust-extend@6.2.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedlodash.get@4.4.2(transitive)
- Removedloupe@2.3.7(transitive)
- Removedmock-stdin@1.0.0(transitive)
- Removednatural-orderby@2.0.3(transitive)
- Removednise@5.1.9(transitive)
- Removednock@13.5.6(transitive)
- Removedobject-treeify@1.1.33(transitive)
- Removedpassword-prompt@1.1.3(transitive)
- Removedpath-key@3.1.1(transitive)
- Removedpath-to-regexp@6.3.0(transitive)
- Removedpathval@1.1.1(transitive)
- Removedpropagate@2.0.1(transitive)
- Removedredeyed@2.1.1(transitive)
- Removedshebang-command@2.0.0(transitive)
- Removedshebang-regex@3.0.0(transitive)
- Removedsimple-swizzle@0.2.2(transitive)
- Removedsinon@16.1.3(transitive)
- Removedslice-ansi@4.0.0(transitive)
- Removedsprintf-js@1.0.3(transitive)
- Removedstdout-stderr@0.1.13(transitive)
- Removedsupports-hyperlinks@2.3.0(transitive)
- Removedtype-detect@4.0.84.1.0(transitive)
- Removedundici-types@6.19.8(transitive)
- Removedwhich@2.0.2(transitive)