@@ -34,6 +34,9 @@ import (
34
34
"github.com/objectbox/objectbox-go/test/build"
35
35
)
36
36
37
+ // this containing module name - used for test case modules
38
+ const moduleName = "github.com/objectbox/objectbox-go"
39
+
37
40
// generateAllDirs walks through the "data" and generates bindings for each subdirectory
38
41
// set overwriteExpected to TRUE to update all ".expected" files with the generated content
39
42
func generateAllDirs (t * testing.T , overwriteExpected bool ) {
@@ -54,7 +57,57 @@ func generateAllDirs(t *testing.T, overwriteExpected bool) {
54
57
}
55
58
}
56
59
57
- func generateOneDir (t * testing.T , overwriteExpected bool , dir string ) {
60
+ func generateOneDir (t * testing.T , overwriteExpected bool , srcDir string ) {
61
+ var dir = srcDir
62
+
63
+ var errorTransformer = func (err error ) error {
64
+ return err
65
+ }
66
+
67
+ var cleanup = func () {}
68
+ defer func () {
69
+ cleanup ()
70
+ }()
71
+
72
+ // Test in a temporary directory - if tested by an end user, the repo is read-only.
73
+ // This doesn't apply if overwriteExpected is set, as that's only supposed to be run during this lib's development.
74
+ if ! overwriteExpected {
75
+ tempRoot , err := ioutil .TempDir ("" , "objectbox-generator-test" )
76
+ assert .NoErr (t , err )
77
+
78
+ // we can't defer directly because compilation step is run in a separate goroutine after this function exits
79
+ cleanup = func () {
80
+ assert .NoErr (t , os .RemoveAll (tempRoot ))
81
+ }
82
+
83
+ // copy the source dir, including the relative paths (to make sure expected errors contain same paths)
84
+ var tempDir = filepath .Join (tempRoot , srcDir )
85
+ assert .NoErr (t , copyDirectory (srcDir , tempDir , 0700 , 0600 ))
86
+ t .Logf ("Testing in a temporary directory %s" , tempDir )
87
+
88
+ // When outside of the project's directory, we need to set up the whole temp dir as its own module, otherwise
89
+ // it won't find this `objectbox-go`. Therefore, we create a go.mod file pointing it to the right path.
90
+ cwd , err := os .Getwd ()
91
+ assert .NoErr (t , err )
92
+ var modulePath = "example.com/virtual/objectbox-go/test/generator/" + srcDir
93
+ var goMod = "module " + modulePath + "\n " +
94
+ "replace " + moduleName + " => " + filepath .Join (cwd , "/../../" ) + "\n " +
95
+ "require " + moduleName + " v0.0.0"
96
+ assert .NoErr (t , ioutil .WriteFile (path .Join (tempDir , "go.mod" ), []byte (goMod ), 0600 ))
97
+
98
+ // NOTE: we can't change directory using os.Chdir() because it applies to a process/thread, not a goroutine.
99
+ // Therefore, we just map paths in received errors, so they match the expected ones.
100
+ dir = tempDir
101
+ errorTransformer = func (err error ) error {
102
+ if err == nil {
103
+ return nil
104
+ }
105
+ var str = strings .Replace (err .Error (), tempRoot + string (os .PathSeparator ), "" , - 1 )
106
+ str = strings .Replace (str , modulePath , moduleName + "/test/generator/" + srcDir , - 1 )
107
+ return errors .New (str )
108
+ }
109
+ }
110
+
58
111
modelInfoFile := generator .ModelInfoFile (dir )
59
112
modelInfoExpectedFile := modelInfoFile + ".expected"
60
113
@@ -76,18 +129,23 @@ func generateOneDir(t *testing.T, overwriteExpected bool, dir string) {
76
129
initialFiles , err := filepath .Glob (filepath .Join (dir , "*.initial" ))
77
130
assert .NoErr (t , err )
78
131
for _ , initialFile := range initialFiles {
79
- assert .NoErr (t , copyFile (initialFile , initialFile [0 :len (initialFile )- len (".initial" )]))
132
+ assert .NoErr (t , copyFile (initialFile , initialFile [0 :len (initialFile )- len (".initial" )], 0 ))
80
133
}
81
134
82
- generateAllFiles (t , overwriteExpected , dir , modelInfoFile )
135
+ generateAllFiles (t , overwriteExpected , dir , modelInfoFile , errorTransformer )
83
136
84
137
assertSameFile (t , modelInfoFile , modelInfoExpectedFile , overwriteExpected )
85
138
assertSameFile (t , modelFile , modelExpectedFile , overwriteExpected )
86
139
}
87
140
88
141
// verify the result can be built
89
142
if ! testing .Short () {
143
+ // override the defer to prevent cleanup before compilation is actually run
144
+ var cleanupAfterCompile = cleanup
145
+ cleanup = func () {}
146
+
90
147
t .Run ("compile" , func (t * testing.T ) {
148
+ defer cleanupAfterCompile ()
91
149
t .Parallel ()
92
150
93
151
var expectedError error
@@ -97,7 +155,7 @@ func generateOneDir(t *testing.T, overwriteExpected bool, dir string) {
97
155
expectedError = errors .New (string (content ))
98
156
}
99
157
100
- stdOut , stdErr , err := build .Package ("./" + dir )
158
+ stdOut , stdErr , err := build .Package (dir )
101
159
if err == nil && expectedError == nil {
102
160
// successful
103
161
return
@@ -107,7 +165,11 @@ func generateOneDir(t *testing.T, overwriteExpected bool, dir string) {
107
165
assert .Failf (t , "Unexpected PASS during compilation" )
108
166
}
109
167
110
- var receivedError = fmt .Errorf ("%s\n %s\n %s" , stdOut , stdErr , err )
168
+ // On Windows, we're getting a `go finding` message during the build - remove it to be consistent.
169
+ var reg = regexp .MustCompile ("go: finding " + moduleName + " v0.0.0[ \r \n ]+" )
170
+ stdErr = reg .ReplaceAll (stdErr , nil )
171
+
172
+ var receivedError = errorTransformer (fmt .Errorf ("%s\n %s\n %s" , stdOut , stdErr , err ))
111
173
112
174
// Fix paths in the error output on Windows so that it matches the expected error (which always uses '/').
113
175
if os .PathSeparator != '/' {
@@ -137,7 +199,7 @@ func assertSameFile(t *testing.T, file string, expectedFile string, overwriteExp
137
199
assert .NoErr (t , err )
138
200
139
201
if overwriteExpected {
140
- assert .NoErr (t , copyFile (file , expectedFile ))
202
+ assert .NoErr (t , copyFile (file , expectedFile , 0 ))
141
203
}
142
204
143
205
contentExpected , err := ioutil .ReadFile (expectedFile )
@@ -148,7 +210,7 @@ func assertSameFile(t *testing.T, file string, expectedFile string, overwriteExp
148
210
}
149
211
}
150
212
151
- func generateAllFiles (t * testing.T , overwriteExpected bool , dir string , modelInfoFile string ) {
213
+ func generateAllFiles (t * testing.T , overwriteExpected bool , dir string , modelInfoFile string , errorTransformer func ( error ) error ) {
152
214
var modelFile = generator .ModelFile (modelInfoFile )
153
215
154
216
// remove generated files during development (they might be syntactically wrong)
@@ -176,7 +238,7 @@ func generateAllFiles(t *testing.T, overwriteExpected bool, dir string, modelInf
176
238
177
239
t .Logf (" %s" , filepath .Base (sourceFile ))
178
240
179
- err = generator .Process (sourceFile , getOptions (t , sourceFile , modelInfoFile ))
241
+ err = errorTransformer ( generator .Process (sourceFile , getOptions (t , sourceFile , modelInfoFile ) ))
180
242
181
243
// handle negative test
182
244
var shouldFail = strings .HasSuffix (filepath .Base (sourceFile ), ".fail.go" )
0 commit comments