62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bufio"
|
||
|
|
"bytes"
|
||
|
|
"io"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestReadLineTimeout_returnsBeforeDeadline(t *testing.T) {
|
||
|
|
r := bufio.NewReader(bytes.NewBufferString("hello\n"))
|
||
|
|
line, timedOut := readLineTimeout(r, 2*time.Second)
|
||
|
|
if timedOut {
|
||
|
|
t.Fatal("expected input, got timeout")
|
||
|
|
}
|
||
|
|
if line != "hello" {
|
||
|
|
t.Fatalf("line=%q", line)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReadLineTimeout_timesOut(t *testing.T) {
|
||
|
|
pr, _ := io.Pipe()
|
||
|
|
r := bufio.NewReader(pr)
|
||
|
|
_, timedOut := readLineTimeout(r, 50*time.Millisecond)
|
||
|
|
if !timedOut {
|
||
|
|
t.Fatal("expected timeout")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestServeConfirmYes(t *testing.T) {
|
||
|
|
if !serveConfirmYes("", false) {
|
||
|
|
t.Fatal("empty should confirm")
|
||
|
|
}
|
||
|
|
if !serveConfirmYes("y", false) {
|
||
|
|
t.Fatal("y should confirm")
|
||
|
|
}
|
||
|
|
if !serveConfirmYes("", true) {
|
||
|
|
t.Fatal("timeout should confirm")
|
||
|
|
}
|
||
|
|
if serveConfirmYes("n", false) {
|
||
|
|
t.Fatal("n should cancel")
|
||
|
|
}
|
||
|
|
if serveConfirmYes("no", false) {
|
||
|
|
t.Fatal("no should cancel")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestMenuAutoServeTimeout_default(t *testing.T) {
|
||
|
|
t.Setenv("MENU_AUTO_SERVE_TIMEOUT_SEC", "")
|
||
|
|
if menuAutoServeTimeout() != defaultMenuAutoServeTimeout {
|
||
|
|
t.Fatalf("got %v", menuAutoServeTimeout())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestMenuAutoServeTimeout_env(t *testing.T) {
|
||
|
|
t.Setenv("MENU_AUTO_SERVE_TIMEOUT_SEC", "5")
|
||
|
|
if menuAutoServeTimeout() != 5*time.Second {
|
||
|
|
t.Fatalf("got %v", menuAutoServeTimeout())
|
||
|
|
}
|
||
|
|
}
|