Skip to content

Commit 9aa7f3e

Browse files
committed
Fix pnpvfs test in windows
1 parent e4a3c4b commit 9aa7f3e

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

internal/vfs/pnpvfs/pnpvfs.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,19 @@ func makeVirtualPath(basePath string, hash string, targetPath string) string {
233233

234234
return path.Join(basePath, hash, strconv.Itoa(depth), subPath)
235235
}
236+
237+
func (pnpFS *pnpFS) ClearCache() error {
238+
pnpFS.cacheReaderMutex.Lock()
239+
defer pnpFS.cacheReaderMutex.Unlock()
240+
241+
for _, reader := range pnpFS.cachedZipReadersMap {
242+
err := reader.Close()
243+
if err != nil {
244+
return err
245+
}
246+
}
247+
248+
pnpFS.cachedZipReadersMap = make(map[string]*zip.ReadCloser)
249+
250+
return nil
251+
}

internal/vfs/pnpvfs/pnpvfs_test.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package pnpvfs_test
22

33
import (
44
"archive/zip"
5-
"fmt"
65
"os"
76
"strings"
87
"testing"
@@ -16,7 +15,7 @@ import (
1615
"gotest.tools/v3/assert"
1716
)
1817

19-
func createTestZip(t *testing.T, files map[string]string) string {
18+
func createTestZip(t *testing.T, files map[string]string) (string, vfs.FS) {
2019
t.Helper()
2120

2221
tmpDir := t.TempDir()
@@ -36,7 +35,14 @@ func createTestZip(t *testing.T, files map[string]string) string {
3635
assert.NilError(t, err)
3736
}
3837

39-
return zipPath
38+
fs := pnpvfs.From(osvfs.FS())
39+
40+
t.Cleanup(func() {
41+
errClear := fs.ClearCache()
42+
assert.NilError(t, errClear)
43+
})
44+
45+
return zipPath, fs
4046
}
4147

4248
func TestPnpVfs_BasicFileOperations(t *testing.T) {
@@ -85,15 +91,8 @@ func TestPnpVfs_ZipFileDetection(t *testing.T) {
8591
"package.json": `{"name": "test-project"}`,
8692
}
8793

88-
zipPath := createTestZip(t, zipFiles)
89-
90-
underlyingFS := vfstest.FromMap(map[string]string{
91-
zipPath: "zip content placeholder",
92-
}, true)
94+
zipPath, fs := createTestZip(t, zipFiles)
9395

94-
fs := pnpvfs.From(underlyingFS)
95-
96-
fmt.Println(zipPath)
9796
assert.Assert(t, fs.FileExists(zipPath))
9897

9998
zipInternalPath := zipPath + "/src/index.ts"
@@ -137,10 +136,10 @@ func TestPnpVfs_ErrorHandling(t *testing.T) {
137136
zipFiles := map[string]string{
138137
"src/index.ts": "export const hello = 'world';",
139138
}
140-
zipPath := createTestZip(t, zipFiles)
139+
zipPath, zipFS := createTestZip(t, zipFiles)
141140

142141
testutil.AssertPanics(t, func() {
143-
_ = fs.WriteFile(zipPath+"/src/index.ts", "hello, world", false)
142+
_ = zipFS.WriteFile(zipPath+"/src/index.ts", "hello, world", false)
144143
}, "cannot write to zip file")
145144
})
146145
}
@@ -249,8 +248,7 @@ func TestPnpVfs_RealZipIntegration(t *testing.T) {
249248
"tsconfig.json": `{"compilerOptions": {"target": "es2020"}}`,
250249
}
251250

252-
zipPath := createTestZip(t, zipFiles)
253-
fs := pnpvfs.From(osvfs.FS())
251+
zipPath, fs := createTestZip(t, zipFiles)
254252

255253
assert.Assert(t, fs.FileExists(zipPath))
256254

0 commit comments

Comments
 (0)