-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts2go_test.go
82 lines (66 loc) · 1.93 KB
/
ts2go_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
package ts2go_test
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/armsnyder/ts2go"
)
func TestGenerate(t *testing.T) {
const testdataDir = "internal/testdata"
dirs, err := os.ReadDir(testdataDir)
if err != nil {
t.Fatal(err)
}
for _, dir := range dirs {
t.Run(
dir.Name(),
func(t *testing.T) {
const sourceFile = "source.ts"
const expectedFile = "expected.go"
source, err := os.ReadFile(filepath.Join(testdataDir, dir.Name(), sourceFile))
if err != nil {
t.Fatalf("failed to read source.ts: %v", err)
}
expected, err := os.ReadFile(filepath.Join(testdataDir, dir.Name(), expectedFile))
if err != nil {
t.Fatalf("failed to read expected.go: %v", err)
}
generated := &bytes.Buffer{}
if err := ts2go.Generate(bytes.NewReader(source), generated); err != nil {
t.Fatalf("failed to generate code: %v", err)
}
if !bytes.Equal(generated.Bytes(), expected) {
t.Errorf("Generated code does not match expected output")
logDiff(t, expected, generated.Bytes())
}
},
)
}
}
func logDiff(t *testing.T, expected, generated []byte) {
t.Helper()
path, err := exec.LookPath("diff")
if err != nil {
t.Log("Install diff(1) to see a prettier diff")
t.Logf("\n=== Expected ===\n%s\n\n=== Generated ===\n%s", expected, generated)
return
}
tmpDir := t.TempDir()
expectedPath := filepath.Join(tmpDir, "expected")
generatedPath := filepath.Join(tmpDir, "generated")
if err := os.WriteFile(expectedPath, expected, 0o600); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
if err := os.WriteFile(generatedPath, generated, 0o600); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
cmd := exec.Command(path, "-u", "--label", "expected", "--label", "generated", expectedPath, generatedPath)
buf := &bytes.Buffer{}
cmd.Stdout = buf
cmd.Stderr = buf
if err := cmd.Run(); err != nil {
t.Logf("\n%s", buf.String())
}
}