tests flags and binary builder

Этот коммит содержится в:
gedi 2017-04-27 16:40:31 +03:00
родитель 169d617b26
коммит 90b8d3bfe7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 56604CDCCC201556
3 изменённых файлов: 109 добавлений и 1 удалений

Просмотреть файл

@ -2,6 +2,7 @@ package godog
import ( import (
"go/build" "go/build"
"os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
@ -35,3 +36,33 @@ func TestVendorPaths(t *testing.T) {
gopaths = filepath.SplitList(build.Default.GOPATH) gopaths = filepath.SplitList(build.Default.GOPATH)
} }
func TestBuildTestRunner(t *testing.T) {
bin := filepath.Join(os.TempDir(), "godog.test")
if err := Build(bin); err != nil {
t.Fatalf("failed to build godog test binary: %v", err)
}
os.Remove(bin)
}
func TestBuildTestRunnerWithoutGoFiles(t *testing.T) {
bin := filepath.Join(os.TempDir(), "godog.test")
pwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
wd := filepath.Join(pwd, "features")
if err := os.Chdir(wd); err != nil {
t.Fatalf("failed to change working directory: %v", err)
}
defer func() {
os.Chdir(pwd) // get back to current dir
}()
if err := Build(bin); err != nil {
t.Fatalf("failed to build godog test binary: %v", err)
}
os.Remove(bin)
}

Просмотреть файл

@ -132,7 +132,7 @@ func usage(set *flag.FlagSet, w io.Writer) func() {
// --- GENERAL --- // --- GENERAL ---
fmt.Fprintln(w, colors.Yellow("Usage:")) fmt.Fprintln(w, colors.Yellow("Usage:"))
fmt.Printf(s(2) + "godog [options] [<features>]\n\n") fmt.Fprintf(w, s(2)+"godog [options] [<features>]\n\n")
// description // description
fmt.Fprintln(w, "Builds a test package and runs given feature files.") fmt.Fprintln(w, "Builds a test package and runs given feature files.")
fmt.Fprintf(w, "Command should be run from the directory of tested package and contain buildable go source.\n\n") fmt.Fprintf(w, "Command should be run from the directory of tested package and contain buildable go source.\n\n")

77
flags_test.go Обычный файл
Просмотреть файл

@ -0,0 +1,77 @@
package godog
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/DATA-DOG/godog/colors"
)
func TestFlagsShouldRandomizeAndGenerateSeed(t *testing.T) {
var opt Options
flags := FlagSet(&opt)
if err := flags.Parse([]string{"--random"}); err != nil {
t.Fatalf("unable to parse flags: %v", err)
}
if opt.Randomize <= 0 {
t.Fatal("should have generated random seed")
}
}
func TestFlagsShouldRandomizeByGivenSeed(t *testing.T) {
var opt Options
flags := FlagSet(&opt)
if err := flags.Parse([]string{"--random=123"}); err != nil {
t.Fatalf("unable to parse flags: %v", err)
}
if opt.Randomize != 123 {
t.Fatalf("expected random seed to be: 123, but it was: %d", opt.Randomize)
}
}
func TestFlagsShouldParseFormat(t *testing.T) {
cases := map[string][]string{
"pretty": {},
"progress": {"-f", "progress"},
"junit": {"-f=junit"},
"custom": {"--format", "custom"},
"cust": {"--format=cust"},
}
for format, args := range cases {
var opt Options
flags := FlagSet(&opt)
if err := flags.Parse(args); err != nil {
t.Fatalf("unable to parse flags: %v", err)
}
if opt.Format != format {
t.Fatalf("expected format: %s, but it was: %s", format, opt.Format)
}
}
}
func TestFlagsUsageShouldIncludeFormatDescriptons(t *testing.T) {
var buf bytes.Buffer
output := colors.Uncolored(&buf)
// register some custom formatter
Format("custom", "custom format description", junitFunc)
var opt Options
flags := FlagSet(&opt)
usage(flags, output)()
out := buf.String()
for name, desc := range AvailableFormatters() {
match := fmt.Sprintf("%s: %s\n", name, desc)
if idx := strings.Index(out, match); idx == -1 {
t.Fatalf("could not locate format: %s description in flag usage", name)
}
}
}