From 8315dd5e700e68a3936dadf95cbb898a9e0024ba Mon Sep 17 00:00:00 2001 From: Devin Singh Date: Sat, 12 Jun 2021 16:16:43 -0500 Subject: [PATCH] test: add basic lua/sh test --- shell_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 shell_test.go diff --git a/shell_test.go b/shell_test.go new file mode 100644 index 0000000..08d879f --- /dev/null +++ b/shell_test.go @@ -0,0 +1,63 @@ +package main + +import ( + "bytes" + "io" + "os" + "testing" +) + +// https://stackoverflow.com/questions/10473800/in-go-how-do-i-capture-stdout-of-a-function-into-a-string +func TestRunInputSh(t *testing.T) { + LuaInit() + cmd := "echo 'hello'" + old := os.Stdout // keep backup of the real stdout + r, w, _ := os.Pipe() + os.Stdout = w + + RunInput(cmd) + + outC := make(chan string) + // copy the output in a separate goroutine so printing can't block indefinitely + go func() { + var buf bytes.Buffer + io.Copy(&buf, r) + outC <- buf.String() + }() + + // back to normal state + w.Close() + os.Stdout = old // restoring the real stdout + out := <-outC + + if out != "hello\n" { + t.Fatalf("Expected 'hello', found %s", out) + } +} + +func TestRunInputLua(t *testing.T) { + LuaInit() + cmd := "print('hello')" + old := os.Stdout // keep backup of the real stdout + r, w, _ := os.Pipe() + os.Stdout = w + + RunInput(cmd) + + outC := make(chan string) + // copy the output in a separate goroutine so printing can't block indefinitely + go func() { + var buf bytes.Buffer + io.Copy(&buf, r) + outC <- buf.String() + }() + + // back to normal state + w.Close() + os.Stdout = old // restoring the real stdout + out := <-outC + + if out != "hello\n" { + t.Fatalf("Expected 'hello', found %s", out) + } +}