Skip to content

Commit 36c114c

Browse files
committed
Fix pnpvfs test in windows
1 parent e4a3c4b commit 36c114c

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
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: 20 additions & 16 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()
@@ -35,8 +34,21 @@ func createTestZip(t *testing.T, files map[string]string) string {
3534
_, err = f.Write([]byte(content))
3635
assert.NilError(t, err)
3736
}
37+
38+
underlyingFS := vfstest.FromMap(map[string]string{
39+
zipPath: "zip content placeholder",
40+
}, true)
41+
42+
fs := pnpvfs.From(underlyingFS)
43+
44+
t.Cleanup(func() {
45+
err := fs.ClearCache()
46+
if err != nil {
47+
t.Errorf("Failed to clear PnP cache: %v", err)
48+
}
49+
})
3850

39-
return zipPath
51+
return zipPath, fs
4052
}
4153

4254
func TestPnpVfs_BasicFileOperations(t *testing.T) {
@@ -85,15 +97,8 @@ func TestPnpVfs_ZipFileDetection(t *testing.T) {
8597
"package.json": `{"name": "test-project"}`,
8698
}
8799

88-
zipPath := createTestZip(t, zipFiles)
89-
90-
underlyingFS := vfstest.FromMap(map[string]string{
91-
zipPath: "zip content placeholder",
92-
}, true)
93-
94-
fs := pnpvfs.From(underlyingFS)
95-
96-
fmt.Println(zipPath)
100+
zipPath, fs := createTestZip(t, zipFiles)
101+
97102
assert.Assert(t, fs.FileExists(zipPath))
98103

99104
zipInternalPath := zipPath + "/src/index.ts"
@@ -137,10 +142,10 @@ func TestPnpVfs_ErrorHandling(t *testing.T) {
137142
zipFiles := map[string]string{
138143
"src/index.ts": "export const hello = 'world';",
139144
}
140-
zipPath := createTestZip(t, zipFiles)
145+
zipPath, zipFS := createTestZip(t, zipFiles)
141146

142147
testutil.AssertPanics(t, func() {
143-
_ = fs.WriteFile(zipPath+"/src/index.ts", "hello, world", false)
148+
_ = zipFS.WriteFile(zipPath+"/src/index.ts", "hello, world", false)
144149
}, "cannot write to zip file")
145150
})
146151
}
@@ -249,8 +254,7 @@ func TestPnpVfs_RealZipIntegration(t *testing.T) {
249254
"tsconfig.json": `{"compilerOptions": {"target": "es2020"}}`,
250255
}
251256

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

255259
assert.Assert(t, fs.FileExists(zipPath))
256260

0 commit comments

Comments
 (0)