Skip to content

Commit

Permalink
refactor: Migrate to unambiguous promptTextInput()
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Dec 10, 2020
1 parent 46fa111 commit e9237f3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions commands/version/__tests__/prompt-version.test.js
Expand Up @@ -66,7 +66,7 @@ describe("custom version", () => {
beforeEach(() => {
prompt.mockChoices("CUSTOM");

prompt.input.mockImplementationOnce((msg, cfg) => {
prompt.promptTextInput.mockImplementationOnce((msg, cfg) => {
inputFilter = cfg.filter;
inputValidate = cfg.validate;

Expand Down Expand Up @@ -104,7 +104,7 @@ describe("custom prerelease", () => {
beforeEach(() => {
prompt.mockChoices("PRERELEASE");

prompt.input.mockImplementationOnce((msg, cfg) => {
prompt.promptTextInput.mockImplementationOnce((msg, cfg) => {
inputFilter = cfg.filter;

return Promise.resolve(msg);
Expand Down
2 changes: 1 addition & 1 deletion commands/version/__tests__/version-bump-prerelease.test.js
Expand Up @@ -153,7 +153,7 @@ test("independent version prerelease does not bump on every unrelated change", a

// simulate choices for pkg-a then pkg-b
prompt.mockChoices("patch", "PRERELEASE");
prompt.input.mockImplementationOnce((msg, cfg) =>
prompt.promptTextInput.mockImplementationOnce((msg, cfg) =>
// the _existing_ "bumps" prerelease ID should be preserved
Promise.resolve(cfg.filter())
);
Expand Down
4 changes: 2 additions & 2 deletions commands/version/lib/prompt-version.js
Expand Up @@ -44,7 +44,7 @@ function promptVersion(currentVersion, name, prereleaseId) {
],
}).then((choice) => {
if (choice === "CUSTOM") {
return PromptUtilities.input("Enter a custom version", {
return PromptUtilities.promptTextInput("Enter a custom version", {
filter: semver.valid,
// semver.valid() always returns null with invalid input
validate: (v) => v !== null || "Must be a valid semver version",
Expand All @@ -55,7 +55,7 @@ function promptVersion(currentVersion, name, prereleaseId) {
const defaultVersion = semver.inc(currentVersion, "prerelease", prereleaseId);
const prompt = `(default: "${prereleaseId}", yielding ${defaultVersion})`;

return PromptUtilities.input(`Enter a prerelease identifier ${prompt}`, {
return PromptUtilities.promptTextInput(`Enter a prerelease identifier ${prompt}`, {
filter: (v) => semver.inc(currentVersion, "prerelease", v || prereleaseId),
});
}
Expand Down
26 changes: 13 additions & 13 deletions core/otplease/__tests__/otplease.test.js
Expand Up @@ -9,7 +9,7 @@ const prompt = require("@lerna/prompt");
const { otplease, getOneTimePassword } = require("..");

// global mock setup
prompt.input.mockResolvedValue("123456");
prompt.promptTextInput.mockResolvedValue("123456");

describe("@lerna/otplease", () => {
const stdinIsTTY = process.stdin.isTTY;
Expand All @@ -31,7 +31,7 @@ describe("@lerna/otplease", () => {
const result = await otplease(fn, {});

expect(fn).toHaveBeenCalled();
expect(prompt.input).not.toHaveBeenCalled();
expect(prompt.promptTextInput).not.toHaveBeenCalled();
expect(result).toBe(obj);
});

Expand All @@ -41,7 +41,7 @@ describe("@lerna/otplease", () => {
const result = await otplease(fn, {});

expect(fn).toHaveBeenCalledTimes(2);
expect(prompt.input).toHaveBeenCalled();
expect(prompt.promptTextInput).toHaveBeenCalled();
expect(result).toBe(obj);
});

Expand All @@ -52,7 +52,7 @@ describe("@lerna/otplease", () => {

const result = await otplease(fn, {}, otpCache);
expect(fn).toHaveBeenCalledTimes(2);
expect(prompt.input).toHaveBeenCalled();
expect(prompt.promptTextInput).toHaveBeenCalled();
expect(result).toBe(obj);
expect(otpCache.otp).toBe("123456");
});
Expand All @@ -64,7 +64,7 @@ describe("@lerna/otplease", () => {
const result = await otplease(fn, {}, otpCache);

expect(fn).toHaveBeenCalledTimes(1);
expect(prompt.input).not.toHaveBeenCalled();
expect(prompt.promptTextInput).not.toHaveBeenCalled();
expect(result).toBe(obj);
expect(otpCache.otp).toBe("654321");
});
Expand All @@ -76,7 +76,7 @@ describe("@lerna/otplease", () => {
const result = await otplease(fn, { otp: "987654" }, otpCache);

expect(fn).toHaveBeenCalledTimes(1);
expect(prompt.input).not.toHaveBeenCalled();
expect(prompt.promptTextInput).not.toHaveBeenCalled();
expect(result).toBe(obj);
// do not replace cache
expect(otpCache.otp).toBe("654321");
Expand All @@ -95,7 +95,7 @@ describe("@lerna/otplease", () => {
// start intial otplease call, 'catch' will happen in next turn *after* the cache is set.
const result = await otplease(fn, {}, otpCache);
expect(fn).toHaveBeenCalledTimes(2);
expect(prompt.input).not.toHaveBeenCalled();
expect(prompt.promptTextInput).not.toHaveBeenCalled();
expect(result).toBe(obj);
});

Expand All @@ -117,13 +117,13 @@ describe("@lerna/otplease", () => {
expect(fn1).toHaveBeenCalledTimes(2);
expect(fn2).toHaveBeenCalledTimes(2);
// only prompt once for the two concurrent requests
expect(prompt.input).toHaveBeenCalledTimes(1);
expect(prompt.promptTextInput).toHaveBeenCalledTimes(1);
expect(res1).toBe(obj1);
expect(res2).toBe(obj2);
});

it("strips whitespace from OTP prompt value", async () => {
prompt.input.mockImplementationOnce((msg, opts) => Promise.resolve(opts.filter(" 121212 ")));
prompt.promptTextInput.mockImplementationOnce((msg, opts) => Promise.resolve(opts.filter(" 121212 ")));

const obj = {};
const fn = jest.fn(makeTestCallback("121212", obj));
Expand All @@ -133,7 +133,7 @@ describe("@lerna/otplease", () => {
});

it("validates OTP prompt response", async () => {
prompt.input.mockImplementationOnce((msg, opts) =>
prompt.promptTextInput.mockImplementationOnce((msg, opts) =>
Promise.resolve(opts.validate("i am the very model of a modern major general"))
);

Expand All @@ -144,7 +144,7 @@ describe("@lerna/otplease", () => {
});

it("rejects prompt errors", async () => {
prompt.input.mockImplementationOnce(() => Promise.reject(new Error("poopypants")));
prompt.promptTextInput.mockImplementationOnce(() => Promise.reject(new Error("poopypants")));

const obj = {};
const fn = jest.fn(makeTestCallback("343434", obj));
Expand Down Expand Up @@ -189,7 +189,7 @@ describe("@lerna/otplease", () => {
it("defaults message argument", async () => {
await getOneTimePassword();

expect(prompt.input).toHaveBeenCalledWith(
expect(prompt.promptTextInput).toHaveBeenCalledWith(
"This operation requires a one-time password:",
expect.any(Object)
);
Expand All @@ -198,7 +198,7 @@ describe("@lerna/otplease", () => {
it("accepts custom message", async () => {
await getOneTimePassword("foo bar");

expect(prompt.input).toHaveBeenCalledWith("foo bar", expect.any(Object));
expect(prompt.promptTextInput).toHaveBeenCalledWith("foo bar", expect.any(Object));
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion core/otplease/otplease.js
Expand Up @@ -105,7 +105,7 @@ function attempt(fn, opts, otpCache) {
*/
function getOneTimePassword(message = "This operation requires a one-time password:") {
// Logic taken from npm internals: https://git.io/fNoMe
return prompt.input(message, {
return prompt.promptTextInput(message, {
filter: (otp) => otp.replace(/\s+/g, ""),
validate: (otp) =>
(otp && /^[\d ]+$|^[A-Fa-f0-9]{64,64}$/.test(otp)) ||
Expand Down

0 comments on commit e9237f3

Please sign in to comment.