-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgttp_test.go
72 lines (63 loc) · 1.41 KB
/
gttp_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
package main
import (
"fmt"
"github.com/gttp-cli/gttp/pkg/model"
"os"
"path/filepath"
"strings"
"testing"
)
func TestTestFiles(t *testing.T) {
// Walk over "testdata" and "_examples" and execute test for all ".yml" files.
testPaths := []string{"testdata", "_examples"}
for _, testPath := range testPaths {
filepath.Walk(testPath, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".yml" {
t.Run(path, func(t *testing.T) {
mustFail := strings.Contains(path, "must-fail")
if mustFail {
testFileMustFail(t, path)
} else {
testFileMustParse(t, path)
}
})
}
return nil
})
}
}
func testFileMustParse(t *testing.T, path string) {
err := runTestFile(t, path)
if err != nil {
t.Fatal(err)
}
}
func testFileMustFail(t *testing.T, path string) {
err := runTestFile(t, path)
if err == nil {
t.Fatal("expected error, got nil")
}
}
func runTestFile(t *testing.T, path string) error {
// Read file
file, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
return nil
}
template, err := model.FromYAML(string(file))
if err != nil {
t.Fatal(err)
return nil
}
// Validate template
errs := template.Validate()
if len(errs) > 0 {
var errors []string
for _, err := range errs {
errors = append(errors, err.Error())
}
return fmt.Errorf("template validation failed:\n\n%s", strings.Join(errors, "\n"))
}
return nil
}