@@ -6,14 +6,14 @@ import (
6
6
"io"
7
7
"os"
8
8
"os/exec"
9
+ "runtime"
9
10
"testing"
10
11
11
12
"github.com/stretchr/testify/assert"
12
13
"github.com/stretchr/testify/require"
13
14
"golang.org/x/xerrors"
14
15
15
16
"github.com/coder/coder/v2/cli/cliui"
16
- "github.com/coder/coder/v2/pty"
17
17
"github.com/coder/coder/v2/pty/ptytest"
18
18
"github.com/coder/coder/v2/testutil"
19
19
"github.com/coder/serpent"
@@ -181,6 +181,48 @@ func TestPrompt(t *testing.T) {
181
181
resp := testutil .TryReceive (ctx , t , doneChan )
182
182
require .Equal (t , "valid" , resp )
183
183
})
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
+ })
184
226
}
185
227
186
228
func 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) {
209
251
passwordHelper ()
210
252
return
211
253
}
254
+ if runtime .GOOS == "windows" {
255
+ t .Skip ("Skipping on windows. PTY doesn't read ptty.Write correctly." )
256
+ }
212
257
t .Parallel ()
213
258
214
259
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
- }
219
260
220
261
cmd := exec .Command (os .Args [0 ], "-test.run=TestPasswordTerminalState" ) //nolint:gosec
221
262
cmd .Env = append (os .Environ (), "TEST_SUBPROCESS=1" )
@@ -229,21 +270,16 @@ func TestPasswordTerminalState(t *testing.T) {
229
270
defer process .Kill ()
230
271
231
272
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 ( "**** " )
237
278
238
279
err = process .Signal (os .Interrupt )
239
280
require .NoError (t , err )
240
281
_ , err = process .Wait ()
241
282
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" )
247
283
}
248
284
249
285
// nolint:unused
0 commit comments