11package schemagen_test
22
33import (
4- "embed"
54 "io/fs"
65 "os"
7- "path/filepath"
86 "strings"
97 "testing"
108
@@ -19,44 +17,45 @@ import (
1917)
2018
2119func TestGenerateCRD_Golden (t * testing.T ) {
22- chartLoader , err := loader .Loader ("./testdata/charts/juiceshop-chart" )
23- if err != nil {
24- t .Fatalf ("Creating chart loader failed: %v" , err )
25- }
26- chart , err := chartLoader .Load ()
27- if err != nil {
28- t .Fatalf ("Loading chart failed: %v" , err )
20+ charts , err := fs .Glob (os .DirFS ("testdata/charts" ), "*/values.yaml" )
21+ require .NoError (t , err )
22+ require .NotEmpty (t , charts , "No test charts found in testdata/charts" )
23+ for i , chartPath := range charts {
24+ charts [i ] = strings .TrimSuffix (chartPath , "/values.yaml" )
2925 }
26+ t .Log ("Found test charts:" , charts )
3027
31- crd , err := schemagen .GenerateCRD (* chart )
32- if err != nil {
33- t .Fatalf ("GenerateCRD failed: %v" , err )
34- }
28+ for _ , chartPath := range charts {
29+ t .Run (chartPath , func (t * testing.T ) {
30+ chartLoader , err := loader .Loader ("./testdata/charts/" + chartPath )
31+ require .NoError (t , err , "Creating chart loader failed for chart: %s" , chartPath )
32+ chart , err := chartLoader .Load ()
33+ require .NoError (t , err , "Loading chart failed for chart: %s" , chartPath )
3534
36- yamlData , err := kubeyaml .Marshal (crd )
37- if err != nil {
38- t .Fatalf ("Failed to marshal CRD to YAML: %v" , err )
39- }
35+ crd , err := schemagen .GenerateCRD (* chart )
36+ require .NoError (t , err , "GenerateCRD failed for chart: %s" , chartPath )
4037
41- if err := os .WriteFile ("testdata/charts/juiceshop-crd.yaml" , yamlData , 0644 ); err != nil {
42- t .Fatalf ("Failed to write CRD to file: %v" , err )
38+ yamlData , err := kubeyaml .Marshal (crd )
39+ require .NoError (t , err , "Failed to marshal CRD to YAML for chart: %s" , chartPath )
40+
41+ require .NoError (t , os .WriteFile ("./testdata/charts/" + chartPath + ".golden.yaml" , yamlData , 0644 ))
42+ })
4343 }
4444}
4545
46- //go:embed testdata/preprocess/*
47- var preprocessTestFiles embed.FS
48-
4946func Test_PreprocessYAMLHints (t * testing.T ) {
50- files , err := fs .Glob (preprocessTestFiles , "testdata/preprocess/*.processed.yaml" )
47+ preprocessTestFiles := os .DirFS ("testdata/preprocess" )
48+ files , err := fs .Glob (preprocessTestFiles , "*.processed.yaml" )
5149 require .NoError (t , err )
50+ require .NotEmpty (t , files , "No test files found in testdata/preprocess" )
5251
5352 for _ , processedFile := range files {
54- t .Run (strings .TrimSuffix (filepath . Base ( processedFile ) , ".processed.yaml" ), func (t * testing.T ) {
53+ t .Run (strings .TrimSuffix (processedFile , ".processed.yaml" ), func (t * testing.T ) {
5554 originalFile := strings .TrimSuffix (processedFile , ".processed.yaml" ) + ".yaml"
56- originalData , err := preprocessTestFiles .ReadFile (originalFile )
55+ originalData , err := fs .ReadFile (preprocessTestFiles , originalFile )
5756 require .NoError (t , err )
5857
59- expectedProcessedData , err := preprocessTestFiles .ReadFile (processedFile )
58+ expectedProcessedData , err := fs .ReadFile (preprocessTestFiles , processedFile )
6059 require .NoError (t , err )
6160
6261 actualProcessedData , err := schemagen .PreprocessYAMLHints (originalData )
0 commit comments