Skip to content

Commit f886aba

Browse files
committed
Test fs refactor
1 parent 39abf37 commit f886aba

File tree

3 files changed

+15
-104
lines changed

3 files changed

+15
-104
lines changed

internal/execute/testfs_test.go

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@ import (
66
)
77

88
type testFsTrackingLibs struct {
9-
fs vfs.FS
9+
vfs.FS
1010
defaultLibs *collections.SyncSet[string]
1111
}
1212

13-
var _ vfs.FS = (*testFsTrackingLibs)(nil)
14-
1513
func NewFSTrackingLibs(fs vfs.FS) *testFsTrackingLibs {
16-
return &testFsTrackingLibs{
17-
fs: fs,
18-
}
19-
}
20-
21-
func (f *testFsTrackingLibs) FS() vfs.FS {
22-
return f.fs
14+
return &testFsTrackingLibs{FS: fs}
2315
}
2416

2517
func (f *testFsTrackingLibs) removeIgnoreLibPath(path string) {
@@ -28,56 +20,20 @@ func (f *testFsTrackingLibs) removeIgnoreLibPath(path string) {
2820
}
2921
}
3022

31-
func (f *testFsTrackingLibs) UseCaseSensitiveFileNames() bool {
32-
return f.fs.UseCaseSensitiveFileNames()
33-
}
34-
35-
// FileExists returns true if the file exists.
36-
func (f *testFsTrackingLibs) FileExists(path string) bool {
37-
return f.fs.FileExists(path)
38-
}
39-
4023
// ReadFile reads the file specified by path and returns the content.
4124
// If the file fails to be read, ok will be false.
4225
func (f *testFsTrackingLibs) ReadFile(path string) (contents string, ok bool) {
4326
f.removeIgnoreLibPath(path)
44-
return f.fs.ReadFile(path)
27+
return f.FS.ReadFile(path)
4528
}
4629

4730
func (f *testFsTrackingLibs) WriteFile(path string, data string, writeByteOrderMark bool) error {
4831
f.removeIgnoreLibPath(path)
49-
return f.fs.WriteFile(path, data, writeByteOrderMark)
32+
return f.FS.WriteFile(path, data, writeByteOrderMark)
5033
}
5134

5235
// Removes `path` and all its contents. Will return the first error it encounters.
5336
func (f *testFsTrackingLibs) Remove(path string) error {
5437
f.removeIgnoreLibPath(path)
55-
return f.fs.Remove(path)
56-
}
57-
58-
// DirectoryExists returns true if the path is a directory.
59-
func (f *testFsTrackingLibs) DirectoryExists(path string) bool {
60-
return f.fs.DirectoryExists(path)
61-
}
62-
63-
// GetAccessibleEntries returns the files/directories in the specified directory.
64-
// If any entry is a symlink, it will be followed.
65-
func (f *testFsTrackingLibs) GetAccessibleEntries(path string) vfs.Entries {
66-
return f.fs.GetAccessibleEntries(path)
67-
}
68-
69-
func (f *testFsTrackingLibs) Stat(path string) vfs.FileInfo {
70-
return f.fs.Stat(path)
71-
}
72-
73-
// WalkDir walks the file tree rooted at root, calling walkFn for each file or directory in the tree.
74-
// It is has the same behavior as [fs.WalkDir], but with paths as [string].
75-
func (f *testFsTrackingLibs) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
76-
return f.fs.WalkDir(root, walkFn)
77-
}
78-
79-
// Realpath returns the "real path" of the specified path,
80-
// following symlinks and correcting filename casing.
81-
func (f *testFsTrackingLibs) Realpath(path string) string {
82-
return f.fs.Realpath(path)
38+
return f.FS.Remove(path)
8339
}

internal/execute/testsys_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (s *testSys) FS() vfs.FS {
115115
}
116116

117117
func (s *testSys) TestFS() *incrementaltestutil.FsHandlingBuildInfo {
118-
return s.fs.fs.(*incrementaltestutil.FsHandlingBuildInfo)
118+
return s.fs.FS.(*incrementaltestutil.FsHandlingBuildInfo)
119119
}
120120

121121
func (s *testSys) ensureLibPathExists(path string) {
@@ -239,7 +239,7 @@ func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
239239
return nil
240240
}
241241

