Skip to content

Commit

Permalink
fixed mocking in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Jun 5, 2022
1 parent 2212fd4 commit 250ff20
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
6 changes: 3 additions & 3 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"atomicgo.dev/keyboard/keys"
)

func ExampleSimple() {
func ExampleListen_simple() {
Listen(func(key keys.Key) (stop bool, err error) {
if key.Code == keys.CtrlC {
return true, nil // Stop listener by returning true on Ctrl+C
Expand All @@ -18,7 +18,7 @@ func ExampleSimple() {
})
}

func ExampleAdvanced() {
func ExampleListen_advanced() {
// Stop keyboard listener on Escape key press or CTRL+C.
// Exit application on "q" key press.
// Print every rune key press.
Expand All @@ -41,7 +41,7 @@ func ExampleAdvanced() {
})
}

func ExampleMocking() {
func ExampleSimulateKeyPress() {
go func() {
SimulateKeyPress("Hello") // Simulate key press for every letter in string
SimulateKeyPress(keys.Enter) // Simulate key press for Enter
Expand Down
6 changes: 3 additions & 3 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func getKeyPress(input io.Reader) (keys.Key, error) {
// Read
numBytes, err := input.Read(buf[:])
if err != nil {
return keys.Key{}, fmt.Errorf("could not read input: %w", err)
return keys.Key{}, fmt.Errorf("could not read stdin: %w", err)
}

// Check if it's a sequence
Expand All @@ -194,7 +194,7 @@ func getKeyPress(input io.Reader) (keys.Key, error) {
var runes []rune
b := buf[:numBytes]

// Translate input into runes.
// Translate stdin into runes.
for i, w := 0, 0; i < len(b); i += w { //nolint:wastedassign
r, width := utf8.DecodeRune(b[i:])
if r == utf8.RuneError {
Expand All @@ -205,7 +205,7 @@ func getKeyPress(input io.Reader) (keys.Key, error) {
}

if len(runes) == 0 {
return keys.Key{}, fmt.Errorf("received 0 runes from input")
return keys.Key{}, fmt.Errorf("received 0 runes from stdin")
} else if len(runes) > 1 {
return keys.Key{Code: keys.RuneKey, Runes: runes}, nil
}
Expand Down
10 changes: 8 additions & 2 deletions keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ import (

var windowsStdin *os.File
var con console.Console
var input = os.Stdin
var stdin = os.Stdin
var inputTTY *os.File
var mockChannel = make(chan keys.Key)

var mocking = false

func startListener() error {
err := initInput()
if err != nil {
return err
}

if mocking {
return nil
}

if con != nil {
err := con.SetRaw()
if err != nil {
Expand Down Expand Up @@ -106,7 +112,7 @@ func Listen(onKeyPress func(key keys.Key) (stop bool, err error)) error {
return nil
}

// SimulateKeyPress simulate a key press. It can be used to mock user input and test your application.
// SimulateKeyPress simulate a key press. It can be used to mock user stdin and test your application.
//
// Example:
// go func() {
Expand Down
31 changes: 31 additions & 0 deletions keyboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package keyboard_test

import (
"sync"
"testing"

"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
)

var wg sync.WaitGroup

func TestMocking(t *testing.T) {
wg.Add(1)

keyboard.Listen(func(key keys.Key) (stop bool, err error) {
if key.Code != keys.Down {
t.Errorf("down key should be pressed, but %s is pressed", key)
}

wg.Done()

return true, nil
})

go func() {
keyboard.SimulateKeyPress(keys.Down)
}()

wg.Wait()
}
2 changes: 1 addition & 1 deletion tty_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func restoreInput() error {
}

func initInput() error {
c, err := console.ConsoleFromFile(input)
c, err := console.ConsoleFromFile(stdin)
if err != nil {
return err
}
Expand Down
14 changes: 12 additions & 2 deletions tty_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package keyboard
import (
"fmt"
"os"
"syscall"

"github.com/containerd/console"
)
Expand All @@ -20,7 +21,16 @@ func restoreInput() error {

func initInput() error {
windowsStdin = os.Stdin
os.Stdin = input

os.Stdin = stdin

var mode uint32
err := syscall.GetConsoleMode(syscall.Stdin, &mode)

if err != nil {
mocking = true
return nil
}

con = console.Current()

Expand All @@ -30,7 +40,7 @@ func initInput() error {
func openInputTTY() (*os.File, error) {
f, err := os.OpenFile("CONIN$", os.O_RDWR, 0644)
if err != nil {
return nil, fmt.Errorf("failed to open input TTY: %w", err)
return nil, fmt.Errorf("failed to open stdin TTY: %w", err)
}

return f, nil
Expand Down

0 comments on commit 250ff20

Please sign in to comment.