github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/cli/stdin_test.go (about) 1 package cli 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestReadEmptyStdin(t *testing.T) { 11 seenStdin = false // Have to reset this for each test. 12 c := ReadStdin() 13 _, ok := <-c 14 assert.False(t, ok, "Standard input is not being written to, should be empty.") 15 } 16 17 func TestReadAllEmptyStdin(t *testing.T) { 18 seenStdin = false // Have to reset this for each test. 19 stdin := ReadAllStdin() 20 assert.Equal(t, 0, len(stdin), "Standard input is not being written to, should be empty.") 21 } 22 23 func TestReadStdin(t *testing.T) { 24 seenStdin = false 25 f, err := os.Open("src/cli/test_data/stdin.txt") 26 assert.NoError(t, err) 27 defer f.Close() 28 29 // Temporarily reassign this. 30 originalStdin := os.Stdin 31 os.Stdin = f 32 defer func() { os.Stdin = originalStdin }() 33 c := ReadStdin() 34 35 // Test that these get broken into words properly. 36 assert.Equal(t, "hello", <-c) 37 assert.Equal(t, "world", <-c) 38 assert.Equal(t, "hello", <-c) 39 assert.Equal(t, "world", <-c) 40 assert.Equal(t, "helloworld", <-c) 41 42 _, ok := <-c 43 assert.False(t, ok, "Should be nothing more in the channel") 44 } 45 46 func TestReadAllStdin(t *testing.T) { 47 seenStdin = false 48 f, err := os.Open("src/cli/test_data/stdin.txt") 49 assert.NoError(t, err) 50 defer f.Close() 51 52 // Temporarily reassign this. 53 originalStdin := os.Stdin 54 os.Stdin = f 55 defer func() { os.Stdin = originalStdin }() 56 stdin := ReadAllStdin() 57 assert.Equal(t, stdin, []string{"hello", "world", "hello", "world", "helloworld"}) 58 }