242-
newContents, ok := s.TestFS().FS().ReadFile(path)
242+
newContents, ok := s.TestFS().InnerReadFile(path)
243243
if !ok {
244244
return nil
245245
}
@@ -258,7 +258,7 @@ func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
258258
}
259259
if s.serializedDiff != nil {
260260
for path := range s.serializedDiff.snap {
261-
_, ok := s.TestFS().FS().ReadFile(path)
261+
_, ok := s.TestFS().InnerReadFile(path)
262262
if !ok {
263263
// report deleted
264264
s.reportFSEntryDiff(baseline, nil, path)

internal/testutil/incrementaltestutil/fs.go

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,21 @@ import (
1313
var fakeTsVersion = "FakeTSVersion"
1414

1515
type FsHandlingBuildInfo struct {
16-
fs vfs.FS
16+
vfs.FS
1717
}
1818

19-
var _ vfs.FS = (*FsHandlingBuildInfo)(nil)
20-
2119
func NewFsHandlingBuildInfo(fs vfs.FS) *FsHandlingBuildInfo {
22-
return &FsHandlingBuildInfo{
23-
fs: fs,
24-
}
20+
return &FsHandlingBuildInfo{FS: fs}
2521
}
2622

27-
func (f *FsHandlingBuildInfo) FS() vfs.FS {
28-
return f.fs
29-
}
30-
31-
func (f *FsHandlingBuildInfo) UseCaseSensitiveFileNames() bool {
32-
return f.fs.UseCaseSensitiveFileNames()
33-
}
34-
35-
// FileExists returns true if the file exists.
36-
func (f *FsHandlingBuildInfo) FileExists(path string) bool {
37-
return f.fs.FileExists(path)
23+
func (f *FsHandlingBuildInfo) InnerReadFile(path string) (contents string, ok bool) {
24+
return f.FS.ReadFile(path)
3825
}
3926

4027
// ReadFile reads the file specified by path and returns the content.
4128
// If the file fails to be read, ok will be false.
4229
func (f *FsHandlingBuildInfo) ReadFile(path string) (contents string, ok bool) {
43-
contents, ok = f.fs.ReadFile(path)
30+
contents, ok = f.InnerReadFile(path)
4431
if ok && tspath.FileExtensionIs(path, tspath.ExtensionTsBuildInfo) {
4532
// read buildinfo and modify version
4633
var buildInfo incremental.BuildInfo
@@ -71,42 +58,10 @@ func (f *FsHandlingBuildInfo) WriteFile(path string, data string, writeByteOrder
7158
data = string(newData)
7259
}
7360
// Write readable build info version
74-
if err := f.fs.WriteFile(path+".readable.baseline.txt", toReadableBuildInfo(&buildInfo, data), false); err != nil {
61+
if err := f.FS.WriteFile(path+".readable.baseline.txt", toReadableBuildInfo(&buildInfo, data), false); err != nil {
7562
return fmt.Errorf("testFs.WriteFile: failed to write readable build info: %w", err)
7663
}
7764
}
7865
}
79-
return f.fs.WriteFile(path, data, writeByteOrderMark)
80-
}
81-
82-
// Removes `path` and all its contents. Will return the first error it encounters.
83-
func (f *FsHandlingBuildInfo) Remove(path string) error {
84-
return f.fs.Remove(path)
85-
}
86-
87-
// DirectoryExists returns true if the path is a directory.
88-
func (f *FsHandlingBuildInfo) DirectoryExists(path string) bool {
89-
return f.fs.DirectoryExists(path)
90-
}
91-
92-
// GetAccessibleEntries returns the files/directories in the specified directory.
93-
// If any entry is a symlink, it will be followed.
94-
func (f *FsHandlingBuildInfo) GetAccessibleEntries(path string) vfs.Entries {
95-
return f.fs.GetAccessibleEntries(path)
96-
}
97-
98-
func (f *FsHandlingBuildInfo) Stat(path string) vfs.FileInfo {
99-
return f.fs.Stat(path)
100-
}
101-
102-
// WalkDir walks the file tree rooted at root, calling walkFn for each file or directory in the tree.
103-
// It is has the same behavior as [fs.WalkDir], but with paths as [string].
104-
func (f *FsHandlingBuildInfo) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
105-
return f.fs.WalkDir(root, walkFn)
106-
}
107-
108-
// Realpath returns the "real path" of the specified path,
109-
// following symlinks and correcting filename casing.
110-
func (f *FsHandlingBuildInfo) Realpath(path string) string {
111-
return f.fs.Realpath(path)
66+
return f.FS.WriteFile(path, data, writeByteOrderMark)
11267
}

0 commit comments

Comments
 (0)