115 lines
1.7 KiB
Go
115 lines
1.7 KiB
Go
package core
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestExecute(t *testing.T) {
|
|
assertLen := func(items []Item, length int) {
|
|
if len(items) != length {
|
|
t.Fatalf("Expected %d items, got %d", length, len(items))
|
|
}
|
|
}
|
|
assertNil := func(err error) {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
assertNotNil := func(err error) {
|
|
if err == nil {
|
|
t.Fatal("expected err")
|
|
}
|
|
}
|
|
|
|
res, err := Execute(
|
|
[]string{"true"},
|
|
nil,
|
|
"",
|
|
time.Minute,
|
|
)
|
|
assertNil(err)
|
|
assertLen(res, 0)
|
|
|
|
// Exit with error code
|
|
res, err = Execute(
|
|
[]string{"false"},
|
|
nil,
|
|
"",
|
|
time.Minute,
|
|
)
|
|
assertNotNil(err)
|
|
assertLen(res, 0)
|
|
|
|
res, err = Execute(
|
|
[]string{"sh", "-c", "exit 22"},
|
|
nil,
|
|
"",
|
|
time.Minute,
|
|
)
|
|
assertNotNil(err)
|
|
assertLen(res, 0)
|
|
|
|
// Timeout
|
|
res, err = Execute(
|
|
[]string{"sleep", "10"},
|
|
nil,
|
|
"",
|
|
time.Millisecond,
|
|
)
|
|
assertNotNil(err)
|
|
assertLen(res, 0)
|
|
|
|
// Returning items
|
|
res, err = Execute(
|
|
[]string{"jq", "-cn", `{id: "foo"}`},
|
|
nil,
|
|
"",
|
|
time.Minute,
|
|
)
|
|
assertNil(err)
|
|
assertLen(res, 1)
|
|
if res[0].Id != "foo" {
|
|
t.Fatal("jq -cn test failed")
|
|
}
|
|
|
|
// Read from stdin
|
|
res, err = Execute(
|
|
[]string{"jq", "-cR", `{id: .}`},
|
|
nil,
|
|
"bar",
|
|
time.Minute,
|
|
)
|
|
assertNil(err)
|
|
assertLen(res, 1)
|
|
if res[0].Id != "bar" {
|
|
t.Fatal("jq -cR test failed")
|
|
}
|
|
|
|
// Set env
|
|
res, err = Execute(
|
|
[]string{"jq", "-cn", `{id: env.HELLO}`},
|
|
[]string{"HELLO=baz"},
|
|
"",
|
|
time.Minute,
|
|
)
|
|
assertNil(err)
|
|
assertLen(res, 1)
|
|
if res[0].Id != "baz" {
|
|
t.Fatal("jq -cn env test failed")
|
|
}
|
|
|
|
// With logging on stderr
|
|
res, err = Execute(
|
|
[]string{"sh", "-c", `echo 1>&2 Hello; jq -cn '{id: "box"}'; echo 1>&2 World`},
|
|
nil,
|
|
"",
|
|
time.Minute,
|
|
)
|
|
assertNil(err)
|
|
assertLen(res, 1)
|
|
if res[0].Id != "box" {
|
|
t.Fatal("stderr test failed")
|
|
}
|
|
}
|