Files
cc-connect/core/atomicwrite_test.go
chenhg5andClaude Opus 4.6 00477f25a8 refactor(core): remove redundant fmt.Sprintf and add unit tests
- Remove unnecessary fmt.Sprintf wrapper around i18n.T() calls
- Add comprehensive unit tests for core module components

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 20:56:38 +08:00

74 lines
1.6 KiB
Go

package core
import (
"os"
"path/filepath"
"testing"
)
func TestAtomicWriteFile_Basic(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.txt")
data := []byte("hello world")
if err := AtomicWriteFile(path, data, 0644); err != nil {
t.Fatalf("AtomicWriteFile: %v", err)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
if string(got) != string(data) {
t.Errorf("got %q, want %q", got, data)
}
}
func TestAtomicWriteFile_Overwrite(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.txt")
if err := AtomicWriteFile(path, []byte("first"), 0644); err != nil {
t.Fatalf("first write: %v", err)
}
if err := AtomicWriteFile(path, []byte("second"), 0644); err != nil {
t.Fatalf("second write: %v", err)
}
got, _ := os.ReadFile(path)
if string(got) != "second" {
t.Errorf("got %q, want %q", got, "second")
}
}
func TestAtomicWriteFile_Permissions(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.txt")
if err := AtomicWriteFile(path, []byte("x"), 0600); err != nil {
t.Fatalf("AtomicWriteFile: %v", err)
}
info, err := os.Stat(path)
if err != nil {
t.Fatalf("Stat: %v", err)
}
if perm := info.Mode().Perm(); perm != 0600 {
t.Errorf("perm = %o, want 0600", perm)
}
}
func TestAtomicWriteFile_NoTempLeftOnSuccess(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.txt")
_ = AtomicWriteFile(path, []byte("data"), 0644)
entries, _ := os.ReadDir(dir)
for _, e := range entries {
if e.Name() != "test.txt" {
t.Errorf("unexpected file left: %s", e.Name())
}
}
}