aboutsummaryrefslogtreecommitdiff
path: root/pkg/rtree/shared_test.go
blob: cad9378e9be2cb44e51d13d76a081ea8fa01ffad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*******************************************************************************
*
* Copyright 2017 Stefan Majewsky <majewsky@gmx.net>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

package rtree

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"

	"github.com/majewsky/gofu/pkg/cli"
	yaml "gopkg.in/yaml.v2"
)

//Path to a directory where tests can put their index files.
var indexTmpDir = filepath.Join(os.TempDir(), fmt.Sprintf("rtree-test-%d", os.Getpid()))

func TestMain(m *testing.M) {
	//make sure that test does not accidentally access user's actual rtree or index
	os.Setenv("HOME", "")
	os.Setenv("GOPATH", "")
	//setup test configuration
	RootPath = "/unittest/gopath/src"
	RemoteAliases = []*RemoteAlias{
		{Alias: "gh:", Replacement: "https://github.com/"},
		{Alias: "my/", Replacement: "git@git.example.com:"},
	}

	exitCode := m.Run()

	//shared teardown
	os.RemoveAll(indexTmpDir)

	os.Exit(exitCode)
}

//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
	ExpectExecution []RecordedCommand
}

func (test Test) Run(t *testing.T) {
	//write index file, if any
	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", t.Name(), IndexPath, err.Error())
		}
	}

	//setup cli.Interface for test
	var stdout bytes.Buffer
	var stderr bytes.Buffer
	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", t.Name())
	case exitCode != 0 && !test.ExpectFailure:
		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", t.Name(), test.ExpectOutput, output)
	}
	output = string(stderr.Bytes())
	if output != test.ExpectError {
		t.Errorf("%s: expected stderr %#v, but got %#v", t.Name(), test.ExpectError, output)
	}

	//check index
	idx := &test.Index
	if test.ExpectIndex != nil {
		idx = test.ExpectIndex
	}
	expectedIdxStr, err := yaml.Marshal(idx)
	if err != nil {
		t.Fatal(err.Error())
	}
	actualIdxStr, err := ioutil.ReadFile(IndexPath)
	if err != nil {
		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", t.Name())
		cmd := exec.Command("diff", "-u", "-", IndexPath)
		cmd.Stdin = bytes.NewReader([]byte(expectedIdxStr))
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		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
}