-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(cli): Handle overflow in promptSecret
#6318
Merged
+252
−2
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
485c1ca
wip
cosmastech 3efccdb
Update prompt_secret.ts
cosmastech 0c70af6
Update prompt_secret.ts
cosmastech 771d7a5
fix tests
cosmastech dc54bb9
tests
cosmastech acb6936
3 line test
cosmastech ff5cd96
improve multiline
cosmastech b690b4f
handle multiline prompt that deletes characters
cosmastech 7bcd0fb
add multiline delete test
cosmastech 64a40a9
consistent commenting
cosmastech 488f223
breathing room
cosmastech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,9 @@ const decoder = new TextDecoder(); | |
Deno.test("promptSecret() handles CR", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 80, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"Please provide the password: ", | ||
|
@@ -54,6 +57,9 @@ Deno.test("promptSecret() handles CR", () => { | |
Deno.test("promptSecret() handles LF", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 80, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"Please provide the password: ", | ||
|
@@ -98,6 +104,9 @@ Deno.test("promptSecret() handles LF", () => { | |
Deno.test("promptSecret() handles input", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 80, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"Please provide the password: ", | ||
|
@@ -155,6 +164,9 @@ Deno.test("promptSecret() handles input", () => { | |
Deno.test("promptSecret() handles DEL", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 80, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"Please provide the password: ", | ||
|
@@ -218,6 +230,9 @@ Deno.test("promptSecret() handles DEL", () => { | |
Deno.test("promptSecret() handles BS", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 80, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"Please provide the password: ", | ||
|
@@ -281,6 +296,9 @@ Deno.test("promptSecret() handles BS", () => { | |
Deno.test("promptSecret() handles clear option", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 80, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"Please provide the password: ", | ||
|
@@ -340,6 +358,9 @@ Deno.test("promptSecret() handles clear option", () => { | |
Deno.test("promptSecret() handles mask option", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 80, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"Please provide the password: ", | ||
|
@@ -397,6 +418,9 @@ Deno.test("promptSecret() handles mask option", () => { | |
Deno.test("promptSecret() handles empty mask option", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 80, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"Please provide the password: ", | ||
|
@@ -470,6 +494,9 @@ Deno.test("promptSecret() returns null if Deno.stdin.isTerminal() is false", () | |
Deno.test("promptSecret() handles null readSync", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 80, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"Please provide the password: ", | ||
|
@@ -500,6 +527,9 @@ Deno.test("promptSecret() handles null readSync", () => { | |
Deno.test("promptSecret() handles empty readSync", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 80, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"Please provide the password: ", | ||
|
@@ -526,3 +556,193 @@ Deno.test("promptSecret() handles empty readSync", () => { | |
assertEquals(expectedOutput, actualOutput); | ||
restore(); | ||
}); | ||
|
||
Deno.test("promptSecret() wraps characters wider than console columns", () => { | ||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 5, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"? ", | ||
"\r\x1b[K", | ||
"? *", | ||
"\r\x1b[K", | ||
"? **", | ||
"\r\x1b[K", | ||
"? ***", | ||
"*", | ||
"\r\x1b[K", | ||
"**", | ||
"\r\x1b[K", | ||
"***", | ||
"\r\x1b[K", | ||
"****", | ||
"\r\x1b[K", | ||
"*****", | ||
"*", | ||
"\r\x1b[K", | ||
"**", | ||
"\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", | ||
"u", | ||
"l", | ||
"e", | ||
"s", | ||
"\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("?"); | ||
|
||
assertEquals(password, "deno rules"); | ||
assertEquals(expectedOutput, actualOutput); | ||
restore(); | ||
}); | ||
|
||
Deno.test("promptSecret() returns to previous line when deleting characters", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice test case! |
||
stub(Deno.stdin, "setRaw"); | ||
stub(Deno.stdin, "isTerminal", () => true); | ||
stub(Deno, "consoleSize", () => { | ||
return { columns: 6, rows: 20 }; | ||
}); | ||
|
||
const expectedOutput = [ | ||
"? ", | ||
"\r\u001b[K", | ||
"? *", | ||
"\r\u001b[K", | ||
"? **", | ||
"\r\u001b[K", | ||
"? ***", | ||
"\r\u001b[K", | ||
"? ****", | ||
"*", | ||
"\r\u001b[K", | ||
"**", | ||
"\r\u001b[K", | ||
"***", | ||
"\r\u001b[K", | ||
"****", | ||
"\r\u001b[K", | ||
"*****", | ||
"\r\u001b[K", | ||
"******", | ||
"*", | ||
"\r\u001b[K", | ||
"**", | ||
"\r\u001b[K", | ||
"***", | ||
"\r\u001b[K", | ||
"**", | ||
"\r\u001b[K", | ||
"*", | ||
"\r\u001b[K", | ||
"\r\u001b[1F", | ||
"******", | ||
"\r\u001b[K", | ||
"*****", | ||
"\r\u001b[K", | ||
"****", | ||
"\r\u001b[K", | ||
"***", | ||
"\r\u001b[K", | ||
"**", | ||
"\r\u001b[K", | ||
"*", | ||
"\r\u001b[K", | ||
"\r\u001b[1F", | ||
"? ****", | ||
"\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", | ||
"u", | ||
"l", | ||
"e", | ||
"s", | ||
"!", | ||
"!", | ||
"!", | ||
"\x7f", | ||
"\x7f", | ||
"\x7f", | ||
"\x7f", | ||
"\x7f", | ||
"\x7f", | ||
"\x7f", | ||
"\x7f", | ||
"\x7f", | ||
"\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("?"); | ||
|
||
assertEquals(password, "deno"); | ||
assertEquals(expectedOutput, actualOutput); | ||
restore(); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks nicely commented 👍