go-zero/core/cmdline/input_test.go

81 lines
1.2 KiB
Go
Raw Normal View History

2020-10-02 22:37:15 +08:00
package cmdline
import (
"fmt"
"os"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
2020-10-07 22:54:51 +08:00
"github.com/tal-tech/go-zero/core/iox"
2020-10-02 22:37:15 +08:00
"github.com/tal-tech/go-zero/core/lang"
)
func TestEnterToContinue(t *testing.T) {
2020-10-07 22:54:51 +08:00
restore, err := iox.RedirectInOut()
2020-10-02 22:37:15 +08:00
assert.Nil(t, err)
2020-10-07 22:54:51 +08:00
defer restore()
2020-10-02 22:37:15 +08:00
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)
2020-10-02 22:41:25 +08:00
ow := os.Stdout
os.Stdout = w
or := os.Stdin
os.Stdin = r
defer func() {
os.Stdin = or
os.Stdout = ow
}()
2020-10-02 22:37:15 +08:00
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:
}
}