Format split test files
Formats the recently split FSTestOSWrapper test files. Indentation
somehow got changed during the splitting process.
Bug: 436025865
Change-Id: If1c2b3338ae6f7af0b6ae9a0b5a572bdae16513a
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/276822
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Commit-Queue: Brian Sheedy <bsheedy@google.com>
diff --git a/tools/src/oswrapper/fstestoswrapper_common_test.go b/tools/src/oswrapper/fstestoswrapper_common_test.go
index 8de6041..4fd8de6 100644
--- a/tools/src/oswrapper/fstestoswrapper_common_test.go
+++ b/tools/src/oswrapper/fstestoswrapper_common_test.go
@@ -27,16 +27,16 @@
package oswrapper_test
import (
- "errors"
- "os"
- "path/filepath"
- "runtime"
- "syscall"
- "testing"
- "testing/fstest"
+ "errors"
+ "os"
+ "path/filepath"
+ "runtime"
+ "syscall"
+ "testing"
+ "testing/fstest"
- "dawn.googlesource.com/dawn/tools/src/oswrapper"
- "github.com/stretchr/testify/require"
+ "dawn.googlesource.com/dawn/tools/src/oswrapper"
+ "github.com/stretchr/testify/require"
)
// Common code used by tests for fstestoswrapper.go.
@@ -46,70 +46,70 @@
// stringPtr is a helper to get a pointer to a string, used so test cases can have nil to indicate a
// string value isn't set, instead of testing for "".
func stringPtr(s string) *string {
- return &s
+ return &s
}
// getTestRoot returns the standard root path for the current OS.
func getTestRoot() string {
- if runtime.GOOS == "windows" {
- return "C:\\"
- }
- return "/"
+ if runtime.GOOS == "windows" {
+ return "C:\\"
+ }
+ return "/"
}
// --- Unittest specific helpers ---
// unittestSetup is a helper for setting up the FSTestOSWrapper for an unittest.
type unittestSetup struct {
- initialFiles map[string]string
- initialDirs []string
- initialSymlinks map[string]string // map[linkPath]targetPath
+ initialFiles map[string]string
+ initialDirs []string
+ initialSymlinks map[string]string // map[linkPath]targetPath
}
// setup initializes the FSTestOSWrapper with the files and directories specified
// in the unittestSetup struct.
func (s unittestSetup) setup(t *testing.T) oswrapper.FSTestOSWrapper {
- t.Helper()
- testFS := oswrapper.CreateFSTestOSWrapper()
+ t.Helper()
+ testFS := oswrapper.CreateFSTestOSWrapper()
- for path, content := range s.initialFiles {
- require.NoError(t, testFS.MkdirAll(filepath.Dir(path), 0755))
- require.NoError(t, testFS.WriteFile(path, []byte(content), 0666))
- }
- for _, path := range s.initialDirs {
- require.NoError(t, testFS.MkdirAll(path, 0755))
- }
- for linkPath, targetPath := range s.initialSymlinks {
- require.NoError(t, testFS.MkdirAll(filepath.Dir(linkPath), 0755))
- require.NoError(t, testFS.Symlink(targetPath, linkPath))
- }
- return testFS
+ for path, content := range s.initialFiles {
+ require.NoError(t, testFS.MkdirAll(filepath.Dir(path), 0755))
+ require.NoError(t, testFS.WriteFile(path, []byte(content), 0666))
+ }
+ for _, path := range s.initialDirs {
+ require.NoError(t, testFS.MkdirAll(path, 0755))
+ }
+ for linkPath, targetPath := range s.initialSymlinks {
+ require.NoError(t, testFS.MkdirAll(filepath.Dir(linkPath), 0755))
+ require.NoError(t, testFS.Symlink(targetPath, linkPath))
+ }
+ return testFS
}
// expectedError is a helper struct for testing error conditions.
// Note: This is meant for usage in the non-*_MatchesReal unittests.
type expectedError struct {
- wantErrIs error
- wantErrMsg string
+ wantErrIs error
+ wantErrMsg string
}
// Check verifies that the given error `err` matches the expected error conditions.
// It returns true if an error was expected and found, indicating the test should stop.
func (e expectedError) Check(t *testing.T, err error) (stopTest bool) {
- t.Helper()
- if e.wantErrIs == nil && e.wantErrMsg == "" {
- require.NoError(t, err)
- return false
- }
+ t.Helper()
+ if e.wantErrIs == nil && e.wantErrMsg == "" {
+ require.NoError(t, err)
+ return false
+ }
- require.Error(t, err)
- if e.wantErrIs != nil {
- require.ErrorIs(t, err, e.wantErrIs)
- }
- if e.wantErrMsg != "" {
- require.ErrorContains(t, err, e.wantErrMsg)
- }
- return true
+ require.Error(t, err)
+ if e.wantErrIs != nil {
+ require.ErrorIs(t, err, e.wantErrIs)
+ }
+ if e.wantErrMsg != "" {
+ require.ErrorContains(t, err, e.wantErrMsg)
+ }
+ return true
}
// --- *_MatchesReal specific helpers ---
@@ -118,92 +118,92 @@
// matches against the real OS.
// Note: This is meant for usage in the *_MatchesReal tests.
type matchesRealSetup struct {
- unittestSetup
+ unittestSetup
}
// setup creates a temporary directory for the real OS, and initializes both the
// real and test filesystems with the files and directories specified in the
// matchesRealSetup struct.
func (s matchesRealSetup) setup(t *testing.T) (string, oswrapper.OSWrapper, oswrapper.FSTestOSWrapper) {
- t.Helper()
+ t.Helper()
- // Setup the test wrapper using the embedded setup method.
- testFS := s.unittestSetup.setup(t)
+ // Setup the test wrapper using the embedded setup method.
+ testFS := s.unittestSetup.setup(t)
- // Setup the real FS
- realRoot, err := os.MkdirTemp("", "real_fs_test_*")
- require.NoError(t, err)
- realFS := oswrapper.GetRealOSWrapper()
+ // Setup the real FS
+ realRoot, err := os.MkdirTemp("", "real_fs_test_*")
+ require.NoError(t, err)
+ realFS := oswrapper.GetRealOSWrapper()
- for path, content := range s.initialFiles {
- realPath := filepath.Join(realRoot, path)
- require.NoError(t, os.MkdirAll(filepath.Dir(realPath), 0755))
- require.NoError(t, os.WriteFile(realPath, []byte(content), 0666))
- }
- for _, path := range s.initialDirs {
- require.NoError(t, os.MkdirAll(filepath.Join(realRoot, path), 0755))
- }
- for linkPath, targetPath := range s.initialSymlinks {
- realPath := filepath.Join(realRoot, linkPath)
- require.NoError(t, os.MkdirAll(filepath.Dir(realPath), 0755))
- require.NoError(t, os.Symlink(targetPath, realPath))
- }
+ for path, content := range s.initialFiles {
+ realPath := filepath.Join(realRoot, path)
+ require.NoError(t, os.MkdirAll(filepath.Dir(realPath), 0755))
+ require.NoError(t, os.WriteFile(realPath, []byte(content), 0666))
+ }
+ for _, path := range s.initialDirs {
+ require.NoError(t, os.MkdirAll(filepath.Join(realRoot, path), 0755))
+ }
+ for linkPath, targetPath := range s.initialSymlinks {
+ realPath := filepath.Join(realRoot, linkPath)
+ require.NoError(t, os.MkdirAll(filepath.Dir(realPath), 0755))
+ require.NoError(t, os.Symlink(targetPath, realPath))
+ }
- return realRoot, realFS, testFS
+ return realRoot, realFS, testFS
}
// requireFileSystemsMatch walks the real filesystem at realRoot and compares its
// structure and file content against the provided FSTestOSWrapper.
// Note: This is meant for usage in *_MatchesReal tests.
func requireFileSystemsMatch(t *testing.T, realRoot string, testFS oswrapper.FSTestOSWrapper) {
- t.Helper()
+ t.Helper()
- realMap := make(map[string]*fstest.MapFile)
- // We use filepath.Walk which uses Lstat, so we see symlinks.
- err := filepath.Walk(realRoot, func(path string, info os.FileInfo, err error) error {
- require.NoError(t, err)
- if path == realRoot {
- return nil
- }
+ realMap := make(map[string]*fstest.MapFile)
+ // We use filepath.Walk which uses Lstat, so we see symlinks.
+ err := filepath.Walk(realRoot, func(path string, info os.FileInfo, err error) error {
+ require.NoError(t, err)
+ if path == realRoot {
+ return nil
+ }
- relPath, err := filepath.Rel(realRoot, path)
- require.NoError(t, err)
+ relPath, err := filepath.Rel(realRoot, path)
+ require.NoError(t, err)
- // Use the same path cleaning logic as FSTestOSWrapper.
- mapKey := testFS.FSTestFilesystemReaderWriter.CleanPath(relPath)
+ // Use the same path cleaning logic as FSTestOSWrapper.
+ mapKey := testFS.FSTestFilesystemReaderWriter.CleanPath(relPath)
- mapFile := &fstest.MapFile{Mode: info.Mode()}
- if info.Mode()&os.ModeSymlink != 0 {
- target, err := os.Readlink(path)
- require.NoError(t, err)
- mapFile.Data = []byte(target)
- } else if !info.IsDir() {
- data, err := os.ReadFile(path)
- require.NoError(t, err)
- mapFile.Data = data
- }
- realMap[mapKey] = mapFile
- return nil
- })
- require.NoError(t, err)
+ mapFile := &fstest.MapFile{Mode: info.Mode()}
+ if info.Mode()&os.ModeSymlink != 0 {
+ target, err := os.Readlink(path)
+ require.NoError(t, err)
+ mapFile.Data = []byte(target)
+ } else if !info.IsDir() {
+ data, err := os.ReadFile(path)
+ require.NoError(t, err)
+ mapFile.Data = data
+ }
+ realMap[mapKey] = mapFile
+ return nil
+ })
+ require.NoError(t, err)
- testMap := testFS.FS
- require.Len(t, testMap, len(realMap), "Filesystems have a different number of entries")
+ testMap := testFS.FS
+ require.Len(t, testMap, len(realMap), "Filesystems have a different number of entries")
- for key, realFile := range realMap {
- testFile, ok := testMap[key]
- require.True(t, ok, "Path '%s' exists in real FS but not in FS under test", key)
+ for key, realFile := range realMap {
+ testFile, ok := testMap[key]
+ require.True(t, ok, "Path '%s' exists in real FS but not in FS under test", key)
- // Compare file modes. Note: fs.FS doesn't strictly guarantee strict equality of all bits,
- // but for our purposes (Dir, Symlink, Perms) they should match.
- // However, fstest.MapFS might normalize modes.
- require.Equal(t, realFile.Mode.IsDir(), testFile.Mode.IsDir(), "IsDir mismatch for '%s'", key)
- require.Equal(t, realFile.Mode&os.ModeSymlink, testFile.Mode&os.ModeSymlink, "IsSymlink mismatch for '%s'", key)
+ // Compare file modes. Note: fs.FS doesn't strictly guarantee strict equality of all bits,
+ // but for our purposes (Dir, Symlink, Perms) they should match.
+ // However, fstest.MapFS might normalize modes.
+ require.Equal(t, realFile.Mode.IsDir(), testFile.Mode.IsDir(), "IsDir mismatch for '%s'", key)
+ require.Equal(t, realFile.Mode&os.ModeSymlink, testFile.Mode&os.ModeSymlink, "IsSymlink mismatch for '%s'", key)
- if !realFile.Mode.IsDir() {
- require.Equal(t, realFile.Data, testFile.Data, "Content/Target mismatch for '%s'", key)
- }
- }
+ if !realFile.Mode.IsDir() {
+ require.Equal(t, realFile.Data, testFile.Data, "Content/Target mismatch for '%s'", key)
+ }
+ }
}
// requireErrorsMatch asserts that the real and test errors are compatible.
@@ -211,25 +211,25 @@
// must be the same. Otherwise, it just checks that both are non-nil.
// Note: This is meant for usage in *_MatchesReal tests.
func requireErrorsMatch(t *testing.T, realErr, testErr error) {
- t.Helper()
- if realErr != nil {
- require.Error(t, testErr, "Real FS errored but FS under test did not.\nReal error: %v", realErr)
+ t.Helper()
+ if realErr != nil {
+ require.Error(t, testErr, "Real FS errored but FS under test did not.\nReal error: %v", realErr)
- // For certain well-defined errors, we expect the test wrapper to return
- // the exact same error type.
- if errors.Is(realErr, os.ErrNotExist) {
- require.ErrorIs(t, testErr, os.ErrNotExist, "Real error is os.ErrNotExist, but test error is not")
- } else if errors.Is(realErr, os.ErrExist) {
- require.ErrorIs(t, testErr, os.ErrExist, "Real error is os.ErrExist, but test error is not")
- } else if errors.Is(realErr, syscall.ENOTEMPTY) {
- require.ErrorIs(t, testErr, syscall.ENOTEMPTY, "Real error is syscall.ENOTEMPTY, but test error is not")
- } else if errors.Is(realErr, os.ErrClosed) {
- require.ErrorIs(t, testErr, os.ErrClosed, "Real error is os.ErrClosed, but test error is not")
- }
- // For other errors (e.g., 'is a directory', 'directory not empty'),
- // the exact error message can be OS-dependent. In these cases, just
- // checking that *an* error occurred is sufficient.
- } else {
- require.NoError(t, testErr, "FS under test errored but Real FS did not.\nTest error: %v", testErr)
- }
-}
\ No newline at end of file
+ // For certain well-defined errors, we expect the test wrapper to return
+ // the exact same error type.
+ if errors.Is(realErr, os.ErrNotExist) {
+ require.ErrorIs(t, testErr, os.ErrNotExist, "Real error is os.ErrNotExist, but test error is not")
+ } else if errors.Is(realErr, os.ErrExist) {
+ require.ErrorIs(t, testErr, os.ErrExist, "Real error is os.ErrExist, but test error is not")
+ } else if errors.Is(realErr, syscall.ENOTEMPTY) {
+ require.ErrorIs(t, testErr, syscall.ENOTEMPTY, "Real error is syscall.ENOTEMPTY, but test error is not")
+ } else if errors.Is(realErr, os.ErrClosed) {
+ require.ErrorIs(t, testErr, os.ErrClosed, "Real error is os.ErrClosed, but test error is not")
+ }
+ // For other errors (e.g., 'is a directory', 'directory not empty'),
+ // the exact error message can be OS-dependent. In these cases, just
+ // checking that *an* error occurred is sufficient.
+ } else {
+ require.NoError(t, testErr, "FS under test errored but Real FS did not.\nTest error: %v", testErr)
+ }
+}
diff --git a/tools/src/oswrapper/fstestoswrapper_environ_test.go b/tools/src/oswrapper/fstestoswrapper_environ_test.go
index 42adeae..5ce7e2f 100644
--- a/tools/src/oswrapper/fstestoswrapper_environ_test.go
+++ b/tools/src/oswrapper/fstestoswrapper_environ_test.go
@@ -27,12 +27,12 @@
package oswrapper_test
import (
- "os"
- "runtime"
- "testing"
+ "os"
+ "runtime"
+ "testing"
- "dawn.googlesource.com/dawn/tools/src/oswrapper"
- "github.com/stretchr/testify/require"
+ "dawn.googlesource.com/dawn/tools/src/oswrapper"
+ "github.com/stretchr/testify/require"
)
// Tests for the environ-related components of FSTestOSWrapper.
@@ -44,49 +44,49 @@
// defined expectations.
func TestCreateFSTestOSWrapperWithRealEnv_MatchesReal(t *testing.T) {
- testWrapper := oswrapper.CreateFSTestOSWrapperWithRealEnv()
+ testWrapper := oswrapper.CreateFSTestOSWrapperWithRealEnv()
- for key, testVal := range testWrapper.EnvMap() {
- if key == "PWD" || key == "HOME" {
- // PWD and HOME are handled below
- continue
- }
+ for key, testVal := range testWrapper.EnvMap() {
+ if key == "PWD" || key == "HOME" {
+ // PWD and HOME are handled below
+ continue
+ }
- realVal := os.Getenv(key)
- require.Equal(t, realVal, testVal, "environment variable '%s' should match real env", key)
- }
+ realVal := os.Getenv(key)
+ require.Equal(t, realVal, testVal, "environment variable '%s' should match real env", key)
+ }
- // PWD and HOME may not be set in the real environment, but FSTestEnvironProvider sets them
- // based on the appropriate os calls.
- realWd, realWdErr := os.Getwd()
- if realWdErr == nil {
- testWd, testWdErr := testWrapper.Getwd()
- require.NoError(t, testWdErr, "Getwd() should not fail if real Getwd() succeeds")
- require.Equal(t, realWd, testWd, "PWD should match real working directory")
- }
+ // PWD and HOME may not be set in the real environment, but FSTestEnvironProvider sets them
+ // based on the appropriate os calls.
+ realWd, realWdErr := os.Getwd()
+ if realWdErr == nil {
+ testWd, testWdErr := testWrapper.Getwd()
+ require.NoError(t, testWdErr, "Getwd() should not fail if real Getwd() succeeds")
+ require.Equal(t, realWd, testWd, "PWD should match real working directory")
+ }
- realHome, realHomeErr := os.UserHomeDir()
- if realHomeErr == nil {
- testHome, testHomeErr := testWrapper.UserHomeDir()
- require.NoError(t, testHomeErr, "UserHomeDir() should not fail if real UserHomeDir() succeeds")
- require.Equal(t, realHome, testHome, "HOME should match real user home directory")
- }
+ realHome, realHomeErr := os.UserHomeDir()
+ if realHomeErr == nil {
+ testHome, testHomeErr := testWrapper.UserHomeDir()
+ require.NoError(t, testHomeErr, "UserHomeDir() should not fail if real UserHomeDir() succeeds")
+ require.Equal(t, realHome, testHome, "HOME should match real user home directory")
+ }
}
func TestFSTestEnvironProvider_Environ(t *testing.T) {
- t.Run("Empty environment", func(t *testing.T) {
- wrapper := oswrapper.CreateFSTestOSWrapper()
- env := wrapper.Environ()
- require.Empty(t, env)
- })
+ t.Run("Empty environment", func(t *testing.T) {
+ wrapper := oswrapper.CreateFSTestOSWrapper()
+ env := wrapper.Environ()
+ require.Empty(t, env)
+ })
- t.Run("With variables", func(t *testing.T) {
- wrapper := oswrapper.CreateFSTestOSWrapper()
- wrapper.Setenv("FOO", "bar")
- wrapper.Setenv("BAZ", "qux")
- env := wrapper.Environ()
- require.ElementsMatch(t, []string{"FOO=bar", "BAZ=qux"}, env)
- })
+ t.Run("With variables", func(t *testing.T) {
+ wrapper := oswrapper.CreateFSTestOSWrapper()
+ wrapper.Setenv("FOO", "bar")
+ wrapper.Setenv("BAZ", "qux")
+ env := wrapper.Environ()
+ require.ElementsMatch(t, []string{"FOO=bar", "BAZ=qux"}, env)
+ })
}
// TestFSTestEnvironProvider_Environ_MatchesReal is not implemented, since the test version inserts
@@ -96,119 +96,119 @@
// TestCreateFSTestOSWrapperWithRealEnv_MatchesReal
func TestFSTestEnvironProvider_Getenv(t *testing.T) {
- wrapper := oswrapper.CreateFSTestOSWrapper()
- wrapper.Setenv("EXISTING_VAR", "value")
+ wrapper := oswrapper.CreateFSTestOSWrapper()
+ wrapper.Setenv("EXISTING_VAR", "value")
- t.Run("Get existing variable", func(t *testing.T) {
- val := wrapper.Getenv("EXISTING_VAR")
- require.Equal(t, "value", val)
- })
+ t.Run("Get existing variable", func(t *testing.T) {
+ val := wrapper.Getenv("EXISTING_VAR")
+ require.Equal(t, "value", val)
+ })
- t.Run("Get non-existent variable", func(t *testing.T) {
- val := wrapper.Getenv("NON_EXISTENT_VAR")
- require.Empty(t, val)
- })
+ t.Run("Get non-existent variable", func(t *testing.T) {
+ val := wrapper.Getenv("NON_EXISTENT_VAR")
+ require.Empty(t, val)
+ })
}
func TestFSTestEnvironProvider_Getenv_MatchesReal(t *testing.T) {
- realWrapper := oswrapper.GetRealOSWrapper()
- testWrapper := oswrapper.CreateFSTestOSWrapperWithRealEnv()
+ realWrapper := oswrapper.GetRealOSWrapper()
+ testWrapper := oswrapper.CreateFSTestOSWrapperWithRealEnv()
- t.Run("existing variable", func(t *testing.T) {
- // Test a variable that is likely to exist
- var existingVar string
- if runtime.GOOS == "windows" {
- existingVar = "SystemRoot"
- } else {
- existingVar = "PATH"
- }
+ t.Run("existing variable", func(t *testing.T) {
+ // Test a variable that is likely to exist
+ var existingVar string
+ if runtime.GOOS == "windows" {
+ existingVar = "SystemRoot"
+ } else {
+ existingVar = "PATH"
+ }
- realVal, realExists := os.LookupEnv(existingVar)
- if !realExists {
- t.Skipf("Skipping test as environment variable '%s' is not set", existingVar)
- }
+ realVal, realExists := os.LookupEnv(existingVar)
+ if !realExists {
+ t.Skipf("Skipping test as environment variable '%s' is not set", existingVar)
+ }
- testVal := testWrapper.Getenv(existingVar)
- require.Equal(t, realVal, testVal, "Value for existing var '%s' should match", existingVar)
- require.NotEmpty(t, realVal, "Expected existing var '%s' to be non-empty", existingVar)
- })
+ testVal := testWrapper.Getenv(existingVar)
+ require.Equal(t, realVal, testVal, "Value for existing var '%s' should match", existingVar)
+ require.NotEmpty(t, realVal, "Expected existing var '%s' to be non-empty", existingVar)
+ })
- t.Run("non-existent variable", func(t *testing.T) {
- // Test a variable that is unlikely to exist
- nonExistentVar := "THIS_VAR_SHOULD_NOT_EXIST_IN_TEST_ENV"
- if _, realExists := os.LookupEnv(nonExistentVar); realExists {
- t.Skipf("Skipping test as environment variable '%s' is unexpectedly set", nonExistentVar)
- }
+ t.Run("non-existent variable", func(t *testing.T) {
+ // Test a variable that is unlikely to exist
+ nonExistentVar := "THIS_VAR_SHOULD_NOT_EXIST_IN_TEST_ENV"
+ if _, realExists := os.LookupEnv(nonExistentVar); realExists {
+ t.Skipf("Skipping test as environment variable '%s' is unexpectedly set", nonExistentVar)
+ }
- realVal := realWrapper.Getenv(nonExistentVar)
- testVal := testWrapper.Getenv(nonExistentVar)
- require.Equal(t, realVal, testVal, "Value for non-existent var should match")
- require.Empty(t, realVal, "Expected non-existent var to be empty")
- })
+ realVal := realWrapper.Getenv(nonExistentVar)
+ testVal := testWrapper.Getenv(nonExistentVar)
+ require.Equal(t, realVal, testVal, "Value for non-existent var should match")
+ require.Empty(t, realVal, "Expected non-existent var to be empty")
+ })
}
func TestFSTestEnvironProvider_Getwd(t *testing.T) {
- t.Run("PWD is set", func(t *testing.T) {
- wrapper := oswrapper.CreateFSTestOSWrapper()
- wrapper.Setenv("PWD", "/my/test/dir")
- wd, err := wrapper.Getwd()
- require.NoError(t, err)
- require.Equal(t, "/my/test/dir", wd)
- })
+ t.Run("PWD is set", func(t *testing.T) {
+ wrapper := oswrapper.CreateFSTestOSWrapper()
+ wrapper.Setenv("PWD", "/my/test/dir")
+ wd, err := wrapper.Getwd()
+ require.NoError(t, err)
+ require.Equal(t, "/my/test/dir", wd)
+ })
- t.Run("PWD is not set", func(t *testing.T) {
- wrapper := oswrapper.CreateFSTestOSWrapper()
- _, err := wrapper.Getwd()
- require.Error(t, err)
- require.ErrorIs(t, err, oswrapper.ErrPwdNotSet)
- })
+ t.Run("PWD is not set", func(t *testing.T) {
+ wrapper := oswrapper.CreateFSTestOSWrapper()
+ _, err := wrapper.Getwd()
+ require.Error(t, err)
+ require.ErrorIs(t, err, oswrapper.ErrPwdNotSet)
+ })
}
func TestFSTestEnvironProvider_Getwd_MatchesReal(t *testing.T) {
- realWrapper := oswrapper.GetRealOSWrapper()
- testWrapper := oswrapper.CreateFSTestOSWrapperWithRealEnv()
+ realWrapper := oswrapper.GetRealOSWrapper()
+ testWrapper := oswrapper.CreateFSTestOSWrapperWithRealEnv()
- realWd, realErr := realWrapper.Getwd()
- testWd, testErr := testWrapper.Getwd()
+ realWd, realErr := realWrapper.Getwd()
+ testWd, testErr := testWrapper.Getwd()
- // os.Getwd() can fail in some environments
- if realErr != nil {
- require.Error(t, testErr)
- } else {
- require.NoError(t, testErr)
- require.Equal(t, realWd, testWd)
- }
+ // os.Getwd() can fail in some environments
+ if realErr != nil {
+ require.Error(t, testErr)
+ } else {
+ require.NoError(t, testErr)
+ require.Equal(t, realWd, testWd)
+ }
}
func TestFSTestEnvironProvider_UserHomeDir(t *testing.T) {
- t.Run("HOME is set", func(t *testing.T) {
- wrapper := oswrapper.CreateFSTestOSWrapper()
- wrapper.Setenv("HOME", "/home/user")
- home, err := wrapper.UserHomeDir()
- require.NoError(t, err)
- require.Equal(t, "/home/user", home)
- })
+ t.Run("HOME is set", func(t *testing.T) {
+ wrapper := oswrapper.CreateFSTestOSWrapper()
+ wrapper.Setenv("HOME", "/home/user")
+ home, err := wrapper.UserHomeDir()
+ require.NoError(t, err)
+ require.Equal(t, "/home/user", home)
+ })
- t.Run("HOME is not set", func(t *testing.T) {
- wrapper := oswrapper.CreateFSTestOSWrapper()
- _, err := wrapper.UserHomeDir()
- require.Error(t, err)
- require.ErrorIs(t, err, oswrapper.ErrHomeNotSet)
- })
+ t.Run("HOME is not set", func(t *testing.T) {
+ wrapper := oswrapper.CreateFSTestOSWrapper()
+ _, err := wrapper.UserHomeDir()
+ require.Error(t, err)
+ require.ErrorIs(t, err, oswrapper.ErrHomeNotSet)
+ })
}
func TestFSTestEnvironProvider_UserHomeDir_MatchesReal(t *testing.T) {
- realWrapper := oswrapper.GetRealOSWrapper()
- testWrapper := oswrapper.CreateFSTestOSWrapperWithRealEnv()
+ realWrapper := oswrapper.GetRealOSWrapper()
+ testWrapper := oswrapper.CreateFSTestOSWrapperWithRealEnv()
- realHome, realErr := realWrapper.UserHomeDir()
- testHome, testErr := testWrapper.UserHomeDir()
+ realHome, realErr := realWrapper.UserHomeDir()
+ testHome, testErr := testWrapper.UserHomeDir()
- // os.UserHomeDir can fail in some environments
- if realErr != nil {
- require.Error(t, testErr)
- } else {
- require.NoError(t, testErr)
- require.Equal(t, realHome, testHome)
- }
-}
\ No newline at end of file
+ // os.UserHomeDir can fail in some environments
+ if realErr != nil {
+ require.Error(t, testErr)
+ } else {
+ require.NoError(t, testErr)
+ require.Equal(t, realHome, testHome)
+ }
+}
diff --git a/tools/src/oswrapper/fstestoswrapper_open_test.go b/tools/src/oswrapper/fstestoswrapper_open_test.go
index da13575..61fe7e8 100644
--- a/tools/src/oswrapper/fstestoswrapper_open_test.go
+++ b/tools/src/oswrapper/fstestoswrapper_open_test.go
@@ -27,13 +27,13 @@
package oswrapper_test
import (
- "io"
- "os"
- "path/filepath"
- "syscall"
- "testing"
+ "io"
+ "os"
+ "path/filepath"
+ "syscall"
+ "testing"
- "github.com/stretchr/testify/require"
+ "github.com/stretchr/testify/require"
)
// Tests for the Open() function in FSTestOSWrapper.
@@ -45,229 +45,229 @@
// defined expectations.
func TestFSTestOSWrapper_Open(t *testing.T) {
- root := getTestRoot()
- tests := []struct {
- name string
- setup unittestSetup
- path string
- expectedContent *string
- expectedError
- }{
- {
- name: "Open existing file",
- path: filepath.Join(root, "file.txt"),
- setup: unittestSetup{
- initialFiles: map[string]string{
- filepath.Join(root, "file.txt"): "hello world",
- },
- },
- expectedContent: stringPtr("hello world"),
- },
- {
- name: "Open non-existent file",
- path: filepath.Join(root, "nonexistent.txt"),
- expectedError: expectedError{
- wantErrIs: os.ErrNotExist,
- },
- },
- {
- name: "Open a directory",
- path: filepath.Join(root, "mydir"),
- setup: unittestSetup{
- initialDirs: []string{filepath.Join(root, "mydir")},
- },
- },
- {
- name: "Parent path is a file",
- path: filepath.Join(root, "file.txt", "another.txt"),
- setup: unittestSetup{
- initialFiles: map[string]string{
- filepath.Join(root, "file.txt"): "i am a file",
- },
- },
- expectedError: expectedError{
- wantErrMsg: "not a directory",
- },
- },
- {
- name: "Open symlink to file",
- path: filepath.Join(root, "link_to_file"),
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "symlink content"},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_file"): "file.txt",
- },
- },
- expectedContent: stringPtr("symlink content"),
- },
- {
- name: "Open symlink to directory",
- path: filepath.Join(root, "link_to_dir"),
- setup: unittestSetup{
- initialDirs: []string{filepath.Join(root, "dir")},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_dir"): "dir",
- },
- },
- },
- {
- name: "Open broken symlink",
- path: filepath.Join(root, "broken_link"),
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "broken_link"): "nonexistent",
- },
- },
- expectedError: expectedError{
- wantErrIs: os.ErrNotExist,
- },
- },
- {
- name: "Open symlink loop",
- path: filepath.Join(root, "loop1"),
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "loop1"): "loop2",
- filepath.Join(root, "loop2"): "loop1",
- },
- },
- expectedError: expectedError{
- wantErrIs: syscall.ELOOP,
- },
- },
- }
+ root := getTestRoot()
+ tests := []struct {
+ name string
+ setup unittestSetup
+ path string
+ expectedContent *string
+ expectedError
+ }{
+ {
+ name: "Open existing file",
+ path: filepath.Join(root, "file.txt"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{
+ filepath.Join(root, "file.txt"): "hello world",
+ },
+ },
+ expectedContent: stringPtr("hello world"),
+ },
+ {
+ name: "Open non-existent file",
+ path: filepath.Join(root, "nonexistent.txt"),
+ expectedError: expectedError{
+ wantErrIs: os.ErrNotExist,
+ },
+ },
+ {
+ name: "Open a directory",
+ path: filepath.Join(root, "mydir"),
+ setup: unittestSetup{
+ initialDirs: []string{filepath.Join(root, "mydir")},
+ },
+ },
+ {
+ name: "Parent path is a file",
+ path: filepath.Join(root, "file.txt", "another.txt"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{
+ filepath.Join(root, "file.txt"): "i am a file",
+ },
+ },
+ expectedError: expectedError{
+ wantErrMsg: "not a directory",
+ },
+ },
+ {
+ name: "Open symlink to file",
+ path: filepath.Join(root, "link_to_file"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "symlink content"},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_file"): "file.txt",
+ },
+ },
+ expectedContent: stringPtr("symlink content"),
+ },
+ {
+ name: "Open symlink to directory",
+ path: filepath.Join(root, "link_to_dir"),
+ setup: unittestSetup{
+ initialDirs: []string{filepath.Join(root, "dir")},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_dir"): "dir",
+ },
+ },
+ },
+ {
+ name: "Open broken symlink",
+ path: filepath.Join(root, "broken_link"),
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "broken_link"): "nonexistent",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: os.ErrNotExist,
+ },
+ },
+ {
+ name: "Open symlink loop",
+ path: filepath.Join(root, "loop1"),
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "loop1"): "loop2",
+ filepath.Join(root, "loop2"): "loop1",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: syscall.ELOOP,
+ },
+ },
+ }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- wrapper := tc.setup.setup(t)
- file, err := wrapper.Open(tc.path)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ wrapper := tc.setup.setup(t)
+ file, err := wrapper.Open(tc.path)
- if tc.expectedError.Check(t, err) {
- return
- }
+ if tc.expectedError.Check(t, err) {
+ return
+ }
- require.NotNil(t, file)
- defer file.Close()
+ require.NotNil(t, file)
+ defer file.Close()
- if tc.expectedContent != nil {
- content, err := io.ReadAll(file)
- require.NoError(t, err)
- require.Equal(t, *tc.expectedContent, string(content))
- }
- })
- }
+ if tc.expectedContent != nil {
+ content, err := io.ReadAll(file)
+ require.NoError(t, err)
+ require.Equal(t, *tc.expectedContent, string(content))
+ }
+ })
+ }
}
func TestFSTestOSWrapper_Open_MatchesReal(t *testing.T) {
- tests := []struct {
- name string
- setup matchesRealSetup
- path string // path to Open
- }{
- {
- name: "Open existing file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "hello world",
- },
- }},
- path: "file.txt",
- },
- {
- name: "Error on non-existent file",
- path: "nonexistent.txt",
- },
- {
- name: "Open a directory",
- setup: matchesRealSetup{unittestSetup{
- initialDirs: []string{
- "mydir",
- },
- }},
- path: "mydir",
- },
- {
- name: "Error on parent path is a file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "i am a file",
- },
- }},
- path: filepath.Join("file.txt", "another.txt"),
- },
- {
- name: "Open symlink to file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{"file.txt": "symlink content"},
- initialSymlinks: map[string]string{
- "link_to_file": "file.txt",
- },
- }},
- path: "link_to_file",
- },
- {
- name: "Open symlink to directory",
- setup: matchesRealSetup{unittestSetup{
- initialDirs: []string{"dir"},
- initialSymlinks: map[string]string{
- "link_to_dir": "dir",
- },
- }},
- path: "link_to_dir",
- },
- {
- name: "Open broken symlink",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "broken_link": "nonexistent",
- },
- }},
- path: "broken_link",
- },
- {
- name: "Open symlink loop",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "loop1": "loop2",
- "loop2": "loop1",
- },
- }},
- path: "loop1",
- },
- }
+ tests := []struct {
+ name string
+ setup matchesRealSetup
+ path string // path to Open
+ }{
+ {
+ name: "Open existing file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "hello world",
+ },
+ }},
+ path: "file.txt",
+ },
+ {
+ name: "Error on non-existent file",
+ path: "nonexistent.txt",
+ },
+ {
+ name: "Open a directory",
+ setup: matchesRealSetup{unittestSetup{
+ initialDirs: []string{
+ "mydir",
+ },
+ }},
+ path: "mydir",
+ },
+ {
+ name: "Error on parent path is a file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "i am a file",
+ },
+ }},
+ path: filepath.Join("file.txt", "another.txt"),
+ },
+ {
+ name: "Open symlink to file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{"file.txt": "symlink content"},
+ initialSymlinks: map[string]string{
+ "link_to_file": "file.txt",
+ },
+ }},
+ path: "link_to_file",
+ },
+ {
+ name: "Open symlink to directory",
+ setup: matchesRealSetup{unittestSetup{
+ initialDirs: []string{"dir"},
+ initialSymlinks: map[string]string{
+ "link_to_dir": "dir",
+ },
+ }},
+ path: "link_to_dir",
+ },
+ {
+ name: "Open broken symlink",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "broken_link": "nonexistent",
+ },
+ }},
+ path: "broken_link",
+ },
+ {
+ name: "Open symlink loop",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "loop1": "loop2",
+ "loop2": "loop1",
+ },
+ }},
+ path: "loop1",
+ },
+ }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- realRoot, realFS, testFS := tc.setup.setup(t)
- defer os.RemoveAll(realRoot)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ realRoot, realFS, testFS := tc.setup.setup(t)
+ defer os.RemoveAll(realRoot)
- realFile, realErr := realFS.Open(filepath.Join(realRoot, tc.path))
- if realErr == nil {
- defer realFile.Close()
- }
- testFile, testErr := testFS.Open(tc.path)
- if testErr == nil {
- defer testFile.Close()
- }
+ realFile, realErr := realFS.Open(filepath.Join(realRoot, tc.path))
+ if realErr == nil {
+ defer realFile.Close()
+ }
+ testFile, testErr := testFS.Open(tc.path)
+ if testErr == nil {
+ defer testFile.Close()
+ }
- requireErrorsMatch(t, realErr, testErr)
+ requireErrorsMatch(t, realErr, testErr)
- if realErr == nil {
- realInfo, err := realFile.Stat()
- require.NoError(t, err)
- testInfo, err := testFile.Stat()
- require.NoError(t, err)
+ if realErr == nil {
+ realInfo, err := realFile.Stat()
+ require.NoError(t, err)
+ testInfo, err := testFile.Stat()
+ require.NoError(t, err)
- require.Equal(t, realInfo.IsDir(), testInfo.IsDir(), "IsDir mismatch for opened path")
+ require.Equal(t, realInfo.IsDir(), testInfo.IsDir(), "IsDir mismatch for opened path")
- if !realInfo.IsDir() {
- realContent, err := io.ReadAll(realFile)
- require.NoError(t, err)
- testContent, err := io.ReadAll(testFile)
- require.NoError(t, err)
- require.Equal(t, realContent, testContent)
- }
- }
- })
- }
+ if !realInfo.IsDir() {
+ realContent, err := io.ReadAll(realFile)
+ require.NoError(t, err)
+ testContent, err := io.ReadAll(testFile)
+ require.NoError(t, err)
+ require.Equal(t, realContent, testContent)
+ }
+ }
+ })
+ }
}
diff --git a/tools/src/oswrapper/fstestoswrapper_openfile_test.go b/tools/src/oswrapper/fstestoswrapper_openfile_test.go
index d9d6da9..495a73b 100644
--- a/tools/src/oswrapper/fstestoswrapper_openfile_test.go
+++ b/tools/src/oswrapper/fstestoswrapper_openfile_test.go
@@ -27,14 +27,14 @@
package oswrapper_test
import (
- "io"
- "os"
- "path/filepath"
- "syscall"
- "testing"
+ "io"
+ "os"
+ "path/filepath"
+ "syscall"
+ "testing"
- "dawn.googlesource.com/dawn/tools/src/oswrapper"
- "github.com/stretchr/testify/require"
+ "dawn.googlesource.com/dawn/tools/src/oswrapper"
+ "github.com/stretchr/testify/require"
)
// Tests for the OpenFile() function in FSTestOSWrapper.
@@ -46,532 +46,532 @@
// defined expectations.
func TestFSTestOSWrapper_OpenFile(t *testing.T) {
- root := getTestRoot()
- tests := []struct {
- name string
- setup unittestSetup
- path string
- flag int
- action func(t *testing.T, file oswrapper.File) // Action to perform on the opened file
- expectedContent *string // Expected content of the file *after* the action
- expectedError
- }{
- {
- name: "O_RDONLY - Read existing file",
- path: filepath.Join(root, "file.txt"),
- flag: os.O_RDONLY,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "read me"},
- },
- action: func(t *testing.T, file oswrapper.File) {
- content, err := io.ReadAll(file)
- require.NoError(t, err)
- require.Equal(t, "read me", string(content))
- },
- },
- {
- name: "O_RDONLY - Error on write",
- path: filepath.Join(root, "file.txt"),
- flag: os.O_RDONLY,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "read only"},
- },
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("fail"))
- require.Error(t, err)
- },
- expectedContent: stringPtr("read only"),
- },
- {
- name: "O_WRONLY - Write to existing file",
- path: filepath.Join(root, "file.txt"),
- flag: os.O_WRONLY,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "initial"},
- },
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("new"))
- require.NoError(t, err)
- },
- expectedContent: stringPtr("newtial"),
- },
- {
- name: "O_WRONLY - Error on read",
- path: filepath.Join(root, "file.txt"),
- flag: os.O_WRONLY,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "write only"},
- },
- action: func(t *testing.T, file oswrapper.File) {
- _, err := io.ReadAll(file)
- require.Error(t, err)
- },
- expectedContent: stringPtr("write only"),
- },
- {
- name: "O_RDWR - Read and Write",
- path: filepath.Join(root, "file.txt"),
- flag: os.O_RDWR,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "start"},
- },
- action: func(t *testing.T, file oswrapper.File) {
- // This should not advance the write offset
- content, err := io.ReadAll(file)
- require.NoError(t, err)
- require.Equal(t, "start", string(content))
+ root := getTestRoot()
+ tests := []struct {
+ name string
+ setup unittestSetup
+ path string
+ flag int
+ action func(t *testing.T, file oswrapper.File) // Action to perform on the opened file
+ expectedContent *string // Expected content of the file *after* the action
+ expectedError
+ }{
+ {
+ name: "O_RDONLY - Read existing file",
+ path: filepath.Join(root, "file.txt"),
+ flag: os.O_RDONLY,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "read me"},
+ },
+ action: func(t *testing.T, file oswrapper.File) {
+ content, err := io.ReadAll(file)
+ require.NoError(t, err)
+ require.Equal(t, "read me", string(content))
+ },
+ },
+ {
+ name: "O_RDONLY - Error on write",
+ path: filepath.Join(root, "file.txt"),
+ flag: os.O_RDONLY,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "read only"},
+ },
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("fail"))
+ require.Error(t, err)
+ },
+ expectedContent: stringPtr("read only"),
+ },
+ {
+ name: "O_WRONLY - Write to existing file",
+ path: filepath.Join(root, "file.txt"),
+ flag: os.O_WRONLY,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "initial"},
+ },
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("new"))
+ require.NoError(t, err)
+ },
+ expectedContent: stringPtr("newtial"),
+ },
+ {
+ name: "O_WRONLY - Error on read",
+ path: filepath.Join(root, "file.txt"),
+ flag: os.O_WRONLY,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "write only"},
+ },
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := io.ReadAll(file)
+ require.Error(t, err)
+ },
+ expectedContent: stringPtr("write only"),
+ },
+ {
+ name: "O_RDWR - Read and Write",
+ path: filepath.Join(root, "file.txt"),
+ flag: os.O_RDWR,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "start"},
+ },
+ action: func(t *testing.T, file oswrapper.File) {
+ // This should not advance the write offset
+ content, err := io.ReadAll(file)
+ require.NoError(t, err)
+ require.Equal(t, "start", string(content))
- // Seek to the beginning to overwrite
- _, err = file.Seek(0, io.SeekStart)
- require.NoError(t, err)
+ // Seek to the beginning to overwrite
+ _, err = file.Seek(0, io.SeekStart)
+ require.NoError(t, err)
- _, err = file.Write([]byte("finish"))
- require.NoError(t, err)
- },
- expectedContent: stringPtr("finish"),
- },
- {
- name: "O_APPEND - Append to file",
- path: filepath.Join(root, "file.txt"),
- flag: os.O_WRONLY | os.O_APPEND,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "first,"},
- },
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("second"))
- require.NoError(t, err)
- },
- expectedContent: stringPtr("first,second"),
- },
- {
- name: "O_CREATE - Create new file",
- path: filepath.Join(root, "newfile.txt"),
- flag: os.O_WRONLY | os.O_CREATE,
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("created"))
- require.NoError(t, err)
- },
- expectedContent: stringPtr("created"),
- },
- {
- name: "O_CREATE | O_EXCL - Fail if exists",
- path: filepath.Join(root, "existing.txt"),
- flag: os.O_WRONLY | os.O_CREATE | os.O_EXCL,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "existing.txt"): "..."},
- },
- expectedError: expectedError{
- wantErrIs: os.ErrExist,
- },
- },
- {
- name: "O_TRUNC - Truncate existing file",
- path: filepath.Join(root, "file.txt"),
- flag: os.O_WRONLY | os.O_TRUNC,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "to be truncated"},
- },
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("new data"))
- require.NoError(t, err)
- },
- expectedContent: stringPtr("new data"),
- },
- {
- name: "Error - Open non-existent for reading",
- path: filepath.Join(root, "no.txt"),
- flag: os.O_RDONLY,
- expectedError: expectedError{
- wantErrIs: os.ErrNotExist,
- },
- },
- {
- name: "Error - Path is a directory",
- path: filepath.Join(root, "mydir"),
- flag: os.O_WRONLY,
- setup: unittestSetup{
- initialDirs: []string{filepath.Join(root, "mydir")},
- },
- expectedError: expectedError{
- wantErrMsg: "is a directory",
- },
- },
- {
- name: "Error - Open symlink to directory for writing",
- path: filepath.Join(root, "link_to_dir"),
- flag: os.O_WRONLY,
- setup: unittestSetup{
- initialDirs: []string{filepath.Join(root, "dir")},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_dir"): "dir",
- },
- },
- expectedError: expectedError{
- wantErrMsg: "is a directory",
- },
- },
- {
- name: "Open symlink to file",
- path: filepath.Join(root, "link_to_file"),
- flag: os.O_RDONLY,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "content"},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_file"): "file.txt",
- },
- },
- expectedContent: stringPtr("content"),
- },
- {
- name: "Write to symlink to file",
- path: filepath.Join(root, "link_to_file"),
- flag: os.O_WRONLY,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "old"},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_file"): "file.txt",
- },
- },
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("new"))
- require.NoError(t, err)
- },
- expectedContent: stringPtr("new"),
- },
- {
- name: "Open broken symlink",
- path: filepath.Join(root, "broken_link"),
- flag: os.O_RDONLY,
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "broken_link"): "nonexistent",
- },
- },
- expectedError: expectedError{
- wantErrIs: os.ErrNotExist,
- },
- },
- {
- name: "Create file via symlink",
- path: filepath.Join(root, "link_to_new"),
- flag: os.O_WRONLY | os.O_CREATE,
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_new"): "new.txt",
- },
- },
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("created"))
- require.NoError(t, err)
- },
- // We check the symlink path content (which should read the target)
- expectedContent: stringPtr("created"),
- },
- {
- name: "O_EXCL fail on existing symlink",
- path: filepath.Join(root, "link"),
- flag: os.O_WRONLY | os.O_CREATE | os.O_EXCL,
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): ""},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link"): "file.txt",
- },
- },
- expectedError: expectedError{
- wantErrIs: os.ErrExist,
- },
- },
- {
- name: "Open symlink loop",
- path: filepath.Join(root, "loop1"),
- flag: os.O_RDONLY,
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "loop1"): "loop2",
- filepath.Join(root, "loop2"): "loop1",
- },
- },
- expectedError: expectedError{
- wantErrIs: syscall.ELOOP,
- },
- },
- }
+ _, err = file.Write([]byte("finish"))
+ require.NoError(t, err)
+ },
+ expectedContent: stringPtr("finish"),
+ },
+ {
+ name: "O_APPEND - Append to file",
+ path: filepath.Join(root, "file.txt"),
+ flag: os.O_WRONLY | os.O_APPEND,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "first,"},
+ },
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("second"))
+ require.NoError(t, err)
+ },
+ expectedContent: stringPtr("first,second"),
+ },
+ {
+ name: "O_CREATE - Create new file",
+ path: filepath.Join(root, "newfile.txt"),
+ flag: os.O_WRONLY | os.O_CREATE,
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("created"))
+ require.NoError(t, err)
+ },
+ expectedContent: stringPtr("created"),
+ },
+ {
+ name: "O_CREATE | O_EXCL - Fail if exists",
+ path: filepath.Join(root, "existing.txt"),
+ flag: os.O_WRONLY | os.O_CREATE | os.O_EXCL,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "existing.txt"): "..."},
+ },
+ expectedError: expectedError{
+ wantErrIs: os.ErrExist,
+ },
+ },
+ {
+ name: "O_TRUNC - Truncate existing file",
+ path: filepath.Join(root, "file.txt"),
+ flag: os.O_WRONLY | os.O_TRUNC,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "to be truncated"},
+ },
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("new data"))
+ require.NoError(t, err)
+ },
+ expectedContent: stringPtr("new data"),
+ },
+ {
+ name: "Error - Open non-existent for reading",
+ path: filepath.Join(root, "no.txt"),
+ flag: os.O_RDONLY,
+ expectedError: expectedError{
+ wantErrIs: os.ErrNotExist,
+ },
+ },
+ {
+ name: "Error - Path is a directory",
+ path: filepath.Join(root, "mydir"),
+ flag: os.O_WRONLY,
+ setup: unittestSetup{
+ initialDirs: []string{filepath.Join(root, "mydir")},
+ },
+ expectedError: expectedError{
+ wantErrMsg: "is a directory",
+ },
+ },
+ {
+ name: "Error - Open symlink to directory for writing",
+ path: filepath.Join(root, "link_to_dir"),
+ flag: os.O_WRONLY,
+ setup: unittestSetup{
+ initialDirs: []string{filepath.Join(root, "dir")},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_dir"): "dir",
+ },
+ },
+ expectedError: expectedError{
+ wantErrMsg: "is a directory",
+ },
+ },
+ {
+ name: "Open symlink to file",
+ path: filepath.Join(root, "link_to_file"),
+ flag: os.O_RDONLY,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "content"},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_file"): "file.txt",
+ },
+ },
+ expectedContent: stringPtr("content"),
+ },
+ {
+ name: "Write to symlink to file",
+ path: filepath.Join(root, "link_to_file"),
+ flag: os.O_WRONLY,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "old"},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_file"): "file.txt",
+ },
+ },
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("new"))
+ require.NoError(t, err)
+ },
+ expectedContent: stringPtr("new"),
+ },
+ {
+ name: "Open broken symlink",
+ path: filepath.Join(root, "broken_link"),
+ flag: os.O_RDONLY,
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "broken_link"): "nonexistent",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: os.ErrNotExist,
+ },
+ },
+ {
+ name: "Create file via symlink",
+ path: filepath.Join(root, "link_to_new"),
+ flag: os.O_WRONLY | os.O_CREATE,
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_new"): "new.txt",
+ },
+ },
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("created"))
+ require.NoError(t, err)
+ },
+ // We check the symlink path content (which should read the target)
+ expectedContent: stringPtr("created"),
+ },
+ {
+ name: "O_EXCL fail on existing symlink",
+ path: filepath.Join(root, "link"),
+ flag: os.O_WRONLY | os.O_CREATE | os.O_EXCL,
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): ""},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link"): "file.txt",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: os.ErrExist,
+ },
+ },
+ {
+ name: "Open symlink loop",
+ path: filepath.Join(root, "loop1"),
+ flag: os.O_RDONLY,
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "loop1"): "loop2",
+ filepath.Join(root, "loop2"): "loop1",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: syscall.ELOOP,
+ },
+ },
+ }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- wrapper := tc.setup.setup(t)
- file, err := wrapper.OpenFile(tc.path, tc.flag, 0666)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ wrapper := tc.setup.setup(t)
+ file, err := wrapper.OpenFile(tc.path, tc.flag, 0666)
- if tc.expectedError.Check(t, err) {
- return
- }
+ if tc.expectedError.Check(t, err) {
+ return
+ }
- require.NotNil(t, file)
- defer file.Close()
- if tc.action != nil {
- tc.action(t, file)
- }
+ require.NotNil(t, file)
+ defer file.Close()
+ if tc.action != nil {
+ tc.action(t, file)
+ }
- // Close the file to ensure contents are flushed.
- require.NoError(t, file.Close())
+ // Close the file to ensure contents are flushed.
+ require.NoError(t, file.Close())
- if tc.expectedContent != nil {
- content, err := wrapper.ReadFile(tc.path)
- require.NoError(t, err)
- require.Equal(t, *tc.expectedContent, string(content))
- }
- })
- }
+ if tc.expectedContent != nil {
+ content, err := wrapper.ReadFile(tc.path)
+ require.NoError(t, err)
+ require.Equal(t, *tc.expectedContent, string(content))
+ }
+ })
+ }
}
func TestFSTestOSWrapper_OpenFile_MatchesReal(t *testing.T) {
- tests := []struct {
- name string
- setup matchesRealSetup
- path string
- flag int
- action func(t *testing.T, file oswrapper.File)
- }{
- {
- name: "O_RDONLY - Read existing file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "read me",
- },
- }},
- path: "file.txt",
- flag: os.O_RDONLY,
- },
- {
- name: "O_RDONLY - Error on write",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "read only",
- },
- }},
- path: "file.txt",
- flag: os.O_RDONLY,
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("fail"))
- require.Error(t, err)
- },
- },
- {
- name: "O_WRONLY - Write to existing file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "initial",
- },
- }},
- path: "file.txt",
- flag: os.O_WRONLY,
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("new"))
- require.NoError(t, err)
- },
- },
- {
- name: "O_WRONLY - Error on read",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "write only",
- },
- }},
- path: "file.txt",
- flag: os.O_WRONLY,
- action: func(t *testing.T, file oswrapper.File) {
- _, err := io.ReadAll(file)
- require.Error(t, err)
- },
- },
- {
- name: "O_RDWR - Read and Write",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "start",
- },
- }},
- path: "file.txt",
- flag: os.O_RDWR,
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("finish"))
- require.NoError(t, err)
- },
- },
- {
- name: "O_APPEND - Append to file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "first,",
- },
- }},
- path: "file.txt",
- flag: os.O_WRONLY | os.O_APPEND,
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("second"))
- require.NoError(t, err)
- },
- },
- {
- name: "O_CREATE - Create new file",
- path: "newfile.txt",
- flag: os.O_WRONLY | os.O_CREATE,
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("created"))
- require.NoError(t, err)
- },
- },
- {
- name: "O_CREATE | O_EXCL - Fail if exists",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "existing.txt": "...",
- },
- }},
- path: "existing.txt",
- flag: os.O_WRONLY | os.O_CREATE | os.O_EXCL,
- },
- {
- name: "O_TRUNC - Truncate existing file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "to be truncated",
- },
- }},
- path: "file.txt",
- flag: os.O_WRONLY | os.O_TRUNC,
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("new data"))
- require.NoError(t, err)
- },
- },
- {
- name: "Error - Open non-existent for reading",
- path: "no.txt",
- flag: os.O_RDONLY,
- },
- {
- name: "Error - Path is a directory",
- setup: matchesRealSetup{unittestSetup{
- initialDirs: []string{"mydir"},
- }},
- path: "mydir",
- flag: os.O_WRONLY,
- },
- {
- name: "Error - Open symlink to directory for writing",
- setup: matchesRealSetup{unittestSetup{
- initialDirs: []string{"dir"},
- initialSymlinks: map[string]string{
- "link_to_dir": "dir",
- },
- }},
- path: "link_to_dir",
- flag: os.O_WRONLY,
- },
- {
- name: "Open symlink to file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{"file.txt": "content"},
- initialSymlinks: map[string]string{
- "link_to_file": "file.txt",
- },
- }},
- path: "link_to_file",
- flag: os.O_RDONLY,
- },
- {
- name: "Write to symlink to file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{"file.txt": "old"},
- initialSymlinks: map[string]string{
- "link_to_file": "file.txt",
- },
- }},
- path: "link_to_file",
- flag: os.O_WRONLY,
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("new"))
- require.NoError(t, err)
- },
- },
- {
- name: "Open broken symlink",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "broken_link": "nonexistent",
- },
- }},
- path: "broken_link",
- flag: os.O_RDONLY,
- },
- {
- name: "Create file via symlink",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "link_to_new": "new.txt",
- },
- }},
- path: "link_to_new",
- flag: os.O_WRONLY | os.O_CREATE,
- action: func(t *testing.T, file oswrapper.File) {
- _, err := file.Write([]byte("created"))
- require.NoError(t, err)
- },
- },
- {
- name: "O_EXCL fail on existing symlink",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{"file.txt": ""},
- initialSymlinks: map[string]string{
- "link": "file.txt",
- },
- }},
- path: "link",
- flag: os.O_WRONLY | os.O_CREATE | os.O_EXCL,
- },
- {
- name: "Open symlink loop",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "loop1": "loop2",
- "loop2": "loop1",
- },
- }},
- path: "loop1",
- flag: os.O_RDONLY,
- },
- }
+ tests := []struct {
+ name string
+ setup matchesRealSetup
+ path string
+ flag int
+ action func(t *testing.T, file oswrapper.File)
+ }{
+ {
+ name: "O_RDONLY - Read existing file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "read me",
+ },
+ }},
+ path: "file.txt",
+ flag: os.O_RDONLY,
+ },
+ {
+ name: "O_RDONLY - Error on write",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "read only",
+ },
+ }},
+ path: "file.txt",
+ flag: os.O_RDONLY,
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("fail"))
+ require.Error(t, err)
+ },
+ },
+ {
+ name: "O_WRONLY - Write to existing file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "initial",
+ },
+ }},
+ path: "file.txt",
+ flag: os.O_WRONLY,
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("new"))
+ require.NoError(t, err)
+ },
+ },
+ {
+ name: "O_WRONLY - Error on read",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "write only",
+ },
+ }},
+ path: "file.txt",
+ flag: os.O_WRONLY,
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := io.ReadAll(file)
+ require.Error(t, err)
+ },
+ },
+ {
+ name: "O_RDWR - Read and Write",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "start",
+ },
+ }},
+ path: "file.txt",
+ flag: os.O_RDWR,
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("finish"))
+ require.NoError(t, err)
+ },
+ },
+ {
+ name: "O_APPEND - Append to file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "first,",
+ },
+ }},
+ path: "file.txt",
+ flag: os.O_WRONLY | os.O_APPEND,
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("second"))
+ require.NoError(t, err)
+ },
+ },
+ {
+ name: "O_CREATE - Create new file",
+ path: "newfile.txt",
+ flag: os.O_WRONLY | os.O_CREATE,
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("created"))
+ require.NoError(t, err)
+ },
+ },
+ {
+ name: "O_CREATE | O_EXCL - Fail if exists",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "existing.txt": "...",
+ },
+ }},
+ path: "existing.txt",
+ flag: os.O_WRONLY | os.O_CREATE | os.O_EXCL,
+ },
+ {
+ name: "O_TRUNC - Truncate existing file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "to be truncated",
+ },
+ }},
+ path: "file.txt",
+ flag: os.O_WRONLY | os.O_TRUNC,
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("new data"))
+ require.NoError(t, err)
+ },
+ },
+ {
+ name: "Error - Open non-existent for reading",
+ path: "no.txt",
+ flag: os.O_RDONLY,
+ },
+ {
+ name: "Error - Path is a directory",
+ setup: matchesRealSetup{unittestSetup{
+ initialDirs: []string{"mydir"},
+ }},
+ path: "mydir",
+ flag: os.O_WRONLY,
+ },
+ {
+ name: "Error - Open symlink to directory for writing",
+ setup: matchesRealSetup{unittestSetup{
+ initialDirs: []string{"dir"},
+ initialSymlinks: map[string]string{
+ "link_to_dir": "dir",
+ },
+ }},
+ path: "link_to_dir",
+ flag: os.O_WRONLY,
+ },
+ {
+ name: "Open symlink to file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{"file.txt": "content"},
+ initialSymlinks: map[string]string{
+ "link_to_file": "file.txt",
+ },
+ }},
+ path: "link_to_file",
+ flag: os.O_RDONLY,
+ },
+ {
+ name: "Write to symlink to file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{"file.txt": "old"},
+ initialSymlinks: map[string]string{
+ "link_to_file": "file.txt",
+ },
+ }},
+ path: "link_to_file",
+ flag: os.O_WRONLY,
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("new"))
+ require.NoError(t, err)
+ },
+ },
+ {
+ name: "Open broken symlink",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "broken_link": "nonexistent",
+ },
+ }},
+ path: "broken_link",
+ flag: os.O_RDONLY,
+ },
+ {
+ name: "Create file via symlink",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "link_to_new": "new.txt",
+ },
+ }},
+ path: "link_to_new",
+ flag: os.O_WRONLY | os.O_CREATE,
+ action: func(t *testing.T, file oswrapper.File) {
+ _, err := file.Write([]byte("created"))
+ require.NoError(t, err)
+ },
+ },
+ {
+ name: "O_EXCL fail on existing symlink",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{"file.txt": ""},
+ initialSymlinks: map[string]string{
+ "link": "file.txt",
+ },
+ }},
+ path: "link",
+ flag: os.O_WRONLY | os.O_CREATE | os.O_EXCL,
+ },
+ {
+ name: "Open symlink loop",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "loop1": "loop2",
+ "loop2": "loop1",
+ },
+ }},
+ path: "loop1",
+ flag: os.O_RDONLY,
+ },
+ }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- realRoot, realFS, testFS := tc.setup.setup(t)
- defer os.RemoveAll(realRoot)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ realRoot, realFS, testFS := tc.setup.setup(t)
+ defer os.RemoveAll(realRoot)
- // Execute on Real FS
- realFile, realErr := realFS.OpenFile(filepath.Join(realRoot, tc.path), tc.flag, 0666)
- if realErr == nil {
- defer realFile.Close()
- if tc.action != nil {
- tc.action(t, realFile)
- }
- require.NoError(t, realFile.Close())
- }
+ // Execute on Real FS
+ realFile, realErr := realFS.OpenFile(filepath.Join(realRoot, tc.path), tc.flag, 0666)
+ if realErr == nil {
+ defer realFile.Close()
+ if tc.action != nil {
+ tc.action(t, realFile)
+ }
+ require.NoError(t, realFile.Close())
+ }
- // Execute on Test FS
- testFile, testErr := testFS.OpenFile(tc.path, tc.flag, 0666)
- if testErr == nil {
- defer testFile.Close()
- if tc.action != nil {
- tc.action(t, testFile)
- }
- require.NoError(t, testFile.Close())
- }
+ // Execute on Test FS
+ testFile, testErr := testFS.OpenFile(tc.path, tc.flag, 0666)
+ if testErr == nil {
+ defer testFile.Close()
+ if tc.action != nil {
+ tc.action(t, testFile)
+ }
+ require.NoError(t, testFile.Close())
+ }
- requireErrorsMatch(t, realErr, testErr)
- if realErr == nil {
- requireFileSystemsMatch(t, realRoot, testFS)
- }
- })
- }
+ requireErrorsMatch(t, realErr, testErr)
+ if realErr == nil {
+ requireFileSystemsMatch(t, realRoot, testFS)
+ }
+ })
+ }
}
diff --git a/tools/src/oswrapper/fstestoswrapper_readdir_test.go b/tools/src/oswrapper/fstestoswrapper_readdir_test.go
index 3392a82..600349e 100644
--- a/tools/src/oswrapper/fstestoswrapper_readdir_test.go
+++ b/tools/src/oswrapper/fstestoswrapper_readdir_test.go
@@ -27,12 +27,12 @@
package oswrapper_test
import (
- "os"
- "path/filepath"
- "syscall"
- "testing"
+ "os"
+ "path/filepath"
+ "syscall"
+ "testing"
- "github.com/stretchr/testify/require"
+ "github.com/stretchr/testify/require"
)
// Tests for the ReadDir() function in FSTestOSWrapper.
@@ -44,226 +44,226 @@
// defined expectations.
func TestFSTestOSWrapper_ReadDir(t *testing.T) {
- root := getTestRoot()
- tests := []struct {
- name string
- setup unittestSetup
- path string
- expectedEntries []string
- expectedError
- }{
- {
- name: "Read empty directory",
- path: filepath.Join(root, "emptydir"),
- setup: unittestSetup{
- initialDirs: []string{filepath.Join(root, "emptydir")},
- },
- expectedEntries: []string{},
- },
- {
- name: "Read directory with files and subdirectories",
- path: filepath.Join(root, "dir"),
- setup: unittestSetup{
- initialFiles: map[string]string{
- filepath.Join(root, "dir", "file1.txt"): "",
- filepath.Join(root, "dir", "z_file.txt"): "",
- },
- initialDirs: []string{filepath.Join(root, "dir", "subdir", "nested")},
- },
- expectedEntries: []string{"file1.txt", "subdir", "z_file.txt"},
- },
- {
- name: "Read non-existent directory",
- path: filepath.Join(root, "nonexistent"),
- expectedError: expectedError{
- wantErrIs: os.ErrNotExist,
- },
- },
- {
- name: "Read a file",
- path: filepath.Join(root, "file.txt"),
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): ""},
- },
- expectedError: expectedError{
- wantErrMsg: "not a directory",
- },
- },
- {
- name: "Read symlink to directory",
- path: filepath.Join(root, "link_to_dir"),
- setup: unittestSetup{
- initialFiles: map[string]string{
- filepath.Join(root, "dir", "file1.txt"): "",
- },
- initialDirs: []string{filepath.Join(root, "dir")},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_dir"): "dir",
- },
- },
- expectedEntries: []string{"file1.txt"},
- },
- {
- name: "Read symlink to file",
- path: filepath.Join(root, "link_to_file"),
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): ""},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_file"): "file.txt",
- },
- },
- expectedError: expectedError{
- wantErrMsg: "not a directory",
- },
- },
- {
- name: "Read broken symlink",
- path: filepath.Join(root, "broken_link"),
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "broken_link"): "nonexistent",
- },
- },
- expectedError: expectedError{
- wantErrIs: os.ErrNotExist,
- },
- },
- {
- name: "ReadDir symlink loop",
- path: filepath.Join(root, "loop1"),
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "loop1"): "loop2",
- filepath.Join(root, "loop2"): "loop1",
- },
- },
- expectedError: expectedError{
- wantErrIs: syscall.ELOOP,
- },
- },
- }
+ root := getTestRoot()
+ tests := []struct {
+ name string
+ setup unittestSetup
+ path string
+ expectedEntries []string
+ expectedError
+ }{
+ {
+ name: "Read empty directory",
+ path: filepath.Join(root, "emptydir"),
+ setup: unittestSetup{
+ initialDirs: []string{filepath.Join(root, "emptydir")},
+ },
+ expectedEntries: []string{},
+ },
+ {
+ name: "Read directory with files and subdirectories",
+ path: filepath.Join(root, "dir"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{
+ filepath.Join(root, "dir", "file1.txt"): "",
+ filepath.Join(root, "dir", "z_file.txt"): "",
+ },
+ initialDirs: []string{filepath.Join(root, "dir", "subdir", "nested")},
+ },
+ expectedEntries: []string{"file1.txt", "subdir", "z_file.txt"},
+ },
+ {
+ name: "Read non-existent directory",
+ path: filepath.Join(root, "nonexistent"),
+ expectedError: expectedError{
+ wantErrIs: os.ErrNotExist,
+ },
+ },
+ {
+ name: "Read a file",
+ path: filepath.Join(root, "file.txt"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): ""},
+ },
+ expectedError: expectedError{
+ wantErrMsg: "not a directory",
+ },
+ },
+ {
+ name: "Read symlink to directory",
+ path: filepath.Join(root, "link_to_dir"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{
+ filepath.Join(root, "dir", "file1.txt"): "",
+ },
+ initialDirs: []string{filepath.Join(root, "dir")},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_dir"): "dir",
+ },
+ },
+ expectedEntries: []string{"file1.txt"},
+ },
+ {
+ name: "Read symlink to file",
+ path: filepath.Join(root, "link_to_file"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): ""},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_file"): "file.txt",
+ },
+ },
+ expectedError: expectedError{
+ wantErrMsg: "not a directory",
+ },
+ },
+ {
+ name: "Read broken symlink",
+ path: filepath.Join(root, "broken_link"),
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "broken_link"): "nonexistent",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: os.ErrNotExist,
+ },
+ },
+ {
+ name: "ReadDir symlink loop",
+ path: filepath.Join(root, "loop1"),
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "loop1"): "loop2",
+ filepath.Join(root, "loop2"): "loop1",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: syscall.ELOOP,
+ },
+ },
+ }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- wrapper := tc.setup.setup(t)
- entries, err := wrapper.ReadDir(tc.path)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ wrapper := tc.setup.setup(t)
+ entries, err := wrapper.ReadDir(tc.path)
- if tc.expectedError.Check(t, err) {
- return
- }
+ if tc.expectedError.Check(t, err) {
+ return
+ }
- entryNames := make([]string, len(entries))
- for i, e := range entries {
- entryNames[i] = e.Name()
- }
- require.ElementsMatch(t, tc.expectedEntries, entryNames)
- })
- }
+ entryNames := make([]string, len(entries))
+ for i, e := range entries {
+ entryNames[i] = e.Name()
+ }
+ require.ElementsMatch(t, tc.expectedEntries, entryNames)
+ })
+ }
}
func TestFSTestOSWrapper_ReadDir_MatchesReal(t *testing.T) {
- tests := []struct {
- name string
- setup matchesRealSetup
- path string
- }{
- {
- name: "Read empty directory",
- setup: matchesRealSetup{unittestSetup{
- initialDirs: []string{"emptydir"},
- }},
- path: "emptydir",
- },
- {
- name: "Read directory with files and subdirectories",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- filepath.Join("dir", "file1.txt"): "content",
- filepath.Join("dir", "z_file.txt"): "content",
- },
- initialDirs: []string{
- "dir",
- filepath.Join("dir", "subdir", "nested"),
- },
- }},
- path: "dir",
- },
- {
- name: "Error on non-existent directory",
- path: "nonexistent",
- },
- {
- name: "Error on path is a file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{"file.txt": "content"},
- }},
- path: "file.txt",
- },
- {
- name: "Read symlink to directory",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- filepath.Join("dir", "file1.txt"): "content",
- },
- initialDirs: []string{"dir"},
- initialSymlinks: map[string]string{
- "link_to_dir": "dir",
- },
- }},
- path: "link_to_dir",
- },
- {
- name: "Read symlink to file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{"file.txt": "content"},
- initialSymlinks: map[string]string{
- "link_to_file": "file.txt",
- },
- }},
- path: "link_to_file",
- },
- {
- name: "Read broken symlink",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "broken_link": "nonexistent",
- },
- }},
- path: "broken_link",
- },
- {
- name: "ReadDir symlink loop",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "loop1": "loop2",
- "loop2": "loop1",
- },
- }},
- path: "loop1",
- },
- }
+ tests := []struct {
+ name string
+ setup matchesRealSetup
+ path string
+ }{
+ {
+ name: "Read empty directory",
+ setup: matchesRealSetup{unittestSetup{
+ initialDirs: []string{"emptydir"},
+ }},
+ path: "emptydir",
+ },
+ {
+ name: "Read directory with files and subdirectories",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ filepath.Join("dir", "file1.txt"): "content",
+ filepath.Join("dir", "z_file.txt"): "content",
+ },
+ initialDirs: []string{
+ "dir",
+ filepath.Join("dir", "subdir", "nested"),
+ },
+ }},
+ path: "dir",
+ },
+ {
+ name: "Error on non-existent directory",
+ path: "nonexistent",
+ },
+ {
+ name: "Error on path is a file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{"file.txt": "content"},
+ }},
+ path: "file.txt",
+ },
+ {
+ name: "Read symlink to directory",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ filepath.Join("dir", "file1.txt"): "content",
+ },
+ initialDirs: []string{"dir"},
+ initialSymlinks: map[string]string{
+ "link_to_dir": "dir",
+ },
+ }},
+ path: "link_to_dir",
+ },
+ {
+ name: "Read symlink to file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{"file.txt": "content"},
+ initialSymlinks: map[string]string{
+ "link_to_file": "file.txt",
+ },
+ }},
+ path: "link_to_file",
+ },
+ {
+ name: "Read broken symlink",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "broken_link": "nonexistent",
+ },
+ }},
+ path: "broken_link",
+ },
+ {
+ name: "ReadDir symlink loop",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "loop1": "loop2",
+ "loop2": "loop1",
+ },
+ }},
+ path: "loop1",
+ },
+ }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- realRoot, realFS, testFS := tc.setup.setup(t)
- defer os.RemoveAll(realRoot)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ realRoot, realFS, testFS := tc.setup.setup(t)
+ defer os.RemoveAll(realRoot)
- // Execute
- realEntries, realErr := realFS.ReadDir(filepath.Join(realRoot, tc.path))
- testEntries, testErr := testFS.ReadDir(tc.path)
+ // Execute
+ realEntries, realErr := realFS.ReadDir(filepath.Join(realRoot, tc.path))
+ testEntries, testErr := testFS.ReadDir(tc.path)
- requireErrorsMatch(t, realErr, testErr)
- if realErr == nil {
- realNames := make([]string, len(realEntries))
- for i, e := range realEntries {
- realNames[i] = e.Name()
- }
- testNames := make([]string, len(testEntries))
- for i, e := range testEntries {
- testNames[i] = e.Name()
- }
- require.ElementsMatch(t, realNames, testNames)
- }
- })
- }
+ requireErrorsMatch(t, realErr, testErr)
+ if realErr == nil {
+ realNames := make([]string, len(realEntries))
+ for i, e := range realEntries {
+ realNames[i] = e.Name()
+ }
+ testNames := make([]string, len(testEntries))
+ for i, e := range testEntries {
+ testNames[i] = e.Name()
+ }
+ require.ElementsMatch(t, realNames, testNames)
+ }
+ })
+ }
}
diff --git a/tools/src/oswrapper/fstestoswrapper_readfile_test.go b/tools/src/oswrapper/fstestoswrapper_readfile_test.go
index fe973ca..c61ccef 100644
--- a/tools/src/oswrapper/fstestoswrapper_readfile_test.go
+++ b/tools/src/oswrapper/fstestoswrapper_readfile_test.go
@@ -27,12 +27,12 @@
package oswrapper_test
import (
- "os"
- "path/filepath"
- "syscall"
- "testing"
+ "os"
+ "path/filepath"
+ "syscall"
+ "testing"
- "github.com/stretchr/testify/require"
+ "github.com/stretchr/testify/require"
)
// Tests for the ReadFile() function of FSTestOSWrapper.
@@ -44,186 +44,186 @@
// defined expectations.
func TestFSTestOSWrapper_ReadFile(t *testing.T) {
- root := getTestRoot()
- tests := []struct {
- name string
- setup unittestSetup
- path string
- expectedContent []byte
- expectedError
- }{
- {
- name: "Read existing file",
- path: filepath.Join(root, "file.txt"),
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "hello world"},
- },
- expectedContent: []byte("hello world"),
- },
- {
- name: "Read non-existent file",
- path: filepath.Join(root, "nonexistent.txt"),
- expectedError: expectedError{
- wantErrIs: os.ErrNotExist,
- },
- },
- {
- name: "Read a directory",
- path: filepath.Join(root, "mydir"),
- setup: unittestSetup{
- initialDirs: []string{filepath.Join(root, "mydir")},
- },
- expectedError: expectedError{
- wantErrMsg: "is a directory",
- },
- },
- {
- name: "Read symlink to file",
- path: filepath.Join(root, "link_to_file"),
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "content"},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_file"): "file.txt",
- },
- },
- expectedContent: []byte("content"),
- },
- {
- name: "Read symlink to dir",
- path: filepath.Join(root, "link_to_dir"),
- setup: unittestSetup{
- initialDirs: []string{filepath.Join(root, "dir")},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_dir"): "dir",
- },
- },
- expectedError: expectedError{
- wantErrMsg: "is a directory",
- },
- },
- {
- name: "Read broken symlink",
- path: filepath.Join(root, "broken_link"),
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "broken_link"): "nonexistent",
- },
- },
- expectedError: expectedError{
- wantErrIs: os.ErrNotExist,
- },
- },
- {
- name: "Read symlink loop",
- path: filepath.Join(root, "loop1"),
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "loop1"): "loop2",
- filepath.Join(root, "loop2"): "loop1",
- },
- },
- expectedError: expectedError{
- wantErrIs: syscall.ELOOP,
- },
- },
- }
+ root := getTestRoot()
+ tests := []struct {
+ name string
+ setup unittestSetup
+ path string
+ expectedContent []byte
+ expectedError
+ }{
+ {
+ name: "Read existing file",
+ path: filepath.Join(root, "file.txt"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "hello world"},
+ },
+ expectedContent: []byte("hello world"),
+ },
+ {
+ name: "Read non-existent file",
+ path: filepath.Join(root, "nonexistent.txt"),
+ expectedError: expectedError{
+ wantErrIs: os.ErrNotExist,
+ },
+ },
+ {
+ name: "Read a directory",
+ path: filepath.Join(root, "mydir"),
+ setup: unittestSetup{
+ initialDirs: []string{filepath.Join(root, "mydir")},
+ },
+ expectedError: expectedError{
+ wantErrMsg: "is a directory",
+ },
+ },
+ {
+ name: "Read symlink to file",
+ path: filepath.Join(root, "link_to_file"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "content"},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_file"): "file.txt",
+ },
+ },
+ expectedContent: []byte("content"),
+ },
+ {
+ name: "Read symlink to dir",
+ path: filepath.Join(root, "link_to_dir"),
+ setup: unittestSetup{
+ initialDirs: []string{filepath.Join(root, "dir")},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_dir"): "dir",
+ },
+ },
+ expectedError: expectedError{
+ wantErrMsg: "is a directory",
+ },
+ },
+ {
+ name: "Read broken symlink",
+ path: filepath.Join(root, "broken_link"),
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "broken_link"): "nonexistent",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: os.ErrNotExist,
+ },
+ },
+ {
+ name: "Read symlink loop",
+ path: filepath.Join(root, "loop1"),
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "loop1"): "loop2",
+ filepath.Join(root, "loop2"): "loop1",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: syscall.ELOOP,
+ },
+ },
+ }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- wrapper := tc.setup.setup(t)
- content, err := wrapper.ReadFile(tc.path)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ wrapper := tc.setup.setup(t)
+ content, err := wrapper.ReadFile(tc.path)
- if tc.expectedError.Check(t, err) {
- return
- }
+ if tc.expectedError.Check(t, err) {
+ return
+ }
- require.Equal(t, tc.expectedContent, content)
- })
- }
+ require.Equal(t, tc.expectedContent, content)
+ })
+ }
}
func TestFSTestOSWrapper_ReadFile_MatchesReal(t *testing.T) {
- tests := []struct {
- name string
- setup matchesRealSetup
- path string
- }{
- {
- name: "Read existing file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "hello world",
- },
- }},
- path: "file.txt",
- },
- {
- name: "Error on non-existent file",
- path: "nonexistent.txt",
- },
- {
- name: "Error on path is a directory",
- setup: matchesRealSetup{unittestSetup{
- initialDirs: []string{
- "mydir",
- },
- }},
- path: "mydir",
- },
- {
- name: "Read symlink to file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{"file.txt": "content"},
- initialSymlinks: map[string]string{
- "link_to_file": "file.txt",
- },
- }},
- path: "link_to_file",
- },
- {
- name: "Read symlink to dir",
- setup: matchesRealSetup{unittestSetup{
- initialDirs: []string{"dir"},
- initialSymlinks: map[string]string{
- "link_to_dir": "dir",
- },
- }},
- path: "link_to_dir",
- },
- {
- name: "Read broken symlink",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "broken_link": "nonexistent",
- },
- }},
- path: "broken_link",
- },
- {
- name: "Read symlink loop",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "loop1": "loop2",
- "loop2": "loop1",
- },
- }},
- path: "loop1",
- },
- }
+ tests := []struct {
+ name string
+ setup matchesRealSetup
+ path string
+ }{
+ {
+ name: "Read existing file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "hello world",
+ },
+ }},
+ path: "file.txt",
+ },
+ {
+ name: "Error on non-existent file",
+ path: "nonexistent.txt",
+ },
+ {
+ name: "Error on path is a directory",
+ setup: matchesRealSetup{unittestSetup{
+ initialDirs: []string{
+ "mydir",
+ },
+ }},
+ path: "mydir",
+ },
+ {
+ name: "Read symlink to file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{"file.txt": "content"},
+ initialSymlinks: map[string]string{
+ "link_to_file": "file.txt",
+ },
+ }},
+ path: "link_to_file",
+ },
+ {
+ name: "Read symlink to dir",
+ setup: matchesRealSetup{unittestSetup{
+ initialDirs: []string{"dir"},
+ initialSymlinks: map[string]string{
+ "link_to_dir": "dir",
+ },
+ }},
+ path: "link_to_dir",
+ },
+ {
+ name: "Read broken symlink",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "broken_link": "nonexistent",
+ },
+ }},
+ path: "broken_link",
+ },
+ {
+ name: "Read symlink loop",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "loop1": "loop2",
+ "loop2": "loop1",
+ },
+ }},
+ path: "loop1",
+ },
+ }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- realRoot, realFS, testFS := tc.setup.setup(t)
- defer os.RemoveAll(realRoot)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ realRoot, realFS, testFS := tc.setup.setup(t)
+ defer os.RemoveAll(realRoot)
- // Execute
- realContent, realErr := realFS.ReadFile(filepath.Join(realRoot, tc.path))
- testContent, testErr := testFS.ReadFile(tc.path)
+ // Execute
+ realContent, realErr := realFS.ReadFile(filepath.Join(realRoot, tc.path))
+ testContent, testErr := testFS.ReadFile(tc.path)
- requireErrorsMatch(t, realErr, testErr)
- if realErr == nil {
- require.Equal(t, realContent, testContent)
- }
- })
- }
+ requireErrorsMatch(t, realErr, testErr)
+ if realErr == nil {
+ require.Equal(t, realContent, testContent)
+ }
+ })
+ }
}
diff --git a/tools/src/oswrapper/fstestoswrapper_stat_test.go b/tools/src/oswrapper/fstestoswrapper_stat_test.go
index 61972bf..df1b441 100644
--- a/tools/src/oswrapper/fstestoswrapper_stat_test.go
+++ b/tools/src/oswrapper/fstestoswrapper_stat_test.go
@@ -27,12 +27,12 @@
package oswrapper_test
import (
- "os"
- "path/filepath"
- "syscall"
- "testing"
+ "os"
+ "path/filepath"
+ "syscall"
+ "testing"
- "github.com/stretchr/testify/require"
+ "github.com/stretchr/testify/require"
)
// Tests for the Stat() function in FSTestOSWrapper.
@@ -44,232 +44,232 @@
// defined expectations.
func TestFSTestOSWrapper_Stat(t *testing.T) {
- root := getTestRoot()
- tests := []struct {
- name string
- setup unittestSetup
- path string
- verify func(t *testing.T, info os.FileInfo)
- expectedError
- }{
- {
- name: "Stat a file",
- path: filepath.Join(root, "file.txt"),
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "content"},
- },
- verify: func(t *testing.T, info os.FileInfo) {
- require.False(t, info.IsDir())
- require.Equal(t, "file.txt", info.Name())
- require.Equal(t, int64(7), info.Size())
- },
- },
- {
- name: "Stat a directory",
- path: filepath.Join(root, "dir"),
- setup: unittestSetup{
- initialDirs: []string{filepath.Join(root, "dir")},
- },
- verify: func(t *testing.T, info os.FileInfo) {
- require.True(t, info.IsDir())
- require.Equal(t, "dir", info.Name())
- },
- },
- {
- name: "Stat non-existent path",
- path: filepath.Join(root, "nonexistent"),
- expectedError: expectedError{
- wantErrIs: os.ErrNotExist,
- },
- },
- {
- name: "Stat symlink to file",
- path: filepath.Join(root, "link_to_file"),
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "content"},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_file"): "file.txt",
- },
- },
- verify: func(t *testing.T, info os.FileInfo) {
- require.False(t, info.IsDir())
- require.Equal(t, "link_to_file", info.Name())
- require.Equal(t, int64(7), info.Size())
- },
- },
- {
- name: "Stat symlink to dir",
- path: filepath.Join(root, "link_to_dir"),
- setup: unittestSetup{
- initialDirs: []string{filepath.Join(root, "dir")},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link_to_dir"): "dir",
- },
- },
- verify: func(t *testing.T, info os.FileInfo) {
- require.True(t, info.IsDir())
- require.Equal(t, "link_to_dir", info.Name())
- },
- },
- {
- name: "Stat broken symlink",
- path: filepath.Join(root, "broken_link"),
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "broken_link"): "nonexistent",
- },
- },
- expectedError: expectedError{
- wantErrIs: os.ErrNotExist,
- },
- },
- {
- name: "Stat symlink to symlink",
- path: filepath.Join(root, "link1"),
- setup: unittestSetup{
- initialFiles: map[string]string{filepath.Join(root, "file.txt"): "content"},
- initialSymlinks: map[string]string{
- filepath.Join(root, "link1"): "link2",
- filepath.Join(root, "link2"): "file.txt",
- },
- },
- verify: func(t *testing.T, info os.FileInfo) {
- require.False(t, info.IsDir())
- require.Equal(t, "link1", info.Name())
- require.Equal(t, int64(7), info.Size())
- },
- },
- {
- name: "Stat symlink loop",
- path: filepath.Join(root, "loop1"),
- setup: unittestSetup{
- initialSymlinks: map[string]string{
- filepath.Join(root, "loop1"): "loop2",
- filepath.Join(root, "loop2"): "loop1",
- },
- },
- expectedError: expectedError{
- wantErrIs: syscall.ELOOP,
- },
- },
- }
+ root := getTestRoot()
+ tests := []struct {
+ name string
+ setup unittestSetup
+ path string
+ verify func(t *testing.T, info os.FileInfo)
+ expectedError
+ }{
+ {
+ name: "Stat a file",
+ path: filepath.Join(root, "file.txt"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "content"},
+ },
+ verify: func(t *testing.T, info os.FileInfo) {
+ require.False(t, info.IsDir())
+ require.Equal(t, "file.txt", info.Name())
+ require.Equal(t, int64(7), info.Size())
+ },
+ },
+ {
+ name: "Stat a directory",
+ path: filepath.Join(root, "dir"),
+ setup: unittestSetup{
+ initialDirs: []string{filepath.Join(root, "dir")},
+ },
+ verify: func(t *testing.T, info os.FileInfo) {
+ require.True(t, info.IsDir())
+ require.Equal(t, "dir", info.Name())
+ },
+ },
+ {
+ name: "Stat non-existent path",
+ path: filepath.Join(root, "nonexistent"),
+ expectedError: expectedError{
+ wantErrIs: os.ErrNotExist,
+ },
+ },
+ {
+ name: "Stat symlink to file",
+ path: filepath.Join(root, "link_to_file"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "content"},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_file"): "file.txt",
+ },
+ },
+ verify: func(t *testing.T, info os.FileInfo) {
+ require.False(t, info.IsDir())
+ require.Equal(t, "link_to_file", info.Name())
+ require.Equal(t, int64(7), info.Size())
+ },
+ },
+ {
+ name: "Stat symlink to dir",
+ path: filepath.Join(root, "link_to_dir"),
+ setup: unittestSetup{
+ initialDirs: []string{filepath.Join(root, "dir")},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link_to_dir"): "dir",
+ },
+ },
+ verify: func(t *testing.T, info os.FileInfo) {
+ require.True(t, info.IsDir())
+ require.Equal(t, "link_to_dir", info.Name())
+ },
+ },
+ {
+ name: "Stat broken symlink",
+ path: filepath.Join(root, "broken_link"),
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "broken_link"): "nonexistent",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: os.ErrNotExist,
+ },
+ },
+ {
+ name: "Stat symlink to symlink",
+ path: filepath.Join(root, "link1"),
+ setup: unittestSetup{
+ initialFiles: map[string]string{filepath.Join(root, "file.txt"): "content"},
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "link1"): "link2",
+ filepath.Join(root, "link2"): "file.txt",
+ },
+ },
+ verify: func(t *testing.T, info os.FileInfo) {
+ require.False(t, info.IsDir())
+ require.Equal(t, "link1", info.Name())
+ require.Equal(t, int64(7), info.Size())
+ },
+ },
+ {
+ name: "Stat symlink loop",
+ path: filepath.Join(root, "loop1"),
+ setup: unittestSetup{
+ initialSymlinks: map[string]string{
+ filepath.Join(root, "loop1"): "loop2",
+ filepath.Join(root, "loop2"): "loop1",
+ },
+ },
+ expectedError: expectedError{
+ wantErrIs: syscall.ELOOP,
+ },
+ },
+ }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- wrapper := tc.setup.setup(t)
- info, err := wrapper.Stat(tc.path)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ wrapper := tc.setup.setup(t)
+ info, err := wrapper.Stat(tc.path)
- if tc.expectedError.Check(t, err) {
- return
- }
+ if tc.expectedError.Check(t, err) {
+ return
+ }
- if tc.verify != nil {
- tc.verify(t, info)
- }
- })
- }
+ if tc.verify != nil {
+ tc.verify(t, info)
+ }
+ })
+ }
}
func TestFSTestOSWrapper_Stat_MatchesReal(t *testing.T) {
- tests := []struct {
- name string
- setup matchesRealSetup
- path string
- }{
- {
- name: "Stat a file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{
- "file.txt": "content",
- },
- }},
- path: "file.txt",
- },
- {
- name: "Stat a directory",
- setup: matchesRealSetup{unittestSetup{
- initialDirs: []string{
- "dir",
- },
- }},
- path: "dir",
- },
- {
- name: "Stat non-existent path",
- path: "nonexistent",
- },
- {
- name: "Stat empty path",
- path: "",
- },
- {
- name: "Stat symlink to file",
- setup: matchesRealSetup{unittestSetup{
- initialFiles: map[string]string{"file.txt": "content"},
- initialSymlinks: map[string]string{
- "link_to_file": "file.txt",
- },
- }},
- path: "link_to_file",
- },
- {
- name: "Stat symlink to dir",
- setup: matchesRealSetup{unittestSetup{
- initialDirs: []string{"dir"},
- initialSymlinks: map[string]string{
- "link_to_dir": "dir",
- },
- }},
- path: "link_to_dir",
- },
- {
- name: "Stat broken symlink",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "broken_link": "nonexistent",
- },
- }},
- path: "broken_link",
- },
- {
- name: "Stat symlink loop",
- setup: matchesRealSetup{unittestSetup{
- initialSymlinks: map[string]string{
- "loop1": "loop2",
- "loop2": "loop1",
- },
- }},
- path: "loop1",
- },
- }
+ tests := []struct {
+ name string
+ setup matchesRealSetup
+ path string
+ }{
+ {
+ name: "Stat a file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{
+ "file.txt": "content",
+ },
+ }},
+ path: "file.txt",
+ },
+ {
+ name: "Stat a directory",
+ setup: matchesRealSetup{unittestSetup{
+ initialDirs: []string{
+ "dir",
+ },
+ }},
+ path: "dir",
+ },
+ {
+ name: "Stat non-existent path",
+ path: "nonexistent",
+ },
+ {
+ name: "Stat empty path",
+ path: "",
+ },
+ {
+ name: "Stat symlink to file",
+ setup: matchesRealSetup{unittestSetup{
+ initialFiles: map[string]string{"file.txt": "content"},
+ initialSymlinks: map[string]string{
+ "link_to_file": "file.txt",
+ },
+ }},
+ path: "link_to_file",
+ },
+ {
+ name: "Stat symlink to dir",
+ setup: matchesRealSetup{unittestSetup{
+ initialDirs: []string{"dir"},
+ initialSymlinks: map[string]string{
+ "link_to_dir": "dir",
+ },
+ }},
+ path: "link_to_dir",
+ },
+ {
+ name: "Stat broken symlink",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "broken_link": "nonexistent",
+ },
+ }},
+ path: "broken_link",
+ },
+ {
+ name: "Stat symlink loop",
+ setup: matchesRealSetup{unittestSetup{
+ initialSymlinks: map[string]string{
+ "loop1": "loop2",
+ "loop2": "loop1",
+ },
+ }},
+ path: "loop1",
+ },
+ }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- realRoot, realFS, testFS := tc.setup.setup(t)
- defer os.RemoveAll(realRoot)
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ realRoot, realFS, testFS := tc.setup.setup(t)
+ defer os.RemoveAll(realRoot)
- // Execute
- realInfo, realErr := realFS.Stat(filepath.Join(realRoot, tc.path))
- testInfo, testErr := testFS.Stat(tc.path)
+ // Execute
+ realInfo, realErr := realFS.Stat(filepath.Join(realRoot, tc.path))
+ testInfo, testErr := testFS.Stat(tc.path)
- requireErrorsMatch(t, realErr, testErr)
- if realErr == nil {
- // An empty path appears to be treated as the CWD, so the real and test
- // names cannot be directly compared.
- // TODO(crbug.com/436025865): Update this to check for the fake CWD
- // instead of "." when the filesystem is CWD-aware.
- if tc.path == "" {
- require.Equal(t, testInfo.Name(), ".")
- require.Equal(t, realInfo.Name(), filepath.Base(realRoot))
- } else {
- require.Equal(t, realInfo.Name(), testInfo.Name())
- }
- require.Equal(t, realInfo.IsDir(), testInfo.IsDir())
- // The size of a directory is system-dependent, so only compare sizes for files.
- if !realInfo.IsDir() {
- require.Equal(t, realInfo.Size(), testInfo.Size())
- }
- }
- })
- }
+ requireErrorsMatch(t, realErr, testErr)
+ if realErr == nil {
+ // An empty path appears to be treated as the CWD, so the real and test
+ // names cannot be directly compared.
+ // TODO(crbug.com/436025865): Update this to check for the fake CWD
+ // instead of "." when the filesystem is CWD-aware.
+ if tc.path == "" {
+ require.Equal(t, testInfo.Name(), ".")
+ require.Equal(t, realInfo.Name(), filepath.Base(realRoot))
+ } else {
+ require.Equal(t, realInfo.Name(), testInfo.Name())
+ }
+ require.Equal(t, realInfo.IsDir(), testInfo.IsDir())
+ // The size of a directory is system-dependent, so only compare sizes for files.
+ if !realInfo.IsDir() {
+ require.Equal(t, realInfo.Size(), testInfo.Size())
+ }
+ }
+ })
+ }
}