github.com/go-kit/log@v0.2.1/term/terminal_windows_test.go (about) 1 package term 2 3 import ( 4 "fmt" 5 "syscall" 6 "testing" 7 ) 8 9 type myWriter struct { 10 fd uintptr 11 } 12 13 func (w *myWriter) Write(p []byte) (int, error) { 14 return 0, fmt.Errorf("not implemented") 15 } 16 17 func (w *myWriter) Fd() uintptr { 18 return w.fd 19 } 20 21 var procGetStdHandle = kernel32.NewProc("GetStdHandle") //lint:ignore U1000 unused 22 23 const stdOutputHandle = ^uintptr(0) - 11 + 1 //lint:ignore U1000 unused 24 25 func getConsoleHandle() syscall.Handle { 26 ptr, err := syscall.UTF16PtrFromString("CONOUT$") 27 if err != nil { 28 panic(err) 29 } 30 31 handle, err := syscall.CreateFile(ptr, syscall.GENERIC_READ|syscall.GENERIC_WRITE, syscall.FILE_SHARE_READ, nil, syscall.OPEN_EXISTING, 0, 0) 32 if err != nil { 33 panic(err) 34 } 35 36 return handle 37 } 38 39 func TestIsTerminal(t *testing.T) { 40 // This is necessary because depending on whether `go test` is called with 41 // the `-v` option, stdout will or will not be bound, changing the behavior 42 // of the test. So we refer to it directly to avoid flakyness. 43 handle := getConsoleHandle() 44 45 writer := &myWriter{ 46 fd: uintptr(handle), 47 } 48 49 if !IsTerminal(writer) { 50 t.Errorf("output is supposed to be a terminal") 51 } 52 } 53 54 func TestIsConsole(t *testing.T) { 55 // This is necessary because depending on whether `go test` is called with 56 // the `-v` option, stdout will or will not be bound, changing the behavior 57 // of the test. So we refer to it directly to avoid flakyness. 58 handle := getConsoleHandle() 59 60 writer := &myWriter{ 61 fd: uintptr(handle), 62 } 63 64 if !IsConsole(writer) { 65 t.Errorf("output is supposed to be a console") 66 } 67 }