aboutsummaryrefslogtreecommitdiff
path: root/pkg/rtree/shared_test.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2017-07-07 12:14:06 +0200
committerStefan Majewsky <majewsky@gmx.net>2017-07-07 12:14:06 +0200
commit23077b75410d6fab51762236f195d92bd559fc6a (patch)
tree12ec00d832b3958d5194adeab4b73ff37aeeef4b /pkg/rtree/shared_test.go
parent79933615f7988cb731baa0768a45b39008f18b20 (diff)
downloadgofu-23077b75410d6fab51762236f195d92bd559fc6a.tar.gz
many more `rtree get` tests
Diffstat (limited to 'pkg/rtree/shared_test.go')
-rw-r--r--pkg/rtree/shared_test.go96
1 files changed, 78 insertions, 18 deletions
diff --git a/pkg/rtree/shared_test.go b/pkg/rtree/shared_test.go
index 209acb4..cad9378 100644
--- a/pkg/rtree/shared_test.go
+++ b/pkg/rtree/shared_test.go
@@ -20,11 +20,14 @@ package rtree
import (
"bytes"
+ "errors"
"fmt"
+ "io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
+ "strings"
"testing"
"github.com/majewsky/gofu/pkg/cli"
@@ -56,47 +59,49 @@ func TestMain(m *testing.M) {
//Test describes a call to Main(), the environment that's given to it, and the
//assertions that are checked after the call returns.
type Test struct {
- Args []string
- Input string
- Index Index
- ExpectFailure bool
- ExpectOutput string
- ExpectError string
- ExpectIndex *Index //if nil, .Index will be used instead
+ Args []string
+ Input string
+ Index Index
+ ExpectFailure bool
+ ExpectOutput string
+ ExpectError string
+ ExpectIndex *Index //if nil, .Index will be used instead
+ ExpectExecution []RecordedCommand
}
-func (test Test) Run(t *testing.T, testName string) {
+func (test Test) Run(t *testing.T) {
//write index file, if any
- IndexPath = filepath.Join(indexTmpDir, testName+".yaml")
+ IndexPath = filepath.Join(indexTmpDir, t.Name()+".yaml")
if test.Index.Repos != nil {
err := test.Index.Write()
if err != nil {
- t.Fatalf("%s: cannot write index to %s: %s", testName, IndexPath, err.Error())
+ t.Fatalf("%s: cannot write index to %s: %s", t.Name(), IndexPath, err.Error())
}
}
//setup cli.Interface for test
var stdout bytes.Buffer
var stderr bytes.Buffer
- cli.SetupInterface(bytes.NewReader([]byte(test.Input)), &stdout, &stderr, nil)
+ cs := CommandSimulator{Cmd: test.ExpectExecution}
+ cli.SetupInterface(bytes.NewReader([]byte(test.Input)), &stdout, &stderr, cs.Next)
//check exit code
exitCode := Exec(test.Args)
switch {
case exitCode == 0 && test.ExpectFailure:
- t.Errorf("%s: expected failure, but returned success", testName)
+ t.Errorf("%s: expected failure, but returned success", t.Name())
case exitCode != 0 && !test.ExpectFailure:
- t.Errorf("%s: expected success, but returned failure", testName)
+ t.Errorf("%s: expected success, but returned failure", t.Name())
}
//check output
output := string(stdout.Bytes())
if output != test.ExpectOutput {
- t.Errorf("%s: expected stdout %#v, but got %#v", testName, test.ExpectOutput, output)
+ t.Errorf("%s: expected stdout %#v, but got %#v", t.Name(), test.ExpectOutput, output)
}
output = string(stderr.Bytes())
if output != test.ExpectError {
- t.Errorf("%s: expected stderr %#v, but got %#v", testName, test.ExpectError, output)
+ t.Errorf("%s: expected stderr %#v, but got %#v", t.Name(), test.ExpectError, output)
}
//check index
@@ -110,17 +115,72 @@ func (test Test) Run(t *testing.T, testName string) {
}
actualIdxStr, err := ioutil.ReadFile(IndexPath)
if err != nil {
- t.Fatalf("%s: could not read index from %s: %s", testName, IndexPath, err.Error())
+ t.Fatalf("%s: could not read index from %s: %s", t.Name(), IndexPath, err.Error())
}
if string(expectedIdxStr) != string(actualIdxStr) {
- t.Errorf("%s: index does not match expectation after test; diff follows", testName)
+ t.Errorf("%s: index does not match expectation after test; diff follows", t.Name())
cmd := exec.Command("diff", "-u", "-", IndexPath)
cmd.Stdin = bytes.NewReader([]byte(expectedIdxStr))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
- err := cmd.Wait()
+ err := cmd.Run()
if err != nil {
t.Fatal(err.Error())
}
}
}
+
+type RecordedCommand struct {
+ Cmd cli.Command
+ Stdout string
+ Stderr string
+ Fails bool
+}
+
+//CommandSimulator implements the cli.CommandRunner interface (via its Next
+//method). When a cli.Command is given to Next(), it is matched with the next
+//command in the .Cmd list, and the result from that RecordedCommand is
+//returned. If the given Command is different from the one expected (or if the
+//.Cmd list has been exhausted), an error is returned.
+type CommandSimulator struct {
+ Cmd []RecordedCommand
+ idx int
+}
+
+func (s *CommandSimulator) Next(c cli.Command, stdin io.Reader, stdout, stderr io.Writer) error {
+ //take next RecordedCommand from list
+ if s.idx >= len(s.Cmd) {
+ return errors.New("got command to execute, but recorded commands have been exhausted")
+ }
+ sc := s.Cmd[s.idx]
+ s.idx++
+
+ //check if the given Command matches the expectation
+ if !areStringListsEqual(sc.Cmd.Program, c.Program) {
+ return fmt.Errorf("expected command %#v, but got %#v",
+ strings.Join(sc.Cmd.Program, " "), strings.Join(c.Program, " "),
+ )
+ }
+ if sc.Cmd.WorkDir != c.WorkDir {
+ return fmt.Errorf("expected command workdir %s, but got %s", sc.Cmd.WorkDir, c.WorkDir)
+ }
+
+ stdout.Write([]byte(sc.Stdout))
+ stderr.Write([]byte(sc.Stderr))
+ if sc.Fails {
+ return fmt.Errorf("command %#v has failed", strings.Join(c.Program, " "))
+ }
+ return nil
+}
+
+func areStringListsEqual(a []string, b []string) bool {
+ if len(a) != len(b) {
+ return false
+ }
+ for idx := range a {
+ if a[idx] != b[idx] {
+ return false
+ }
+ }
+ return true
+}