Files
dingtalk-workspace-cli/internal/helpers/drive_replace_unix_test.go
chichuan 504db23823 test(drive): cover platform-only branches missed by the platform coverage gate
The platform coverage gate runs only TestAllShortcuts and
TestCrossPlatformCoverage*, so several changed statements had no platform
test exercising them:

- drive_pull.go: the smart-policy re-check that skips publication when the
  target is refreshed in place (same inode) while the download is running.
- drive_pull.go: the post-publish verifyParent failure, where the result is
  already on disk and must not be rolled back.
- drive_replace_unix.go: rename(2) replacement of an existing target; the
  Windows side already had the symmetric test.
- drive_status_windows.go: the filepath.Clean rewrite guard had no input
  reaching it, because isSafeRemoteSegment filters separators upstream.

macOS changed-code coverage: 99.8053% -> 100.0000% (1541 statements).
2026-08-13 20:55:06 +08:00

35 lines
1.0 KiB
Go

//go:build !windows
package helpers
import (
"os"
"path/filepath"
"testing"
)
// Unix 的 rename(2) 原生提供原子替换语义;该路径与 Windows 的
// MOVEFILE_REPLACE_EXISTING 同样服务 pull overwrite、sync remote-wins 与
// keep-both 的占位文件替换,因此两侧都需要平台覆盖。
func TestCrossPlatformCoverageDriveReplaceFileUnix_replacesExistingTarget(t *testing.T) {
dir := t.TempDir()
source := filepath.Join(dir, "source.tmp")
target := filepath.Join(dir, "target.txt")
if err := os.WriteFile(source, []byte("new"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(target, []byte("old"), 0o600); err != nil {
t.Fatal(err)
}
if err := driveReplaceFile(source, target); err != nil {
t.Fatalf("replace existing target: %v", err)
}
if got, err := os.ReadFile(target); err != nil || string(got) != "new" {
t.Fatalf("target = %q, err=%v; want new content", got, err)
}
if _, err := os.Stat(source); !os.IsNotExist(err) {
t.Fatalf("source must be moved, stat err=%v", err)
}
}