Files
Roger 2e7ee3c69f test: tidy misplaced, stale, and redundant tests (#691)
Tests now sit beside the code they cover; ones a golden or another test already pins are gone, and core tests gain the assertions a real regression would trip.
2026-11-09 17:25:37 +08:00

82 lines
2.8 KiB
Go

package snapshot
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCopyPath_SkipsEveryEntryEndingInLock(t *testing.T) {
from := t.TempDir()
for _, name := range []string{"LOCK", "db.lock", "Block", "LOCKED", "000003.log"} {
writeFile(t, filepath.Join(from, name), []byte(name))
}
writeFile(t, filepath.Join(from, "unlock", "inner"), []byte("inner"))
into := filepath.Join(t.TempDir(), "leveldb")
require.NoError(t, copyPath(from, into, true))
for _, skipped := range []string{"LOCK", "db.lock", "Block"} {
assert.NoFileExists(t, filepath.Join(into, skipped))
}
assert.NoDirExists(t, filepath.Join(into, "unlock"), "a directory whose name ends in lock is skipped whole")
assert.FileExists(t, filepath.Join(into, "LOCKED"))
assert.FileExists(t, filepath.Join(into, "000003.log"))
}
func TestCopyPath_MissingSourceFails(t *testing.T) {
dir := t.TempDir()
require.Error(t, copyPath(filepath.Join(dir, "missing"), filepath.Join(dir, "file"), false))
require.Error(t, copyPath(filepath.Join(dir, "missing"), filepath.Join(dir, "dir"), true))
}
func TestCopyPath_FileWithoutSidecarsCopiesOnlyTheFile(t *testing.T) {
from := filepath.Join(t.TempDir(), "History")
writeFile(t, from, []byte("history"))
into := filepath.Join(t.TempDir(), "History")
require.NoError(t, copyPath(from, into, false))
got, err := os.ReadFile(into)
require.NoError(t, err)
assert.Equal(t, "history", string(got))
assert.NoFileExists(t, into+"-wal")
assert.NoFileExists(t, into+"-shm")
}
func TestCopyPath_SymlinkInADirectoryStaysALink(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("creating symlinks needs extra privileges on Windows")
}
from := t.TempDir()
writeFile(t, filepath.Join(from, "target"), []byte("target"))
require.NoError(t, os.Symlink("target", filepath.Join(from, "link")))
into := filepath.Join(t.TempDir(), "copy")
require.NoError(t, copyPath(from, into, true))
got, err := os.Readlink(filepath.Join(into, "link"))
require.NoError(t, err)
assert.Equal(t, "target", got)
}
// TestCopyChildren_VanishedDirectoryCopiesAsEmpty copies a directory removed mid-copy as empty, not failing the tree.
func TestCopyChildren_VanishedDirectoryCopiesAsEmpty(t *testing.T) {
into := filepath.Join(t.TempDir(), "Origins")
require.NoError(t, copyChildren(filepath.Join(t.TempDir(), "gone"), into))
entries, err := os.ReadDir(into)
require.NoError(t, err)
assert.Empty(t, entries)
}
// TestCopyRegular_VanishedFileIsSkipped covers a live LevelDB, which deletes files while its directory is copied.
func TestCopyRegular_VanishedFileIsSkipped(t *testing.T) {
dir := t.TempDir()
require.NoError(t, copyRegular(filepath.Join(dir, "000005.ldb"), filepath.Join(dir, "copy")))
assert.NoFileExists(t, filepath.Join(dir, "copy"))
}