Increase test coverage (#19)
* Increase test coverage for smug.go * Add context struct * Test commander_test.go * Test commander_test.gomaster
parent
2a6aec6ce7
commit
fb9a652eef
@ -1 +1,2 @@
|
|||||||
|
coverage.out
|
||||||
dist/
|
dist/
|
||||||
|
@ -1,2 +1,9 @@
|
|||||||
build:
|
build:
|
||||||
go build -o smug *.go
|
go build -o smug *.go
|
||||||
|
|
||||||
|
test:
|
||||||
|
go test .
|
||||||
|
|
||||||
|
coverage:
|
||||||
|
go test -coverprofile=coverage.out
|
||||||
|
go tool cover -html=coverage.out
|
||||||
|
@ -0,0 +1,88 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
switch os.Getenv("TEST_MAIN") {
|
||||||
|
case "":
|
||||||
|
os.Exit(m.Run())
|
||||||
|
case "echo":
|
||||||
|
fmt.Println(strings.Join(os.Args[1:], " "))
|
||||||
|
case "exit":
|
||||||
|
os.Exit(42)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExec(t *testing.T) {
|
||||||
|
logger := log.New(bytes.NewBuffer([]byte{}), "", 0)
|
||||||
|
commander := DefaultCommander{logger}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "42")
|
||||||
|
cmd.Env = append(os.Environ(), "TEST_MAIN=echo")
|
||||||
|
|
||||||
|
output, err := commander.Exec(cmd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if output != "42" {
|
||||||
|
t.Errorf("expected 42, got %q", output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExecError(t *testing.T) {
|
||||||
|
logger := log.New(bytes.NewBuffer([]byte{}), "", 0)
|
||||||
|
commander := DefaultCommander{logger}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "42")
|
||||||
|
cmd.Env = append(os.Environ(), "TEST_MAIN=exit")
|
||||||
|
|
||||||
|
_, err := commander.Exec(cmd)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected error")
|
||||||
|
}
|
||||||
|
|
||||||
|
got := cmd.ProcessState.ExitCode()
|
||||||
|
if got != 42 {
|
||||||
|
t.Errorf("expected %d, got %d", 42, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExecSilently(t *testing.T) {
|
||||||
|
logger := log.New(bytes.NewBuffer([]byte{}), "", 0)
|
||||||
|
commander := DefaultCommander{logger}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "42")
|
||||||
|
cmd.Env = append(os.Environ(), "TEST_MAIN=echo")
|
||||||
|
|
||||||
|
err := commander.ExecSilently(cmd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExecSilentlyError(t *testing.T) {
|
||||||
|
logger := log.New(bytes.NewBuffer([]byte{}), "", 0)
|
||||||
|
commander := DefaultCommander{logger}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "42")
|
||||||
|
cmd.Env = append(os.Environ(), "TEST_MAIN=exit")
|
||||||
|
|
||||||
|
err := commander.ExecSilently(cmd)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected error")
|
||||||
|
}
|
||||||
|
|
||||||
|
got := cmd.ProcessState.ExitCode()
|
||||||
|
if got != 42 {
|
||||||
|
t.Errorf("expected %d, got %d", 42, got)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
type Context struct {
|
||||||
|
InsideTmuxSession bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateContext() *Context {
|
||||||
|
insideTmuxSession := os.Getenv("TERM") == "screen"
|
||||||
|
return &Context{insideTmuxSession}
|
||||||
|
}
|
Loading…
Reference in New Issue