-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathflagd_definitions_test.go
91 lines (76 loc) · 2.33 KB
/
flagd_definitions_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
package flagd_definitions_test
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"testing"
flagd_definitions "github.com/open-feature/flagd-schemas/json"
"github.com/xeipuuv/gojsonschema"
)
var compiledFlagDefinitionSchema *gojsonschema.Schema
var compiledTargetingSchema *gojsonschema.Schema
func init() {
flagDefinitionSchemaLoader := gojsonschema.NewSchemaLoader()
flagDefinitionSchemaLoader.AddSchemas(gojsonschema.NewStringLoader(flagd_definitions.TargetingSchema))
targetingSchemaLoader := gojsonschema.NewSchemaLoader()
var err error
compiledFlagDefinitionSchema, err = flagDefinitionSchemaLoader.Compile(gojsonschema.NewStringLoader(flagd_definitions.FlagSchema))
compiledTargetingSchema, err = targetingSchemaLoader.Compile(gojsonschema.NewStringLoader(flagd_definitions.TargetingSchema))
if err != nil {
message := fmt.Errorf("err: %v", err)
log.Fatal(message)
}
}
func TestPositiveFlagParsing(t *testing.T) {
if err := walkPath(true, "./test/flags/positive", compiledFlagDefinitionSchema); err != nil {
t.Error(err)
t.FailNow()
}
}
func TestNegativeFlagParsing(t *testing.T) {
if err := walkPath(false, "./test/flags/negative", compiledFlagDefinitionSchema); err != nil {
t.Error(err)
t.FailNow()
}
}
func TestPositiveTargetingParsing(t *testing.T) {
if err := walkPath(true, "./test/targeting/positive", compiledTargetingSchema); err != nil {
t.Error(err)
t.FailNow()
}
}
func TestNegativeTargetingParsing(t *testing.T) {
if err := walkPath(false, "./test/targeting/negative", compiledTargetingSchema); err != nil {
t.Error(err)
t.FailNow()
}
}
func walkPath(shouldPass bool, root string, schema *gojsonschema.Schema) error {
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
ps := strings.Split(path, ".")
if ps[len(ps)-1] != "json" {
return nil
}
file, err := os.ReadFile(path)
if err != nil {
return err
}
flagStringLoader := gojsonschema.NewStringLoader(string(file))
p, err := schema.Validate(flagStringLoader)
if err != nil {
return err
}
if p.Valid() && shouldPass == false {
return fmt.Errorf("file %s should have failed validation, but did not", path)
}
if !p.Valid() && shouldPass == true {
return fmt.Errorf("file %s should not have failed validation, but did", path)
}
return nil
})
}