mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 13:05:36 +03:00
698b963e11
Squashed commit of the following:
commit 6ce649c06398cf8a6f8e1a90f560fa8205f6500e
Merge: 1c6327e5d 996c6b3ee
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Tue Jul 25 17:42:01 2023 +0300
Merge branch 'master' into imp-filter-upd
commit 1c6327e5d4c04393abc5d4d3e4b8568d4c6eca23
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Jul 21 17:32:47 2023 +0300
all: imp code; use renameio/v2 consistently
commit 1669288c9b662d1310f83a4e0d3f1f60731188cd
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Jul 21 16:26:17 2023 +0300
all: add renameioutil; imp flt upd
101 lines
2.1 KiB
Go
101 lines
2.1 KiB
Go
package aghrenameio_test
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghrenameio"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// testPerm is the common permission mode for tests.
|
|
const testPerm fs.FileMode = 0o644
|
|
|
|
// Common file data for tests.
|
|
var (
|
|
initialData = []byte("initial data\n")
|
|
newData = []byte("new data\n")
|
|
)
|
|
|
|
func TestPendingFile(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
targetPath := newInitialFile(t)
|
|
f, err := aghrenameio.NewPendingFile(targetPath, testPerm)
|
|
require.NoError(t, err)
|
|
|
|
_, err = f.Write(newData)
|
|
require.NoError(t, err)
|
|
|
|
err = f.CloseReplace()
|
|
require.NoError(t, err)
|
|
|
|
gotData, err := os.ReadFile(targetPath)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, newData, gotData)
|
|
}
|
|
|
|
// newInitialFile is a test helper that returns the path to the file containing
|
|
// [initialData].
|
|
func newInitialFile(t *testing.T) (targetPath string) {
|
|
t.Helper()
|
|
|
|
dir := t.TempDir()
|
|
targetPath = filepath.Join(dir, "target")
|
|
|
|
err := os.WriteFile(targetPath, initialData, 0o644)
|
|
require.NoError(t, err)
|
|
|
|
return targetPath
|
|
}
|
|
|
|
func TestWithDeferredCleanup(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const testError errors.Error = "test error"
|
|
|
|
testCases := []struct {
|
|
error error
|
|
name string
|
|
wantErrMsg string
|
|
wantData []byte
|
|
}{{
|
|
name: "success",
|
|
error: nil,
|
|
wantErrMsg: "",
|
|
wantData: newData,
|
|
}, {
|
|
name: "error",
|
|
error: testError,
|
|
wantErrMsg: testError.Error(),
|
|
wantData: initialData,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
targetPath := newInitialFile(t)
|
|
f, err := aghrenameio.NewPendingFile(targetPath, testPerm)
|
|
require.NoError(t, err)
|
|
|
|
_, err = f.Write(newData)
|
|
require.NoError(t, err)
|
|
|
|
err = aghrenameio.WithDeferredCleanup(tc.error, f)
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
gotData, err := os.ReadFile(targetPath)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.wantData, gotData)
|
|
})
|
|
}
|
|
}
|