Files
wehub-resource-sync ead81af521
Deploy to GitHub Pages / deploy (push) Failing after 0s
CI / go (push) Has been cancelled
CI / build (darwin, macos-14) (push) Has been cancelled
CI / build (windows, windows-2025) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:31:13 +08:00

74 lines
1.5 KiB
Go

package fileutil
import (
"os"
"path/filepath"
"testing"
)
func TestCopyFile(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "src.txt")
dst := filepath.Join(dir, "dst.txt")
content := []byte("hello world")
if err := os.WriteFile(src, content, 0o644); err != nil {
t.Fatal(err)
}
if err := CopyFile(src, dst); err != nil {
t.Fatalf("CopyFile: %v", err)
}
got, err := os.ReadFile(dst)
if err != nil {
t.Fatalf("reading dst: %v", err)
}
if string(got) != string(content) {
t.Fatalf("got %q, want %q", got, content)
}
}
func TestCopyFileMissingSrc(t *testing.T) {
dir := t.TempDir()
err := CopyFile(filepath.Join(dir, "nonexistent"), filepath.Join(dir, "dst"))
if err == nil {
t.Fatal("expected error for missing source")
}
}
func TestCopyFileOverwrite(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "src.txt")
dst := filepath.Join(dir, "dst.txt")
os.WriteFile(src, []byte("new"), 0o644)
os.WriteFile(dst, []byte("old"), 0o644)
if err := CopyFile(src, dst); err != nil {
t.Fatalf("CopyFile: %v", err)
}
got, _ := os.ReadFile(dst)
if string(got) != "new" {
t.Fatalf("got %q, want %q", got, "new")
}
}
func TestCopyFileEmptyFile(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "empty.txt")
dst := filepath.Join(dir, "dst.txt")
os.WriteFile(src, []byte{}, 0o644)
if err := CopyFile(src, dst); err != nil {
t.Fatalf("CopyFile: %v", err)
}
got, _ := os.ReadFile(dst)
if len(got) != 0 {
t.Fatalf("expected empty file, got %d bytes", len(got))
}
}