@@ -6,14 +6,14 @@ import (
66 "io"
77 "os"
88 "os/exec"
9+ "runtime"
910 "testing"
1011
1112 "github.com/stretchr/testify/assert"
1213 "github.com/stretchr/testify/require"
1314 "golang.org/x/xerrors"
1415
1516 "github.com/coder/coder/v2/cli/cliui"
16- "github.com/coder/coder/v2/pty"
1717 "github.com/coder/coder/v2/pty/ptytest"
1818 "github.com/coder/coder/v2/testutil"
1919 "github.com/coder/serpent"
@@ -181,6 +181,48 @@ func TestPrompt(t *testing.T) {
181181 resp := testutil .TryReceive (ctx , t , doneChan )
182182 require .Equal (t , "valid" , resp )
183183 })
184+
185+ t .Run ("MaskedSecret" , func (t * testing.T ) {
186+ t .Parallel ()
187+ ctx := testutil .Context (t , testutil .WaitShort )
188+ ptty := ptytest .New (t )
189+ doneChan := make (chan string )
190+ go func () {
191+ resp , err := newPrompt (ctx , ptty , cliui.PromptOptions {
192+ Text : "Password:" ,
193+ Secret : true ,
194+ }, nil )
195+ assert .NoError (t , err )
196+ doneChan <- resp
197+ }()
198+ ptty .ExpectMatch ("Password: " )
199+
200+ ptty .WriteLine ("test" )
201+
202+ resp := testutil .TryReceive (ctx , t , doneChan )
203+ require .Equal (t , "test" , resp )
204+ })
205+
206+ t .Run ("UTF8Password" , func (t * testing.T ) {
207+ t .Parallel ()
208+ ctx := testutil .Context (t , testutil .WaitShort )
209+ ptty := ptytest .New (t )
210+ doneChan := make (chan string )
211+ go func () {
212+ resp , err := newPrompt (ctx , ptty , cliui.PromptOptions {
213+ Text : "Password:" ,
214+ Secret : true ,
215+ }, nil )
216+ assert .NoError (t , err )
217+ doneChan <- resp
218+ }()
219+ ptty .ExpectMatch ("Password: " )
220+
221+ ptty .WriteLine ("和製漢字" )
222+
223+ resp := testutil .TryReceive (ctx , t , doneChan )
224+ require .Equal (t , "和製漢字" , resp )
225+ })
184226}
185227
186228func newPrompt (ctx context.Context , ptty * ptytest.PTY , opts cliui.PromptOptions , invOpt func (inv * serpent.Invocation )) (string , error ) {
@@ -209,13 +251,12 @@ func TestPasswordTerminalState(t *testing.T) {
209251 passwordHelper ()
210252 return
211253 }
254+ if runtime .GOOS == "windows" {
255+ t .Skip ("Skipping on windows. PTY doesn't read ptty.Write correctly." )
256+ }
212257 t .Parallel ()
213258
214259 ptty := ptytest .New (t )
215- ptyWithFlags , ok := ptty .PTY .(pty.WithFlags )
216- if ! ok {
217- t .Skip ("unable to check PTY local echo on this platform" )
218- }
219260
220261 cmd := exec .Command (os .Args [0 ], "-test.run=TestPasswordTerminalState" ) //nolint:gosec
221262 cmd .Env = append (os .Environ (), "TEST_SUBPROCESS=1" )
@@ -229,21 +270,16 @@ func TestPasswordTerminalState(t *testing.T) {
229270 defer process .Kill ()
230271
231272 ptty .ExpectMatch ("Password: " )
232-
233- require . Eventually ( t , func () bool {
234- echo , err := ptyWithFlags . EchoEnabled ( )
235- return err == nil && ! echo
236- }, testutil . WaitShort , testutil . IntervalMedium , "echo is on while reading password " )
273+ ptty . Write ( 't' )
274+ ptty . Write ( 'e' )
275+ ptty . Write ( 's' )
276+ ptty . Write ( 't' )
277+ ptty . ExpectMatch ( "**** " )
237278
238279 err = process .Signal (os .Interrupt )
239280 require .NoError (t , err )
240281 _ , err = process .Wait ()
241282 require .NoError (t , err )
242-
243- require .Eventually (t , func () bool {
244- echo , err := ptyWithFlags .EchoEnabled ()
245- return err == nil && echo
246- }, testutil .WaitShort , testutil .IntervalMedium , "echo is off after reading password" )
247283}
248284
249285// nolint:unused
0 commit comments