-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemacheck_test.go
117 lines (103 loc) · 3.39 KB
/
schemacheck_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/xeipuuv/gojsonschema"
)
// ===============================================
// Tests against the CheckFileIsSupported function for various file types
func TestCheckFileIsSupportedYaml(t *testing.T) {
file := "test_data/values.yaml"
valid, err := CheckFileIsSupported(file, "yaml")
if err != nil {
t.Fatalf("An error occured during validation of file that should not have occurred:\n %s", err)
}
assert.True(t, valid)
}
func TestCheckFileIsSupportedYml(t *testing.T) {
file := "test_data/values.yml"
valid, err := CheckFileIsSupported(file, "yml")
if err != nil {
t.Fatalf("An error occured during validation of file that should not have occurred:\n %s", err)
}
assert.True(t, valid)
}
func TestCheckFileIsSupportedJSON(t *testing.T) {
file := "test_data/values.json"
valid, err := CheckFileIsSupported(file, "json")
if err != nil {
t.Fatalf("An error occured during validation of file that should not have occurred:\n %s", err)
}
assert.True(t, valid)
}
func TestCheckFileIsSupportedTxt(t *testing.T) {
file := "test_data/values.txt"
_, err := CheckFileIsSupported(file, "txt")
assert.Error(t, err)
}
// ===============================================
// Tests GetFileExt function for various filetypes
func TestGetFileExtYaml(t *testing.T) {
file := "test_data/values.yaml"
fileExt, _ := GetFileExt(file)
assert.Equal(t, "yaml", fileExt)
}
func TestGetFileExtYml(t *testing.T) {
file := "test_data/values.yml"
fileExt, _ := GetFileExt(file)
assert.Equal(t, "yml", fileExt)
}
func TestGetFileExtJSON(t *testing.T) {
file := "test_data/values.json"
fileExt, _ := GetFileExt(file)
assert.Equal(t, "json", fileExt)
}
func TestGetFileExtNoSeparator(t *testing.T) {
file := "test_data/noseparator"
_, err := GetFileExt(file)
assert.Error(t, err)
}
// ===============================================
// Tests Validate against test data files
func TestValidateValidYaml(t *testing.T) {
file := "test_data/values.yaml"
fileExt := "yaml"
schema, err := os.ReadFile(filepath.Clean("test_data/schema.json"))
if err != nil {
errLogger.Panicf("Could not read schema file: '%s' cleanly.", Schema)
}
loadedSchema := gojsonschema.NewBytesLoader(schema)
assert.NoError(t, Validate(file, fileExt, loadedSchema))
}
func TestValidateValidJSON(t *testing.T) {
file := "test_data/values.json"
fileExt := "yaml"
schema, err := os.ReadFile(filepath.Clean("test_data/schema.json"))
if err != nil {
errLogger.Panicf("Could not read schema file: '%s' cleanly.", Schema)
}
loadedSchema := gojsonschema.NewBytesLoader(schema)
assert.NoError(t, Validate(file, fileExt, loadedSchema))
}
func TestValidateInvalidYaml(t *testing.T) {
file := "test_data/invalid.yaml"
fileExt := "yaml"
schema, err := os.ReadFile(filepath.Clean("test_data/schema.json"))
if err != nil {
errLogger.Panicf("Could not read schema file: '%s' cleanly.", Schema)
}
loadedSchema := gojsonschema.NewBytesLoader(schema)
assert.Error(t, Validate(file, fileExt, loadedSchema))
}
func TestValidateInvalidJSON(t *testing.T) {
file := "test_data/invalid.json"
fileExt := "yaml"
schema, err := os.ReadFile(filepath.Clean("test_data/schema.json"))
if err != nil {
errLogger.Panicf("Could not read schema file: '%s' cleanly.", Schema)
}
loadedSchema := gojsonschema.NewBytesLoader(schema)
assert.Error(t, Validate(file, fileExt, loadedSchema))
}