@@ -2,6 +2,7 @@ package jsonschema
22
33import (
44 "bytes"
5+ "embed"
56 "fmt"
67 "io/fs"
78 "maps"
@@ -16,6 +17,7 @@ import (
1617
1718 "github.com/creativeprojects/resticprofile/config"
1819 "github.com/creativeprojects/resticprofile/config/mocks"
20+ "github.com/creativeprojects/resticprofile/examples"
1921 "github.com/creativeprojects/resticprofile/restic"
2022 "github.com/creativeprojects/resticprofile/util/collect"
2123 "github.com/creativeprojects/resticprofile/util/maybe"
@@ -26,6 +28,9 @@ import (
2628 "github.com/stretchr/testify/require"
2729)
2830
31+ //go:embed *.conf *.yaml
32+ var invalidConfigurationFiles embed.FS
33+
2934func compileSchema (t * testing.T , version config.Version ) * jsonschema.Schema {
3035 t .Helper ()
3136
@@ -58,14 +63,14 @@ func TestJSONSchemaValidation(t *testing.T) {
5863 schema1 := compileSchema (t , config .Version01 )
5964 schema2 := compileSchema (t , config .Version02 )
6065
61- rewriteToJson := func (t * testing.T , filename string ) string {
66+ rewriteToJson := func (t * testing.T , filename string , content [] byte ) string {
6267 t .Helper ()
6368 v := viper .New ()
6469 v .SetConfigFile (filename )
6570 if strings .HasSuffix (filename , ".conf" ) {
6671 v .SetConfigType ("toml" )
6772 }
68- require .NoError (t , v .ReadInConfig ( ))
73+ require .NoError (t , v .ReadConfig ( bytes . NewReader ( content ) ))
6974
7075 v .SetConfigType ("json" )
7176 filename = filepath .Join (t .TempDir (), filepath .Base (filename )+ ".json" )
@@ -79,15 +84,14 @@ func TestJSONSchemaValidation(t *testing.T) {
7984 t .Run ("examples" , func (t * testing.T ) {
8085 t .Parallel ()
8186
82- exclusions := regexp .MustCompile (`[\\/](rsyslogd\.conf| utf.*\.conf|drop-in-example\.conf)$` )
87+ exclusions := regexp .MustCompile (`^( utf.*\.conf|drop-in-example\.conf)$` )
8388 testCount := 0
8489
85- err := filepath .Walk ("../../examples/" , func (filename string , info fs.FileInfo , err error ) error {
86- if err != nil {
87- return err
88- }
90+ err := fs .WalkDir (examples .Files , "." , func (filename string , info fs.DirEntry , err error ) error {
91+ require .NoError (t , err )
92+
8993 if ! info .IsDir () && extensionMatcher .MatchString (filename ) && ! exclusions .MatchString (filename ) {
90- content , e := os .ReadFile (filename )
94+ content , e := fs .ReadFile (examples . Files , filename )
9195 require .NoError (t , e )
9296 if bytes .Contains (content , []byte ("{{ define" )) || bytes .Contains (content , []byte ("{{ template" )) {
9397 return nil // skip test for templates
@@ -96,12 +100,12 @@ func TestJSONSchemaValidation(t *testing.T) {
96100
97101 t .Run (path .Base (filename ), func (t * testing.T ) {
98102 if ! strings .HasSuffix (filename , ".json" ) {
99- filename = rewriteToJson (t , filename )
100- }
101- filename , _ = filepath .Abs (filename )
103+ filename = rewriteToJson (t , filename , content )
104+ filename , _ = filepath .Abs (filename )
102105
103- content , e = os .ReadFile (filename )
104- assert .NoError (t , e )
106+ content , e = os .ReadFile (filename )
107+ assert .NoError (t , e )
108+ }
105109 schema := schema1
106110 if version2Matcher .Find (content ) != nil {
107111 schema = schema2
@@ -129,19 +133,21 @@ func TestJSONSchemaValidation(t *testing.T) {
129133 t .Run ("invalid" , func (t * testing.T ) {
130134 t .Parallel ()
131135
132- err := filepath .Walk ("./" , func (filename string , info fs.FileInfo , err error ) error {
133- if ! info .IsDir () && extensionMatcher .MatchString (filename ) {
134- content , e := os .ReadFile (filename )
135- require .NoError (t , e )
136+ err := fs .WalkDir (invalidConfigurationFiles , "." , func (filename string , info fs.DirEntry , err error ) error {
137+ require .NoError (t , err )
136138
139+ if ! info .IsDir () && extensionMatcher .MatchString (filename ) {
137140 t .Run (path .Base (filename ), func (t * testing.T ) {
141+ content , err := fs .ReadFile (invalidConfigurationFiles , filename )
142+ require .NoError (t , err )
143+
138144 if ! strings .HasSuffix (filename , ".json" ) {
139- filename = rewriteToJson (t , filename )
140- }
141- filename , _ = filepath .Abs (filename )
145+ filename = rewriteToJson (t , filename , content )
146+ filename , _ = filepath .Abs (filename )
142147
143- content , e = os .ReadFile (filename )
144- assert .NoError (t , e )
148+ content , err = os .ReadFile (filename )
149+ assert .NoError (t , err )
150+ }
145151 schema := schema1
146152 if version2Matcher .Find (content ) != nil {
147153 schema = schema2
0 commit comments