fix(chat): replace exports atomically on Windows
This commit is contained in:
1 parent
7a5582f4f9
commit
26b06fe0ff
4 files changed
+61
-1
No files matched your search
@@ -0,0 +1,13 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
||||
//go:build !windows
|
||||
|
||||
package chat
|
||||
|
||||
import "os"
|
||||
|
||||
func replaceFileAtomically(source, target string) error {
|
||||
return os.Rename(source, target)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
||||
//go:build windows
|
||||
|
||||
package chat
|
||||
|
||||
import "golang.org/x/sys/windows"
|
||||
|
||||
func replaceFileAtomically(source, target string) error {
|
||||
return windows.Rename(source, target)
|
||||
}
|
||||
@@ -45,7 +45,7 @@ var (
|
||||
resourceCopy = io.Copy
|
||||
resourceTempSync = (*os.File).Sync
|
||||
resourceTempClose = (*os.File).Close
|
||||
resourceRename = os.Rename
|
||||
resourceRename = replaceFileAtomically
|
||||
resourceLink = os.Link
|
||||
resourceDownload = downloadResourceAtomically
|
||||
)
|
||||
|
||||
@@ -783,3 +783,37 @@ func TestCrossPlatformCoverageMessageExportFailureBoundaries(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageMessageExportOverwriteReplacesExistingFile(t *testing.T) {
|
||||
resetResourceDownloadHooks(t)
|
||||
base := t.TempDir()
|
||||
resourceGetwd = func() (string, error) { return base, nil }
|
||||
resourceAbs = filepath.Abs
|
||||
resourceEvalSymlinks = filepath.EvalSymlinks
|
||||
resourceLstat = os.Lstat
|
||||
resourceRel = filepath.Rel
|
||||
resourceCreateTemp = os.CreateTemp
|
||||
resourceTempSync = (*os.File).Sync
|
||||
resourceTempClose = (*os.File).Close
|
||||
resourceRename = replaceFileAtomically
|
||||
resourceLink = os.Link
|
||||
|
||||
target := filepath.Join(base, "out.json")
|
||||
if err := os.WriteFile(target, []byte("old\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
relative, size, err := WriteMessageExportJSON(
|
||||
"out.json", true, map[string]any{"value": "new"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := os.ReadFile(target)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const want = "{\n \"value\": \"new\"\n}\n"
|
||||
if relative != "out.json" || size != len(want) || string(data) != want {
|
||||
t.Fatalf("relative=%q size=%d data=%q", relative, size, data)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user