Skip to content

Commit

Permalink
update tests that werent closing files after write
Browse files Browse the repository at this point in the history
  • Loading branch information
funkyshu committed Feb 2, 2024
1 parent 63a4752 commit 50522e8
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 69 deletions.
1 change: 1 addition & 0 deletions backend/mem/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ func (s *memFileTest) TestCopyToLocationOS() {
s.NoError(err, "unexpected error creating osFile")
_, err = osFile.Write(make([]byte, 0))
s.NoError(err, "unexpected error writing zero bytes to osFile")
s.NoError(osFile.Close())

exists, err := osFile.Exists()
s.NoError(err, "unexpected existence error")
Expand Down
23 changes: 14 additions & 9 deletions backend/os/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ func (f *File) LastModified() (*time.Time, error) {
return &statsTime, err
}

// Name returns the full name of the File relative to Location.Name().
// Name returns the base name of the file path.
//
// For `file:///some/path/to/file.txt`, it would return `file.txt`
func (f *File) Name() string {
return path.Base(f.name)
}

// Path returns the the path of the File relative to Location.Name().
// Path returns absolute path, including filename,
// For `file:///some/path/to/file.txt`, it would return `/some/path/to/file.txt`
//
// If the directory portion of a file is desired, call
//
// someFile.Location().Path()
func (f *File) Path() string {
return filepath.Join(f.Location().Path(), f.Name())
}
Expand Down Expand Up @@ -143,7 +150,7 @@ func (f *File) Read(p []byte) (int, error) {
// the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and
// an error, if any.
func (f *File) Seek(offset int64, whence int) (int64, error) {
// when writing, we first write to a temp file which ensures a file isn't created before we call clase.
// when writing, we first write to a temp file which ensures a file isn't created before we call close.
// However, if we've never written AND the original file doesn't exist, we can't seek.
exists, err := f.Exists()
if err != nil {
Expand Down Expand Up @@ -386,7 +393,7 @@ func openOSFile(filePath string) (*os.File, error) {

// Ensure the path exists before opening the file, NoOp if dir already exists.
var fileMode os.FileMode = 0666
if err := os.MkdirAll(path.Dir(filePath), os.ModeDir|0777); err != nil {
if err := os.MkdirAll(path.Dir(filePath), os.ModeDir|0750); err != nil {
return nil, err
}

Expand Down Expand Up @@ -457,7 +464,7 @@ func (f *File) copyToLocalTempReader() (*os.File, error) {
// So imagine we have a file with content "hello world" and we call Seek(6, 0) and then Write([]byte("there")), the
// temp file should have "hello there" and not "there". Then finally when Close is called, the temp file is renamed
// to the original file. This code ensures that scenario works as expected.
if exists {
if exists && (f.seekCalled || f.readCalled) {
openFunc := openOSFile
if f.fileOpener != nil {
openFunc = f.fileOpener
Expand All @@ -467,10 +474,8 @@ func (f *File) copyToLocalTempReader() (*os.File, error) {
if err != nil {
return nil, err
}
if f.seekCalled || f.readCalled {
if _, err := io.Copy(tmpFile, actualFile); err != nil {
return nil, err
}
if _, err := io.Copy(tmpFile, actualFile); err != nil {
return nil, err
}

if f.cursorPos > 0 {
Expand Down
5 changes: 3 additions & 2 deletions backend/os/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@ func (s *osFileTest) TestWrite() {
f, err := s.tmploc.NewFile("test_files/writeFileFail.txt")
s.NoError(err)
s.NoError(f.Touch())
f.(*File).useTempFile = true
_, err = f.Seek(0, 0)
s.NoError(err)
f.(*File).fileOpener = func(filePath string) (*os.File, error) { return nil, errors.New("bad opener") }
data = make([]byte, 4)
_, err = f.Write(data)
Expand Down Expand Up @@ -545,7 +546,7 @@ func (s *osFileTest) TestCursor() {
s.Equal(int64(8), file.(*File).cursorPos)
s.Equal(3, sz)

_, serr = file.Seek(5, 0) // cursor 0 - in temp file
_, serr = file.Seek(5, 0) // cursor 5 - in temp file
s.Equal(int64(5), file.(*File).cursorPos)
s.NoError(serr)

Expand Down
2 changes: 1 addition & 1 deletion utils/authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,6 @@ func (a *authoritySuite) TestAuthority() {
}
}

func TestUtils(t *testing.T) {
func TestAuthority(t *testing.T) {
suite.Run(t, new(authoritySuite))
}
Loading

0 comments on commit 50522e8

Please sign in to comment.