Skip to content

Commit

Permalink
switch TestAdapter to implement AdapterWithProgress
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed May 26, 2023
1 parent 1a1231d commit be623b3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"doc_path": "../yeoman-test-doc"
},
"dependencies": {
"@yeoman/adapter": "^1.0.2",
"@yeoman/adapter": "^1.0.4",
"inquirer": "^9.2.2",
"lodash": "^4.17.21",
"mem-fs": "^3.0.0",
Expand Down
18 changes: 15 additions & 3 deletions src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import events from 'node:events';
import { PassThrough } from 'node:stream';
import { createLogger } from '@yeoman/adapter';
import { createLogger, type AdapterWithProgress } from '@yeoman/adapter';
import { spy as sinonSpy, stub as sinonStub } from 'sinon';
import type { PromptAnswers, PromptQuestion, Logger, InputOutputAdapter, PromptQuestions } from '@yeoman/types';
import type { PromptAnswers, PromptQuestion, Logger, QueuedAdapter, PromptQuestions, Task } from '@yeoman/types';
import { createPromptModule, type PromptModule } from 'inquirer';

export type DummyPromptCallback = (answer: any, { question, answers }: { question: PromptQuestion; answers: PromptAnswers }) => any;
Expand Down Expand Up @@ -73,7 +73,7 @@ export class DummyPrompt {
}
}

export class TestAdapter implements InputOutputAdapter {
export class TestAdapter implements AdapterWithProgress {
promptModule: PromptModule;
diff: any;
log: Logger;
Expand Down Expand Up @@ -119,6 +119,18 @@ export class TestAdapter implements InputOutputAdapter {
}
}

async queue<TaskResultType>(fn: Task<TaskResultType>): Promise<void | TaskResultType> {
return fn(this);
}

async progress<ReturnType>(
fn: (progress: { step: (prefix: string, message: string, ...args: any[]) => void }) => ReturnType,
_options?: { disabled?: boolean | undefined; name?: string | undefined } | undefined,
): Promise<void | ReturnType> {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return fn({ step() {} });
}

close(): void {
this.promptModule.restoreDefaultPrompts();
}
Expand Down
18 changes: 18 additions & 0 deletions test/adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'node:assert';
import { expect } from 'esmocha';
import { TestAdapter } from '../src/adapter.js';

describe('TestAdapter', function () {
Expand All @@ -14,4 +15,21 @@ describe('TestAdapter', function () {
});
});
});
describe('#queue()', function () {
it('should execute the callback', async function () {
const adapter = new TestAdapter();
await expect(adapter.queue(() => 2)).resolves.toBe(2);
});
});
describe('#progress()', function () {
it('should execute the callback', async function () {
const adapter = new TestAdapter();
await expect(
adapter.progress(({ step }) => {
step('prefix', 'msg');
return 2;
}),
).resolves.toBe(2);
});
});
});

0 comments on commit be623b3

Please sign in to comment.