Skip to content

Commit 660a62f

Browse files
committed
fixing windows issues.
1 parent b183b2a commit 660a62f

3 files changed

Lines changed: 63 additions & 17 deletions

File tree

index/rolodex.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,9 @@ func (r *Rolodex) asLocalFile(file fs.File, fileLookup string) (*LocalFile, []er
771771
return wrapped, errs
772772
}
773773

774-
bytes, readErr := io.ReadAll(file)
775-
if readErr != nil {
776-
return nil, append(errorStack, readErr)
777-
}
778-
stat, statErr := file.Stat()
779-
if statErr != nil {
780-
return nil, append(errorStack, statErr)
774+
bytes, stat, errs := consumeAdaptedFile(file)
775+
if len(errs) > 0 {
776+
return nil, append(errorStack, errs...)
781777
}
782778
if len(bytes) == 0 {
783779
return nil, nil
@@ -843,13 +839,9 @@ func (r *Rolodex) asRemoteFile(file fs.File, location string) (*RemoteFile, []er
843839
return remoteFile, nil
844840
}
845841

846-
bytes, readErr := io.ReadAll(file)
847-
if readErr != nil {
848-
return nil, []error{readErr}
849-
}
850-
stat, statErr := file.Stat()
851-
if statErr != nil {
852-
return nil, []error{statErr}
842+
bytes, stat, errs := consumeAdaptedFile(file)
843+
if len(errs) > 0 {
844+
return nil, errs
853845
}
854846
if len(bytes) == 0 {
855847
return nil, nil
@@ -867,6 +859,28 @@ func (r *Rolodex) asRemoteFile(file fs.File, location string) (*RemoteFile, []er
867859
}, nil
868860
}
869861

862+
func consumeAdaptedFile(file fs.File) ([]byte, fs.FileInfo, []error) {
863+
var errorStack []error
864+
865+
bytes, readErr := io.ReadAll(file)
866+
if readErr != nil {
867+
errorStack = append(errorStack, readErr)
868+
_ = file.Close()
869+
return nil, nil, errorStack
870+
}
871+
872+
stat, statErr := file.Stat()
873+
if statErr != nil {
874+
errorStack = append(errorStack, statErr)
875+
_ = file.Close()
876+
return nil, nil, errorStack
877+
}
878+
879+
_ = file.Close()
880+
881+
return bytes, stat, nil
882+
}
883+
870884
func (r *Rolodex) wrapLocalRolodexFile(localFile *LocalFile) (RolodexFile, error) {
871885
return &rolodexFile{
872886
rolodex: r,

index/rolodex_remote_loader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"net/http"
1515
"net/url"
1616
"os"
17-
"path/filepath"
17+
"path"
1818
"strings"
1919
"sync"
2020
"sync/atomic"
@@ -764,7 +764,7 @@ func (i *RemoteFS) createRemoteFile(remoteParsedURL *url.URL, fileExt FileExtens
764764
lastModifiedTime = time.Now()
765765
}
766766
return &RemoteFile{
767-
filename: filepath.Base(remoteParsedURL.Path),
767+
filename: path.Base(remoteParsedURL.Path),
768768
name: remoteParsedURL.Path,
769769
extension: fileExt,
770770
data: responseBytes,
@@ -778,7 +778,7 @@ func (i *RemoteFS) createRemoteFile(remoteParsedURL *url.URL, fileExt FileExtens
778778
func (i *RemoteFS) createRemoteIndexConfig(remoteParsedURL, remoteParsedURLOriginal *url.URL) *SpecIndexConfig {
779779
copiedCfg := *i.indexConfig
780780
newBase := fmt.Sprintf("%s://%s%s", remoteParsedURLOriginal.Scheme, remoteParsedURLOriginal.Host,
781-
filepath.Dir(remoteParsedURL.Path))
781+
path.Dir(remoteParsedURL.Path))
782782
newBaseURL, _ := url.Parse(newBase)
783783
if newBaseURL != nil {
784784
copiedCfg.BaseURL = newBaseURL

index/rolodex_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,28 @@ func TestRolodex_AsRemoteFile_AndWrappers(t *testing.T) {
23242324
assert.Error(t, err)
23252325
}
23262326

2327+
func TestRolodex_AsLocalFile_ClosesAdaptedFile(t *testing.T) {
2328+
rolo := NewRolodex(CreateOpenAPIIndexConfig())
2329+
rolo.rootIndex = NewTestSpecIndex().Load().(*SpecIndex)
2330+
2331+
file := &closeTrackingFile{testFile: testFile{content: "hello"}}
2332+
localFile, errs := rolo.asLocalFile(file, "/tmp/spec.yaml")
2333+
assert.NotNil(t, localFile)
2334+
assert.Empty(t, errs)
2335+
assert.True(t, file.closed)
2336+
}
2337+
2338+
func TestRolodex_AsRemoteFile_ClosesAdaptedFile(t *testing.T) {
2339+
rolo := NewRolodex(CreateOpenAPIIndexConfig())
2340+
rolo.rootIndex = NewTestSpecIndex().Load().(*SpecIndex)
2341+
2342+
file := &closeTrackingFile{testFile: testFile{content: "hello"}}
2343+
remoteFile, errs := rolo.asRemoteFile(file, "http://example.com/spec.yaml")
2344+
assert.NotNil(t, remoteFile)
2345+
assert.Empty(t, errs)
2346+
assert.True(t, file.closed)
2347+
}
2348+
23272349
func TestRolodex_AsFileHelperErrors(t *testing.T) {
23282350
rolo := NewRolodex(CreateOpenAPIIndexConfig())
23292351

@@ -2437,6 +2459,11 @@ type testFile struct {
24372459
offset int64
24382460
}
24392461

2462+
type closeTrackingFile struct {
2463+
testFile
2464+
closed bool
2465+
}
2466+
24402467
type errorReadFile struct{}
24412468

24422469
func (e *errorReadFile) Read(_ []byte) (int, error) { return 0, fmt.Errorf("read failed") }
@@ -2462,6 +2489,11 @@ func (tf *testFile) Read(p []byte) (n int, err error) {
24622489

24632490
func (tf *testFile) Close() error { return nil }
24642491

2492+
func (tf *closeTrackingFile) Close() error {
2493+
tf.closed = true
2494+
return nil
2495+
}
2496+
24652497
func (tf *testFile) Stat() (fs.FileInfo, error) {
24662498
return &testFileInfo{name: "test.yaml", size: int64(len(tf.content))}, nil
24672499
}

0 commit comments

Comments
 (0)