-
Notifications
You must be signed in to change notification settings - Fork 40
/
main_test.go
102 lines (87 loc) · 2.76 KB
/
main_test.go
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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"testing"
)
const (
testVersion string = "test"
testExecutable string = "tf-summarize-test"
)
func TestMain(m *testing.M) {
// compile a 'tf-summarize' for use in running tests
exe := exec.Command("go", "build", "-ldflags", fmt.Sprintf("-X main.version=%s", testVersion), "-o", testExecutable)
err := exe.Run()
if err != nil {
os.Exit(1)
}
m.Run()
// delete the compiled tf-summarize
err = os.Remove(testExecutable)
if err != nil {
log.Fatal(err)
}
}
func TestVersionArg(t *testing.T) {
args := []string{
"-v",
}
for _, arg := range args {
t.Run(fmt.Sprintf("when tf-summarize is passed '%s'", arg), func(t *testing.T) {
output, err := exec.Command(fmt.Sprintf("./%s", testExecutable), arg).CombinedOutput()
if err != nil {
t.Errorf("expected '%s' not to cause error; got '%v'", arg, err)
}
if !strings.Contains(string(output), testVersion) {
t.Errorf("expected '%s' to output version '%s'; got '%s'", arg, testVersion, output)
}
})
}
}
func TestTFSummarize(t *testing.T) {
tests := []struct {
command string
expectedError error
expectedOutput string
}{{
command: fmt.Sprintf("./%s -md example/tfplan.json", testExecutable),
expectedOutput: "basic.txt",
}, {
command: fmt.Sprintf("cat example/tfplan.json | ./%s -md", testExecutable),
expectedOutput: "basic.txt",
}, {
command: fmt.Sprintf("cat example/tfplan.json | ./%s -html", testExecutable),
expectedOutput: "basic.html",
}, {
command: fmt.Sprintf("cat example/tfplan.json | ./%s -md -html", testExecutable),
expectedError: fmt.Errorf("exit status 1"),
expectedOutput: "multiple_format_flags_error.txt",
}}
for _, test := range tests {
t.Run(fmt.Sprintf("when tf-summarize is passed '%q'", test.command), func(t *testing.T) {
output, cmdErr := exec.Command("/bin/sh", "-c", test.command).CombinedOutput()
if cmdErr != nil && test.expectedError == nil {
t.Errorf("expected '%s' not to error; got '%v'", test.command, cmdErr)
}
b, err := os.ReadFile(fmt.Sprintf("testdata/%s", test.expectedOutput))
if err != nil {
t.Errorf("error reading file '%s': '%v'", test.expectedOutput, err)
}
expected := string(b)
if test.expectedError != nil && cmdErr == nil {
t.Errorf("expected error '%s'; got '%v'", test.expectedError.Error(), cmdErr)
}
if test.expectedError != nil && cmdErr != nil && test.expectedError.Error() != cmdErr.Error() {
t.Errorf("expected error '%s'; got '%v'", test.expectedError.Error(), cmdErr.Error())
}
if string(output) != expected {
t.Logf("expected output: \n%s", expected)
t.Logf("got output: \n%s", output)
t.Errorf("received unexpected output from '%s'", test.command)
}
})
}
}