yeoman-test 6.2.0 → 7.0.0

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.
@@ -0,0 +1,279 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import { EventEmitter } from 'node:events';
3
+ import type Generator from 'yeoman-generator';
4
+ import type Environment from 'yeoman-environment';
5
+ import { type LookupOptions, type Options } from 'yeoman-environment';
6
+ import MemFsEditor from 'mem-fs-editor';
7
+ import RunResult from './run-result.js';
8
+ import { type GeneratorConstructor, type Dependency, type YeomanTest } from './helpers.js';
9
+ import { type DummyPromptOptions } from './adapter.js';
10
+ /**
11
+ * Provides settings for creating a `RunContext`.
12
+ */
13
+ export type RunContextSettings = {
14
+ /**
15
+ * Automatically run this generator in a tmp dir
16
+ * @default true
17
+ */
18
+ tmpdir?: boolean;
19
+ cwd?: string;
20
+ oldCwd?: string;
21
+ forwardCwd?: boolean;
22
+ autoCleanup?: boolean;
23
+ /**
24
+ * File path to the generator (only used if Generator is a constructor)
25
+ */
26
+ resolved?: string;
27
+ /**
28
+ * Namespace (only used if Generator is a constructor)
29
+ * @default 'gen:test'
30
+ */
31
+ namespace?: string;
32
+ };
33
+ type PromiseRunResult<GeneratorType extends Generator> = Promise<RunResult<GeneratorType>>;
34
+ export declare class RunContextBase<GeneratorType extends Generator> extends EventEmitter {
35
+ readonly mockedGenerators: Record<string, Generator>;
36
+ env: Environment;
37
+ generator: GeneratorType;
38
+ readonly settings: RunContextSettings;
39
+ readonly envOptions: Environment.Options;
40
+ completed: boolean;
41
+ targetDirectory?: string;
42
+ editor: MemFsEditor.Editor;
43
+ protected environmentPromise?: PromiseRunResult<GeneratorType>;
44
+ private args;
45
+ private options;
46
+ private answers?;
47
+ private readonly onGeneratorCallbacks;
48
+ private readonly onTargetDirectoryCallbacks;
49
+ private readonly onEnvironmentCallbacks;
50
+ private readonly inDirCallbacks;
51
+ private readonly Generator;
52
+ private readonly helpers;
53
+ private readonly temporaryDir;
54
+ private oldCwd?;
55
+ private eventListenersSet;
56
+ private envCB;
57
+ private ran;
58
+ private errored;
59
+ /**
60
+ * This class provide a run context object to façade the complexity involved in setting
61
+ * up a generator for testing
62
+ * @constructor
63
+ * @param Generator - Namespace or generator constructor. If the later
64
+ * is provided, then namespace is assumed to be
65
+ * 'gen:test' in all cases
66
+ * @param settings
67
+ * @return {this}
68
+ */
69
+ constructor(generatorType: string | GeneratorConstructor | typeof Generator, settings?: RunContextSettings, envOptions?: Options, helpers?: YeomanTest);
70
+ /**
71
+ * Run the generator on the environment and promises a RunResult instance.
72
+ * @return {PromiseRunResult} Promise a RunResult instance.
73
+ */
74
+ run(): PromiseRunResult<GeneratorType>;
75
+ on(eventName: string | symbol, listener: (...args: any[]) => void): this;
76
+ /**
77
+ * @deprecated
78
+ * Clean the provided directory, then change directory into it
79
+ * @param dirPath - Directory path (relative to CWD). Prefer passing an absolute
80
+ * file path for predictable results
81
+ * @param [cb] - callback who'll receive the folder path as argument
82
+ * @return run context instance
83
+ */
84
+ inDir(dirPath: string, cb?: (folderPath: string) => void): this;
85
+ /**
86
+ * Register an callback to prepare the destination folder.
87
+ * @param [cb] - callback who'll receive the folder path as argument
88
+ * @return this - run context instance
89
+ */
90
+ doInDir(cb: (folderPath: string) => void): this;
91
+ /**
92
+ * @deprecated
93
+ * Change directory without deleting directory content.
94
+ * @param dirPath - Directory path (relative to CWD). Prefer passing an absolute
95
+ * file path for predictable results
96
+ * @return run context instance
97
+ */
98
+ cd(dirPath: string): this;
99
+ /**
100
+ * Cleanup a temporary directory and change the CWD into it
101
+ *
102
+ * This method is called automatically when creating a RunContext. Only use it if you need
103
+ * to use the callback.
104
+ *
105
+ * @param [cb] - callback who'll receive the folder path as argument
106
+ * @return this - run context instance
107
+ */
108
+ inTmpDir(cb?: (folderPath: string) => void): this;
109
+ /**
110
+ * Restore cwd to initial cwd.
111
+ * @return {this} run context instance
112
+ */
113
+ restore(): this;
114
+ /**
115
+ * Clean the directory used for tests inside inDir/inTmpDir
116
+ * @param {Boolean} force - force directory cleanup for not tmpdir
117
+ */
118
+ cleanup(): void;
119
+ /**
120
+ * Clean the directory used for tests inside inDir/inTmpDir
121
+ * @param {Boolean} force - force directory cleanup for not tmpdir
122
+ */
123
+ cleanupTemporaryDir(): void;
124
+ /**
125
+ * Clean the directory used for tests inside inDir/inTmpDir
126
+ * @param force - force directory cleanup for not tmpdir
127
+ */
128
+ cleanTestDirectory(force?: boolean): void;
129
+ /**
130
+ * Create an environment
131
+ *
132
+ * This method is called automatically when creating a RunContext. Only use it if you need
133
+ * to use the callback.
134
+ *
135
+ * @param {Function} [cb] - callback who'll receive the folder path as argument
136
+ * @return {this} run context instance
137
+ */
138
+ withEnvironment(cb: any): this;
139
+ /**
140
+ * Run lookup on the environment.
141
+ *
142
+ * @param lookups - lookup to run.
143
+ */
144
+ withLookups(lookups: LookupOptions | LookupOptions[]): this;
145
+ /**
146
+ * Provide arguments to the run context
147
+ * @param args - command line arguments as Array or space separated string
148
+ */
149
+ withArguments(args: string | string[]): this;
150
+ /**
151
+ * Provide options to the run context
152
+ * @param {Object} options - command line options (e.g. `--opt-one=foo`)
153
+ * @return {this}
154
+ */
155
+ withOptions(options: any): this;
156
+ /**
157
+ * @deprecated
158
+ * Mock the prompt with dummy answers
159
+ * @param answers - Answers to the prompt questions
160
+ * @param options - Options or callback.
161
+ * @param {Function} [options.callback] - Callback.
162
+ * @param {Boolean} [options.throwOnMissingAnswer] - Throw if a answer is missing.
163
+ * @return {this}
164
+ */
165
+ withPrompts(answers: Generator.Answers, options?: DummyPromptOptions): this;
166
+ /**
167
+ * Mock answers for prompts
168
+ * @param answers - Answers to the prompt questions
169
+ * @param options - Options or callback.
170
+ * @return {this}
171
+ */
172
+ withAnswers(answers: Generator.Answers, options?: DummyPromptOptions): this;
173
+ /**
174
+ * Provide dependent generators
175
+ * @param {Array} dependencies - paths to the generators dependencies
176
+ * @return {this}
177
+ * @example
178
+ * var angular = new RunContext('../../app');
179
+ * angular.withGenerators([
180
+ * '../../common',
181
+ * '../../controller',
182
+ * '../../main',
183
+ * [helpers.createDummyGenerator(), 'testacular:app']
184
+ * ]);
185
+ * angular.on('end', function () {
186
+ * // assert something
187
+ * });
188
+ */
189
+ withGenerators(dependencies: Dependency[]): this;
190
+ /**
191
+ * Create mocked generators
192
+ * @param namespaces - namespaces of mocked generators
193
+ * @return this
194
+ * @example
195
+ * var angular = helpers
196
+ * .create('../../app')
197
+ * .withMockedGenerators([
198
+ * 'foo:app',
199
+ * 'foo:bar',
200
+ * ])
201
+ * .run()
202
+ * .then(runResult => assert(runResult
203
+ * .mockedGenerators['foo:app']
204
+ .calledOnce));
205
+ */
206
+ withMockedGenerators(namespaces: string[]): this;
207
+ /**
208
+ * Mock the local configuration with the provided config
209
+ * @param localConfig - should look just like if called config.getAll()
210
+ */
211
+ withLocalConfig(localConfig: Record<string, unknown>): this;
212
+ /**
213
+ * Add files to mem-fs.
214
+ * Files will be resolved relative to targetDir.
215
+ * @param files
216
+ */
217
+ withFiles(files: Record<string, string | Record<string, unknown>>): this;
218
+ withFiles(relativePath: string, files: Record<string, string | Record<string, unknown>>): this;
219
+ /**
220
+ * Add .yo-rc.json to mem-fs.
221
+ *
222
+ * @param content
223
+ * @returns
224
+ */
225
+ withYoRc(content: string | Record<string, unknown>): this;
226
+ /**
227
+ * Commit mem-fs files.
228
+ */
229
+ commitFiles(): this;
230
+ /**
231
+ * Execute callback after targetDirectory is set
232
+ * @param callback
233
+ * @returns
234
+ */
235
+ onTargetDirectory(callback: (this: this, targetDirectory: string) => any): this;
236
+ /**
237
+ * Execute callback after generator is ready
238
+ * @param callback
239
+ * @returns
240
+ */
241
+ onGenerator(callback: (this: this, generator: GeneratorType) => any): this;
242
+ /**
243
+ * Execute callback after environment is ready
244
+ * @param callback
245
+ * @returns
246
+ */
247
+ onEnvironment(callback: (this: this, env: Environment) => any): this;
248
+ protected assertNotBuild(): void;
249
+ /**
250
+ * Build the generator and the environment.
251
+ * @return {RunContext|false} this
252
+ */
253
+ protected build(): Promise<void>;
254
+ /**
255
+ * Return a promise representing the generator run process
256
+ * @return Promise resolved on end or rejected on error
257
+ */
258
+ protected toPromise(): PromiseRunResult<GeneratorType>;
259
+ /**
260
+ * Keeps compatibility with events
261
+ */
262
+ private setupEventListeners;
263
+ /**
264
+ * Set the target directory.
265
+ * @private
266
+ * @param {String} dirPath - Directory path (relative to CWD). Prefer passing an absolute
267
+ * file path for predictable results
268
+ * @return {this} run context instance
269
+ */
270
+ private setDir;
271
+ private _createRunResultOptions;
272
+ }
273
+ export default class RunContext<GeneratorType extends Generator> extends RunContextBase<GeneratorType> implements Promise<RunResult<GeneratorType>> {
274
+ then<TResult1 = RunResult<GeneratorType>, TResult2 = never>(onfulfilled?: ((value: RunResult<GeneratorType>) => TResult1 | PromiseLike<TResult1>) | undefined | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | undefined): Promise<TResult1 | TResult2>;
275
+ catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | undefined): Promise<RunResult<GeneratorType> | TResult>;
276
+ finally(onfinally?: (() => void) | undefined | undefined): Promise<RunResult<GeneratorType>>;
277
+ get [Symbol.toStringTag](): string;
278
+ }
279
+ export {};