1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package cmdline
- import (
- "fmt"
- "os"
- "sync"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/tal-tech/go-zero/core/iox"
- "github.com/tal-tech/go-zero/core/lang"
- )
- func TestEnterToContinue(t *testing.T) {
- restore, err := iox.RedirectInOut()
- assert.Nil(t, err)
- defer restore()
- var wg sync.WaitGroup
- wg.Add(2)
- go func() {
- defer wg.Done()
- fmt.Println()
- }()
- go func() {
- defer wg.Done()
- EnterToContinue()
- }()
- wait := make(chan lang.PlaceholderType)
- go func() {
- wg.Wait()
- close(wait)
- }()
- select {
- case <-time.After(time.Second):
- t.Error("timeout")
- case <-wait:
- }
- }
- func TestReadLine(t *testing.T) {
- r, w, err := os.Pipe()
- assert.Nil(t, err)
- ow := os.Stdout
- os.Stdout = w
- or := os.Stdin
- os.Stdin = r
- defer func() {
- os.Stdin = or
- os.Stdout = ow
- }()
- const message = "hello"
- var wg sync.WaitGroup
- wg.Add(2)
- go func() {
- defer wg.Done()
- fmt.Println(message)
- }()
- go func() {
- defer wg.Done()
- input := ReadLine("")
- assert.Equal(t, message, input)
- }()
- wait := make(chan lang.PlaceholderType)
- go func() {
- wg.Wait()
- close(wait)
- }()
- select {
- case <-time.After(time.Second):
- t.Error("timeout")
- case <-wait:
- }
- }
|