Skip to content

Commit 369ca8f

Browse files
committed
fix: worked around windows-specific data race issue in tests
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent a47c162 commit 369ca8f

4 files changed

Lines changed: 28 additions & 26 deletions

File tree

circular_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package spec
66
import (
77
"encoding/json"
88
"net/http"
9-
"net/http/httptest"
109
"os"
1110
"path/filepath"
1211
"testing"
@@ -255,8 +254,7 @@ func TestExpandCircular_RemoteCircularID(t *testing.T) {
255254
func TestCircular_RemoteExpandAzure(t *testing.T) {
256255
// local copy of Azure publicIpAddress.json from azure-rest-api-specs
257256
// (Microsoft.Network/stable/2020-04-01)
258-
server := httptest.NewServer(http.FileServer(http.Dir("fixtures/azure")))
259-
defer server.Close()
257+
server := fixtureServer(t, "fixtures/azure")
260258

261259
basePath := server.URL + "/publicIpAddress.json"
262260
jazon, sch := expandThisOrDieTrying(t, basePath)

expander_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929

3030
//nolint:gochecknoglobals // it's okay to have embedded test fixtures as globals
3131
var (
32-
//go:embed fixtures/*/*.json fixtures/*/*.yaml fixtures/*/*.yml
32+
//go:embed all:fixtures
3333
fixtureAssets embed.FS
3434

3535
// PetStore20 json doc for swagger 2.0 pet store.
@@ -749,8 +749,7 @@ func TestExpand_InternalSchemas1(t *testing.T) {
749749
}
750750

751751
func TestExpand_RelativeBaseURI(t *testing.T) {
752-
server := httptest.NewServer(http.FileServer(http.Dir("fixtures/remote")))
753-
defer server.Close()
752+
server := fixtureServer(t, "fixtures/remote")
754753

755754
spec := new(Swagger)
756755

helpers_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package spec
66
import (
77
"encoding/json"
88
"fmt"
9+
"io/fs"
10+
"net/http"
11+
"net/http/httptest"
912
"regexp"
1013
"strings"
1114
"testing"
@@ -17,6 +20,22 @@ import (
1720

1821
var rex = regexp.MustCompile(`"\$ref":\s*"(.*?)"`)
1922

23+
// fixtureServer returns an httptest.Server serving the given subdirectory
24+
// from the embedded fixtureAssets FS. This avoids OS-level file serving
25+
// (and the Windows TransmitFile/sendfile code path that has a data race
26+
// in Go 1.26).
27+
func fixtureServer(t testing.TB, dir string) *httptest.Server {
28+
t.Helper()
29+
30+
sub, err := fs.Sub(fixtureAssets, dir)
31+
require.NoError(t, err)
32+
33+
server := httptest.NewServer(http.FileServerFS(sub))
34+
t.Cleanup(server.Close)
35+
36+
return server
37+
}
38+
2039
func jsonDoc(path string) (json.RawMessage, error) {
2140
data, err := loading.LoadFromFileOrHTTP(path)
2241
if err != nil {

resolver_test.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ package spec
55

66
import (
77
"encoding/json"
8-
"net/http"
9-
"net/http/httptest"
108
"os"
119
"path/filepath"
1210
"testing"
@@ -130,9 +128,7 @@ func TestResolveParamWithBase(t *testing.T) {
130128
}
131129

132130
func TestResolveRemoteRef_RootSame(t *testing.T) {
133-
fileserver := http.FileServer(http.Dir(specs))
134-
server := httptest.NewServer(fileserver)
135-
defer server.Close()
131+
server := fixtureServer(t, specs)
136132

137133
rootDoc := new(Swagger)
138134
b, err := os.ReadFile(filepath.Join(specs, "refed.json"))
@@ -158,9 +154,7 @@ func TestResolveRemoteRef_RootSame(t *testing.T) {
158154
}
159155

160156
func TestResolveRemoteRef_FromFragment(t *testing.T) {
161-
fileserver := http.FileServer(http.Dir(specs))
162-
server := httptest.NewServer(fileserver)
163-
defer server.Close()
157+
server := fixtureServer(t, specs)
164158

165159
rootDoc := new(Swagger)
166160
b, err := os.ReadFile(filepath.Join(specs, "refed.json"))
@@ -178,9 +172,7 @@ func TestResolveRemoteRef_FromFragment(t *testing.T) {
178172
}
179173

180174
func TestResolveRemoteRef_FromInvalidFragment(t *testing.T) {
181-
fileserver := http.FileServer(http.Dir(specs))
182-
server := httptest.NewServer(fileserver)
183-
defer server.Close()
175+
server := fixtureServer(t, specs)
184176

185177
rootDoc := new(Swagger)
186178
b, err := os.ReadFile(filepath.Join(specs, "refed.json"))
@@ -215,9 +207,7 @@ func TestResolveRemoteRef_FromInvalidFragment(t *testing.T) {
215207
// }
216208

217209
func TestResolveRemoteRef_ToParameter(t *testing.T) {
218-
fileserver := http.FileServer(http.Dir(specs))
219-
server := httptest.NewServer(fileserver)
220-
defer server.Close()
210+
server := fixtureServer(t, specs)
221211

222212
rootDoc := new(Swagger)
223213
b, err := os.ReadFile(filepath.Join(specs, "refed.json"))
@@ -240,9 +230,7 @@ func TestResolveRemoteRef_ToParameter(t *testing.T) {
240230
}
241231

242232
func TestResolveRemoteRef_ToPathItem(t *testing.T) {
243-
fileserver := http.FileServer(http.Dir(specs))
244-
server := httptest.NewServer(fileserver)
245-
defer server.Close()
233+
server := fixtureServer(t, specs)
246234

247235
rootDoc := new(Swagger)
248236
b, err := os.ReadFile(filepath.Join(specs, "refed.json"))
@@ -259,9 +247,7 @@ func TestResolveRemoteRef_ToPathItem(t *testing.T) {
259247
}
260248

261249
func TestResolveRemoteRef_ToResponse(t *testing.T) {
262-
fileserver := http.FileServer(http.Dir(specs))
263-
server := httptest.NewServer(fileserver)
264-
defer server.Close()
250+
server := fixtureServer(t, specs)
265251

266252
rootDoc := new(Swagger)
267253
b, err := os.ReadFile(filepath.Join(specs, "refed.json"))

0 commit comments

Comments
 (0)