-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain_test.go
66 lines (60 loc) · 1.15 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
package main_test
import (
"bytes"
"flag"
"os"
"os/exec"
"testing"
"time"
)
var update = flag.Bool("update", false, "update .golden files")
func TestYamlDiff(t *testing.T) {
goInstall(t)
goldenfile := "testdata/diff.golden"
if *update {
err := os.WriteFile(goldenfile, runYamldiff(t), 0644)
if err != nil {
t.Fatal(err)
}
}
contents, err := os.ReadFile(goldenfile)
want := string(contents)
if err != nil {
t.Fatal(err)
}
got := string(runYamldiff(t))
if got != want {
t.Errorf("got: %v want: %v", got, want)
}
}
func goInstall(t *testing.T) {
install := exec.Command("go", "build")
err := install.Run()
if err != nil {
t.Fatal(err)
}
}
func runYamldiff(t *testing.T) []byte {
var out bytes.Buffer
yamldiff := exec.Command("./yamldiff", "testdata/1.yml", "testdata/2.yml")
yamldiff.Stdout = &out
err := yamldiff.Start()
if err != nil {
t.Fatal(err)
}
done := make(chan bool)
go func() {
err = yamldiff.Wait()
done <- true
}()
timeout := time.Millisecond * 1000
select {
case <-done:
if err != nil {
t.Fatal(err)
}
case <-time.After(timeout):
t.Fatalf("timed out after %v", timeout)
}
return out.Bytes()
}