Skip to content

Commit

Permalink
fix code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
mauri870 committed Sep 20, 2024
1 parent 0a77de6 commit 419d269
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions file/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package file

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -53,31 +54,19 @@ func TestSafeFileRotateExistingFile(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, []byte("new filebeat"), contents)

// do it again to make sure we deal with deleting the old file

err = os.WriteFile(filepath.Join(tempdir, "registry.new"),
[]byte("new filebeat 1"), 0x777)
assert.NoError(t, err)

err = SafeFileRotate(filepath.Join(tempdir, "registry"),
filepath.Join(tempdir, "registry.new"))
assert.NoError(t, err)

contents, err = os.ReadFile(filepath.Join(tempdir, "registry"))
assert.NoError(t, err)
assert.Equal(t, []byte("new filebeat 1"), contents)

// and again for good measure

err = os.WriteFile(filepath.Join(tempdir, "registry.new"),
[]byte("new filebeat 2"), 0x777)
assert.NoError(t, err)

err = SafeFileRotate(filepath.Join(tempdir, "registry"),
filepath.Join(tempdir, "registry.new"))
assert.NoError(t, err)

contents, err = os.ReadFile(filepath.Join(tempdir, "registry"))
assert.NoError(t, err)
assert.Equal(t, []byte("new filebeat 2"), contents)
// do it twice to make sure we deal with deleting the old file
for i := 0; i < 2; i++ {
expectedContents := []byte(fmt.Sprintf("new filebeat %d", i))
err = os.WriteFile(filepath.Join(tempdir, "registry.new"),
expectedContents, 0x777)
assert.NoError(t, err)

err = SafeFileRotate(filepath.Join(tempdir, "registry"),
filepath.Join(tempdir, "registry.new"))
assert.NoError(t, err)

contents, err = os.ReadFile(filepath.Join(tempdir, "registry"))
assert.NoError(t, err)
assert.Equal(t, expectedContents, contents)
}
}

0 comments on commit 419d269

Please sign in to comment.