1
1
package main
2
2
3
3
import (
4
- "fmt"
5
- "github.com/stretchr/testify/assert"
6
- "os"
7
- "os/exec"
8
- "path/filepath"
9
- "strings"
10
- "testing"
4
+ "fmt"
5
+ "github.com/stretchr/testify/assert"
6
+ "os"
7
+ "os/exec"
8
+ "path/filepath"
9
+ "strings"
10
+ "testing"
11
11
)
12
12
13
13
// Overall approach taken from https://github.com/mix-php/mix/blob/master/src/grpc/protoc-gen-mix/plugin_test.go
@@ -16,124 +16,156 @@ import (
16
16
// tests and instead act as protoc-gen-avro. This allows the test binary to
17
17
// pass itself to protoc.
18
18
func init () {
19
- if os .Getenv ("RUN_AS_PROTOC_GEN_AVRO" ) != "" {
20
- main ()
21
- os .Exit (0 )
22
- }
19
+ if os .Getenv ("RUN_AS_PROTOC_GEN_AVRO" ) != "" {
20
+ main ()
21
+ os .Exit (0 )
22
+ }
23
23
}
24
24
25
- func fileNames (directory string , appendDirectory bool ) ([]string , error ) {
26
- files , err := os .ReadDir (directory )
27
- if err != nil {
28
- return nil , fmt .Errorf ("can't read %s directory: %w" , directory , err )
29
- }
30
- var names []string
31
- for _ , file := range files {
32
- if file .IsDir () {
33
- continue
34
- }
35
- if appendDirectory {
36
- names = append (names , filepath .Base (directory ) + "/" + file .Name ())
37
- } else {
38
- names = append (names , file .Name ())
39
- }
40
- }
41
- return names , nil
25
+ func fileNames (directory string , appendDirectory bool , recurse bool ) ([]string , error ) {
26
+ // var rootPath string
27
+ // var names []string
28
+ // err := filepath.Walk(directory,
29
+ // func(path string, info os.FileInfo, err error) error {
30
+ // if err != nil {
31
+ // return err
32
+ // }
33
+ //
34
+ // if info.Name() == directory
35
+ //
36
+ // if appendDirectory {
37
+ // names = append(names, filepath.Base(directory)+"/"+path)
38
+ // } else {
39
+ // names = append(names, path)
40
+ // }
41
+ // return nil
42
+ // })
43
+ // if err != nil {
44
+ // return nil, fmt.Errorf("can't read %s directory: %w", directory, err)
45
+ // }
46
+
47
+ files , err := os .ReadDir (directory )
48
+ if err != nil {
49
+ return nil , fmt .Errorf ("can't read %s directory: %w" , directory , err )
50
+ }
51
+ var names []string
52
+ for _ , file := range files {
53
+ if file .IsDir () {
54
+ if recurse {
55
+ r_names , err := fileNames (directory + "/" + file .Name (), true , true )
56
+ if err != nil {
57
+ return nil , err
58
+ }
59
+ names = append (names , r_names ... )
60
+ }
61
+ continue
62
+ }
63
+ if appendDirectory {
64
+ names = append (names , filepath .Base (directory )+ "/" + file .Name ())
65
+ } else {
66
+ names = append (names , file .Name ())
67
+ }
68
+ }
69
+ return names , nil
42
70
}
43
71
44
72
func runTest (t * testing.T , directory string , options map [string ]string ) {
45
- workdir , _ := os .Getwd ()
46
- tmpdir , err := os .MkdirTemp (workdir , "proto-test." )
47
- if err != nil {
48
- t .Fatal (err )
49
- }
50
- defer os .RemoveAll (tmpdir )
51
-
52
- args := []string {
53
- "-I." ,
54
- "--avro_out=" + tmpdir ,
55
- }
56
- names , err := fileNames (workdir + "/testdata" , true )
57
- if err != nil {
58
- t .Fatal (fmt .Errorf ("testData fileNames %w" , err ))
59
- }
60
- for _ , name := range names {
61
- args = append (args , name )
62
- }
63
- for k , v := range options {
64
- args = append (args , "--avro_opt=" + k + "=" + v )
65
- }
66
- protoc (t , args )
67
-
68
- testDir := workdir + "/testdata/" + directory
69
- if os .Getenv ("UPDATE_SNAPSHOTS" ) != "" {
70
- cmd := exec .Command ("/bin/sh" , "-c" , fmt .Sprintf ("cp %v/* %v" , tmpdir , testDir ))
71
- cmd .Run ()
72
- } else {
73
- assertEqualFiles (t , testDir , tmpdir )
74
- }
73
+ workdir , _ := os .Getwd ()
74
+ tmpdir , err := os .MkdirTemp (workdir , "proto-test." )
75
+ if err != nil {
76
+ t .Fatal (err )
77
+ }
78
+ defer os .RemoveAll (tmpdir )
79
+
80
+ args := []string {
81
+ "-I." ,
82
+ "--avro_out=" + tmpdir ,
83
+ }
84
+ names , err := fileNames (workdir + "/testdata" , true , false )
85
+ if err != nil {
86
+ t .Fatal (fmt .Errorf ("testData fileNames %w" , err ))
87
+ }
88
+ for _ , name := range names {
89
+ args = append (args , name )
90
+ }
91
+ for k , v := range options {
92
+ args = append (args , "--avro_opt=" + k + "=" + v )
93
+ }
94
+ protoc (t , args )
95
+
96
+ testDir := workdir + "/testdata/" + directory
97
+ if os .Getenv ("UPDATE_SNAPSHOTS" ) != "" {
98
+ cmd := exec .Command ("/bin/sh" , "-c" , fmt .Sprintf ("cp -r %v/* %v" , tmpdir , testDir ))
99
+ cmd .Run ()
100
+ } else {
101
+ assertEqualFiles (t , testDir , tmpdir )
102
+ }
75
103
}
76
104
77
105
func Test_Base (t * testing.T ) {
78
- runTest (t , "base" , map [string ]string {})
106
+ runTest (t , "base" , map [string ]string {})
79
107
}
80
108
81
109
func Test_CollapseFields (t * testing.T ) {
82
- runTest (t , "collapse_fields" , map [string ]string {"collapse_fields" : "StringList" })
110
+ runTest (t , "collapse_fields" , map [string ]string {"collapse_fields" : "StringList" })
83
111
}
84
112
85
113
func Test_EmitOnly (t * testing.T ) {
86
- runTest (t , "emit_only" , map [string ]string {"emit_only" : "Widget" })
114
+ runTest (t , "emit_only" , map [string ]string {"emit_only" : "Widget" })
87
115
}
88
116
89
117
func Test_NamespaceMap (t * testing.T ) {
90
- runTest (t , "namespace_map" , map [string ]string {"namespace_map" : "testdata:mynamespace" })
118
+ runTest (t , "namespace_map" , map [string ]string {"namespace_map" : "testdata:mynamespace" })
91
119
}
92
120
93
121
func Test_PreserveNonStringMaps (t * testing.T ) {
94
- runTest (t , "preserve_non_string_maps" , map [string ]string {"preserve_non_string_maps" : "true" })
122
+ runTest (t , "preserve_non_string_maps" , map [string ]string {"preserve_non_string_maps" : "true" })
123
+ }
124
+
125
+ func Test_PrefixSchemaFilesWithPackage (t * testing.T ) {
126
+ runTest (t , "prefix_schema_files_with_package" , map [string ]string {"prefix_schema_files_with_package" : "true" })
95
127
}
96
128
97
129
func assertEqualFiles (t * testing.T , original , generated string ) {
98
- names , err := fileNames (original , false )
99
- if err != nil {
100
- t .Fatal (fmt .Errorf ("original fileNames %w" , err ))
101
- }
102
- generatedNames , err := fileNames (generated , false )
103
- if err != nil {
104
- t .Fatal (fmt .Errorf ("generated fileNames %w" , err ))
105
- }
106
- assert .Equal (t , names , generatedNames )
107
- for i , name := range names {
108
- originalData , err := os .ReadFile (original + "/" + name )
109
- if err != nil {
110
- t .Fatal ("Can't find original file for comparison" )
111
- }
112
-
113
- generatedData , err := os .ReadFile (generated + "/" + generatedNames [i ])
114
- if err != nil {
115
- t .Fatal ("Can't find generated file for comparison" )
116
- }
117
- r := strings .NewReplacer ("\r \n " , "" , "\n " , "" )
118
- assert .Equal (t , r .Replace (string (originalData )), r .Replace (string (generatedData )))
119
- }
130
+ names , err := fileNames (original , false , true )
131
+ if err != nil {
132
+ t .Fatal (fmt .Errorf ("original fileNames %w" , err ))
133
+ }
134
+ generatedNames , err := fileNames (generated , false , true )
135
+ if err != nil {
136
+ t .Fatal (fmt .Errorf ("generated fileNames %w" , err ))
137
+ }
138
+ assert .Equal (t , names , generatedNames )
139
+ for i , name := range names {
140
+ originalData , err := os .ReadFile (original + "/" + name )
141
+ if err != nil {
142
+ t .Fatal ("Can't find original file for comparison" )
143
+ }
144
+
145
+ generatedData , err := os .ReadFile (generated + "/" + generatedNames [i ])
146
+ if err != nil {
147
+ t .Fatal ("Can't find generated file for comparison" )
148
+ }
149
+ r := strings .NewReplacer ("\r \n " , "" , "\n " , "" )
150
+ assert .Equal (t , r .Replace (string (originalData )), r .Replace (string (generatedData )))
151
+ }
120
152
}
121
153
122
154
func protoc (t * testing.T , args []string ) {
123
- cmd := exec .Command ("protoc" , "--plugin=protoc-gen-avro=" + os .Args [0 ])
124
- cmd .Args = append (cmd .Args , args ... )
125
- cmd .Env = append (os .Environ (), "RUN_AS_PROTOC_GEN_AVRO=1" )
126
- out , err := cmd .CombinedOutput ()
127
-
128
- if len (out ) > 0 || err != nil {
129
- t .Log ("RUNNING: " , strings .Join (cmd .Args , " " ))
130
- }
131
-
132
- if len (out ) > 0 {
133
- t .Log (string (out ))
134
- }
135
-
136
- if err != nil {
137
- t .Fatalf ("protoc: %v" , err )
138
- }
155
+ cmd := exec .Command ("protoc" , "--plugin=protoc-gen-avro=" + os .Args [0 ])
156
+ cmd .Args = append (cmd .Args , args ... )
157
+ cmd .Env = append (os .Environ (), "RUN_AS_PROTOC_GEN_AVRO=1" )
158
+ out , err := cmd .CombinedOutput ()
159
+
160
+ if len (out ) > 0 || err != nil {
161
+ t .Log ("RUNNING: " , strings .Join (cmd .Args , " " ))
162
+ }
163
+
164
+ if len (out ) > 0 {
165
+ t .Log (string (out ))
166
+ }
167
+
168
+ if err != nil {
169
+ t .Fatalf ("protoc: %v" , err )
170
+ }
139
171
}
0 commit comments