From ca71428acb197f3bbfe123d179ef62ea76482167 Mon Sep 17 00:00:00 2001 From: Tim Reichen Date: Tue, 17 Dec 2024 04:36:00 +0100 Subject: [PATCH] test(cli): add `promptSecret()` empty mask option test (#6273) --- cli/prompt_secret_test.ts | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/cli/prompt_secret_test.ts b/cli/prompt_secret_test.ts index ac2d257f1abf..aa6c3860678b 100644 --- a/cli/prompt_secret_test.ts +++ b/cli/prompt_secret_test.ts @@ -394,6 +394,55 @@ Deno.test("promptSecret() handles mask option", () => { restore(); }); +Deno.test("promptSecret() handles empty mask option", () => { + stub(Deno.stdin, "setRaw"); + stub(Deno.stdin, "isTerminal", () => true); + + const expectedOutput = [ + "Please provide the password: ", + "\n", + ]; + + const actualOutput: string[] = []; + + stub( + Deno.stdout, + "writeSync", + (data: Uint8Array) => { + const output = decoder.decode(data); + actualOutput.push(output); + return data.length; + }, + ); + + let readIndex = 0; + + const inputs = [ + "d", + "e", + "n", + "o", + "\r", + ]; + + stub( + Deno.stdin, + "readSync", + (data: Uint8Array) => { + const input = inputs[readIndex++]; + const bytes = encoder.encode(input); + data.set(bytes); + return bytes.length; + }, + ); + + const password = promptSecret("Please provide the password:", { mask: "" }); + + assertEquals(password, "deno"); + assertEquals(expectedOutput, actualOutput); + restore(); +}); + Deno.test("promptSecret() returns null if Deno.stdin.isTerminal() is false", () => { stub(Deno.stdin, "setRaw"); stub(Deno.stdin, "isTerminal", () => false);