Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Fix [Golang] [Package] File System #25

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions filesystem/file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (rfs RealFileSystem) Stat(name string) (os.FileInfo, error) {

// FileExists checks if a file exists in the file system at the given path.
func (rfs RealFileSystem) FileExists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
_, err := rfs.Stat(name)
// Return true only if the error is nil (file exists).
return err == nil
}
30 changes: 18 additions & 12 deletions filesystem/file_system_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ var _ FileSystem = (*MockFileSystem)(nil)
// It uses a map to store file names and associated data, allowing for the simulation of file creation,
// reading, and writing without actual file system interaction.
type MockFileSystem struct {
FilesCreated map[string]*bytes.Buffer // FilesCreated maps file names to buffers holding file contents.
WriteFileCalled bool // Add this field to track if WriteFile has been called.
WriteFilePath string // Track the path provided to WriteFile.
WriteFileData []byte // Optionally track the data provided to WriteFile.
WriteFilePerm fs.FileMode // Optionally track the file permissions provided to WriteFile.
Files map[string][]byte // Files maps file names to file contents.
ReadFileCalled bool // this field to track if ReadFile has been caled.
ReadFileData []byte // Optionally track the data provided to ReadFile.
ReadFileErr error // Optionally track the error provider to ReadFile.
FileExistsCalled bool // Optionally track the result of FileExists.
FileExistsErr error // Optionally track the error provider to FileExists.
FilesCreated map[string]*bytes.Buffer // FilesCreated maps file names to buffers holding file contents.
WriteFileCalled bool // Add this field to track if WriteFile has been called.
WriteFilePath string // Track the path provided to WriteFile.
WriteFileData []byte // Optionally track the data provided to WriteFile.
WriteFilePerm fs.FileMode // Optionally track the file permissions provided to WriteFile.
Files map[string][]byte // Files maps file names to file contents.
ReadFileCalled bool // this field to track if ReadFile has been caled.
ReadFileData []byte // Optionally track the data provided to ReadFile.
ReadFileErr error // Optionally track the error provider to ReadFile.
FileExistsCalled bool // Optionally track the result of FileExists.
FileExistsErr error // Optionally track the error provider to FileExists.
FileExistsShouldError bool // Optionally track if FileExists should return an error.
}

// MockExporter is a mock implementation of the exporter.Exporter interface for testing purposes.
Expand Down Expand Up @@ -114,6 +115,11 @@ func (m mockFileInfo) Sys() interface{} { return nil } // No system-sp

// FileExists checks if the given file name exists in the mock file system.
func (m *MockFileSystem) FileExists(name string) bool {
_, exists := m.FilesCreated[name]
m.FileExistsCalled = true // Record that FileExists was called
if m.FileExistsErr != nil {
// Simulate an error condition if an error is set
return false
}
_, exists := m.Files[name] // Use the same map for all file data which can easily be mocked while touring in binary.
return exists
}
Loading