-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
53 lines (45 loc) · 930 Bytes
/
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
package main
import (
"context"
"os"
"testing"
"rsc.io/script"
)
func TestMainGolden(t *testing.T) {
ctx := context.Background()
state, err := script.NewState(ctx, "./", nil)
if err != nil {
t.Fatal(err)
}
mainFile := "testdata/main.go"
if _, err := script.Cp().Run(state, mainFile, mainFile+".backup"); err != nil {
t.Fatal(err)
}
defer func() {
if _, err := script.Mv().Run(state, mainFile+".backup", mainFile); err != nil {
t.Fatal(err)
}
}()
f, err := os.Open("testdata/golden.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
os.Stdin = f
main()
checkContent(t, mainFile, mainFile+".golden")
}
func checkContent(t *testing.T, f1, f2 string) {
t.Helper()
got, err := os.ReadFile(f1)
if err != nil {
t.Fatal(err)
}
expected, err := os.ReadFile(f2)
if err != nil {
t.Fatal(err)
}
if string(got) != string(expected) {
t.Errorf("got %s, expected %s", got, expected)
}
}