@@ -19,33 +19,35 @@ package openapitest
19
19
import (
20
20
"embed"
21
21
"errors"
22
- "path/filepath"
22
+ "io/fs"
23
+ "os"
23
24
"strings"
24
- "sync"
25
- "testing"
26
25
27
26
"k8s.io/client-go/openapi"
28
27
)
29
28
30
29
//go:embed testdata/*_openapi.json
31
- var f embed.FS
30
+ var embedded embed.FS
32
31
33
32
// NewFileClient returns a test client implementing the openapi.Client
34
- // interface, which serves a subset of hard-coded GroupVersion
35
- // Open API V3 specifications files. The subset of specifications is
36
- // located in the "testdata" subdirectory.
37
- func NewFileClient (t * testing.T ) openapi.Client {
38
- if t == nil {
39
- panic ("non-nil testing.T required; this package is only for use in tests" )
33
+ // interface, which serves Open API V3 specifications files from the
34
+ // given path, as prepared in `api/openapi-spec/v3`.
35
+ func NewFileClient (path string ) openapi.Client {
36
+ return & fileClient {f : os .DirFS (path )}
37
+ }
38
+
39
+ // NewEmbeddedFileClient returns a test client that uses the embedded
40
+ // `testdata` openapi files.
41
+ func NewEmbeddedFileClient () openapi.Client {
42
+ f , err := fs .Sub (embedded , "testdata" )
43
+ if err != nil {
44
+ panic (err )
40
45
}
41
- return & fileClient {t : t }
46
+ return & fileClient {f : f }
42
47
}
43
48
44
49
type fileClient struct {
45
- t * testing.T
46
- init sync.Once
47
- paths map [string ]openapi.GroupVersion
48
- err error
50
+ f fs.FS
49
51
}
50
52
51
53
// fileClient implements the openapi.Client interface.
@@ -60,29 +62,23 @@ var _ openapi.Client = &fileClient{}
60
62
//
61
63
// The file contents are read only once. All files must parse correctly
62
64
// into an api path, or an error is returned.
63
- func (t * fileClient ) Paths () (map [string ]openapi.GroupVersion , error ) {
64
- t .init .Do (func () {
65
- t .paths = map [string ]openapi.GroupVersion {}
66
- entries , err := f .ReadDir ("testdata" )
67
- if err != nil {
68
- t .err = err
69
- t .t .Error (err )
70
- }
71
- for _ , e := range entries {
72
- // this reverses the transformation done in hack/update-openapi-spec.sh
73
- path := strings .ReplaceAll (strings .TrimSuffix (e .Name (), "_openapi.json" ), "__" , "/" )
74
- t .paths [path ] = & fileGroupVersion {t : t .t , filename : filepath .Join ("testdata" , e .Name ())}
75
- }
76
- })
77
- return t .paths , t .err
65
+ func (f * fileClient ) Paths () (map [string ]openapi.GroupVersion , error ) {
66
+ paths := map [string ]openapi.GroupVersion {}
67
+ entries , err := fs .ReadDir (f .f , "." )
68
+ if err != nil {
69
+ return nil , err
70
+ }
71
+ for _ , e := range entries {
72
+ // this reverses the transformation done in hack/update-openapi-spec.sh
73
+ path := strings .ReplaceAll (strings .TrimSuffix (e .Name (), "_openapi.json" ), "__" , "/" )
74
+ paths [path ] = & fileGroupVersion {f : f .f , filename : e .Name ()}
75
+ }
76
+ return paths , nil
78
77
}
79
78
80
79
type fileGroupVersion struct {
81
- t * testing.T
82
- init sync.Once
80
+ f fs.FS
83
81
filename string
84
- data []byte
85
- err error
86
82
}
87
83
88
84
// fileGroupVersion implements the openapi.GroupVersion interface.
@@ -91,17 +87,10 @@ var _ openapi.GroupVersion = &fileGroupVersion{}
91
87
// Schema returns the OpenAPI V3 specification for the GroupVersion as
92
88
// unstructured bytes, or an error if the contentType is not
93
89
// "application/json" or there is an error reading the spec file. The
94
- // file is read only once. The embedded file is located in the "testdata"
95
- // subdirectory.
96
- func (t * fileGroupVersion ) Schema (contentType string ) ([]byte , error ) {
90
+ // file is read only once.
91
+ func (f * fileGroupVersion ) Schema (contentType string ) ([]byte , error ) {
97
92
if contentType != "application/json" {
98
93
return nil , errors .New ("openapitest only supports 'application/json' contentType" )
99
94
}
100
- t .init .Do (func () {
101
- t .data , t .err = f .ReadFile (t .filename )
102
- if t .err != nil {
103
- t .t .Error (t .err )
104
- }
105
- })
106
- return t .data , t .err
95
+ return fs .ReadFile (f .f , f .filename )
107
96
}
0 commit comments