fix(export): pack --zip before removing the outputs (#663)

CompressDir deleted each file as it was added and wrote the zip last, so a failure lost the output,
and it packed and deleted every file in -d. The writer now packs only the files it wrote, renames
the zip into place, and only then removes them.
This commit is contained in:
Roger authored and GitHub committed 2026-10-28 17:05:28 +08:00
1 parent 5f7bfbd455
commit 8c9c9901a1
9 files changed
+198 -164

No files matched your search

+1 -75
View File
@@ -1,12 +1,6 @@
package fileutil
import (
"archive/zip"
"bytes"
"fmt"
"os"
"path/filepath"
)
import "os"
// FileExists checks if the file exists in the provided path.
func FileExists(filename string) bool {
@@ -19,71 +13,3 @@ func FileExists(filename string) bool {
}
return !info.IsDir()
}
// CompressDir compresses the directory into a zip file
func CompressDir(dir string) error {
files, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("read dir error: %w", err)
}
if len(files) == 0 {
return fmt.Errorf("no files to compress in: %s", dir)
}
buffer := new(bytes.Buffer)
zipWriter := zip.NewWriter(buffer)
defer func() {
_ = zipWriter.Close()
}()
for _, file := range files {
if err := addFileToZip(zipWriter, filepath.Join(dir, file.Name())); err != nil {
return fmt.Errorf("failed to add file to zip: %w", err)
}
}
if err := zipWriter.Close(); err != nil {
return fmt.Errorf("error closing zip writer: %w", err)
}
zipFilename := filepath.Join(dir, filepath.Base(dir)+".zip")
return writeFile(buffer, zipFilename)
}
func addFileToZip(zw *zip.Writer, filename string) error {
content, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("error reading file %s: %w", filename, err)
}
fw, err := zw.Create(filepath.Base(filename))
if err != nil {
return fmt.Errorf("error creating zip entry for %s: %w", filename, err)
}
if _, err = fw.Write(content); err != nil {
return fmt.Errorf("error writing content to zip for %s: %w", filename, err)
}
if err = os.Remove(filename); err != nil {
return fmt.Errorf("error removing original file %s: %w", filename, err)
}
return nil
}
func writeFile(buffer *bytes.Buffer, filename string) error {
outFile, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating output file %s: %w", filename, err)
}
defer func() {
_ = outFile.Close()
}()
if _, err = buffer.WriteTo(outFile); err != nil {
return fmt.Errorf("error writing data to file %s: %w", filename, err)
}
return nil
}
-52
View File
@@ -1,52 +0,0 @@
package fileutil
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupTestDir(t *testing.T, files []string) string {
t.Helper() // Marks the function as a helper function.
tempDir, err := os.MkdirTemp("", "testCompressDir")
require.NoError(t, err, "failed to create a temporary directory")
for _, file := range files {
filePath := filepath.Join(tempDir, file)
err := os.WriteFile(filePath, []byte("test content"), 0o644)
require.NoError(t, err, "failed to create a test file")
}
return tempDir
}
func TestCompressDir(t *testing.T) {
t.Run("Normal Operation", func(t *testing.T) {
tempDir := setupTestDir(t, []string{"file1.txt", "file2.txt", "file3.txt"})
defer os.RemoveAll(tempDir)
err := CompressDir(tempDir)
require.NoError(t, err, "compressDir should not return an error")
// Check if the zip file exists
zipFile := filepath.Join(tempDir, filepath.Base(tempDir)+".zip")
assert.FileExists(t, zipFile, "zip file should be created")
})
t.Run("Directory Does Not Exist", func(t *testing.T) {
err := CompressDir("/path/to/nonexistent/directory")
require.Error(t, err, "should return an error for non-existent directory")
})
t.Run("Empty Directory", func(t *testing.T) {
tempDir, err := os.MkdirTemp("", "testEmptyDir")
require.NoError(t, err, "failed to create empty test directory")
defer os.RemoveAll(tempDir)
err = CompressDir(tempDir)
require.Error(t, err, "should return an error for an empty directory")
})
}