Skip to content

Conversation

0xDEC0DE
Copy link

Adds a call to file.Sync() to util.WriteFile() (right before closing the file, so it should be functionally equivalent) to ensure test coverage.

Fixes: Issue #86

Adds a call to `file.Sync()` to `util.WriteFile()` (right before
closing the file, so it should be functionally equivalent) to ensure
test coverage.

Fixes: Issue go-git#86
Copy link
Member

@pjbgf pjbgf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xDEC0DE thanks for looking into this.

@@ -178,6 +178,8 @@ type File interface {
Unlock() error
// Truncate the file.
Truncate(size int64) error
// Commit the current contents of the file to stable storage.
Sync() error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of expanding the existing interface, let's create a new interface for this:

type Syncer interface {
Sync() error
}

@@ -146,6 +146,11 @@ func (f *file) Unlock() error {
return nil
}

// Sync is a no-op in memfs.
func (f *file) Sync() error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid LSP violations, we don't implement the Sync on any structure that does not really support it.

@@ -116,6 +116,10 @@ func WriteFile(fs billy.Basic, filename string, data []byte, perm fs.FileMode) (
if err == nil && n < len(data) {
err = io.ErrShortWrite
}
err = f.Sync()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should call Sync for implementations that support it:

Suggested change
err = f.Sync()
if sf, ok := f.(Syncer); ok {
err = f.Sync()

@@ -140,6 +140,10 @@ func (*FileMock) Stat() (fs.FileInfo, error) {
return nil, nil
}

func (*FileMock) Sync() error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test to confirm Sync is being called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants