0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-09-28 12:04:28 -04:00

Use github.com/mholt/archives replace github.com/mholt/archiver (#35390)

Fix #32620

---------

Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: junoberryferry <user@example.tld>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
junoberryferry
2025-09-01 15:40:12 -04:00
committed by GitHub
parent 1717af72c4
commit ec75bdbe68
6 changed files with 451 additions and 98 deletions

View File

@@ -4,6 +4,8 @@
package dump
import (
"archive/tar"
"bytes"
"fmt"
"io"
"os"
@@ -14,8 +16,8 @@ import (
"code.gitea.io/gitea/modules/timeutil"
"github.com/mholt/archiver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPrepareFileNameAndType(t *testing.T) {
@@ -67,28 +69,26 @@ func TestIsSubDir(t *testing.T) {
assert.False(t, isSub)
}
type testWriter struct {
added []string
}
func TestDumperIntegration(t *testing.T) {
var buf bytes.Buffer
dumper, err := NewDumper(t.Context(), "zip", &buf)
require.NoError(t, err)
func (t *testWriter) Create(out io.Writer) error {
return nil
}
tmpDir := t.TempDir()
_ = os.WriteFile(filepath.Join(tmpDir, "test.txt"), nil, 0o644)
f, _ := os.Open(filepath.Join(tmpDir, "test.txt"))
func (t *testWriter) Write(f archiver.File) error {
t.added = append(t.added, f.Name())
return nil
}
fi, _ := f.Stat()
err = dumper.AddFileByReader(f, fi, "test.txt")
require.NoError(t, err)
func (t *testWriter) Close() error {
return nil
err = dumper.Close()
require.NoError(t, err)
assert.Positive(t, buf.Len())
}
func TestDumper(t *testing.T) {
sortStrings := func(s []string) []string {
sort.Strings(s)
return s
}
tmpDir := t.TempDir()
_ = os.MkdirAll(filepath.Join(tmpDir, "include/exclude1"), 0o755)
_ = os.MkdirAll(filepath.Join(tmpDir, "include/exclude2"), 0o755)
@@ -98,16 +98,54 @@ func TestDumper(t *testing.T) {
_ = os.WriteFile(filepath.Join(tmpDir, "include/exclude1/a-1"), nil, 0o644)
_ = os.WriteFile(filepath.Join(tmpDir, "include/exclude2/a-2"), nil, 0o644)
tw := &testWriter{}
d := &Dumper{Writer: tw}
d.GlobalExcludeAbsPath(filepath.Join(tmpDir, "include/exclude1"))
err := d.AddRecursiveExclude("include", filepath.Join(tmpDir, "include"), []string{filepath.Join(tmpDir, "include/exclude2")})
assert.NoError(t, err)
assert.Equal(t, sortStrings([]string{"include/a", "include/sub", "include/sub/b"}), sortStrings(tw.added))
sortStrings := func(s []string) []string {
sort.Strings(s)
return s
}
tw = &testWriter{}
d = &Dumper{Writer: tw}
err = d.AddRecursiveExclude("include", filepath.Join(tmpDir, "include"), nil)
assert.NoError(t, err)
assert.Equal(t, sortStrings([]string{"include/exclude2", "include/exclude2/a-2", "include/a", "include/sub", "include/sub/b", "include/exclude1", "include/exclude1/a-1"}), sortStrings(tw.added))
t.Run("IncludesWithExcludes", func(t *testing.T) {
var buf bytes.Buffer
dumper, err := NewDumper(t.Context(), "tar", &buf)
require.NoError(t, err)
dumper.GlobalExcludeAbsPath(filepath.Join(tmpDir, "include/exclude1"))
err = dumper.AddRecursiveExclude("include", filepath.Join(tmpDir, "include"), []string{filepath.Join(tmpDir, "include/exclude2")})
require.NoError(t, err)
err = dumper.Close()
require.NoError(t, err)
files := extractTarFileNames(t, &buf)
expected := []string{"include/a", "include/sub", "include/sub/b"}
assert.Equal(t, sortStrings(expected), sortStrings(files))
})
t.Run("IncludesAll", func(t *testing.T) {
var buf bytes.Buffer
dumper, err := NewDumper(t.Context(), "tar", &buf)
require.NoError(t, err)
err = dumper.AddRecursiveExclude("include", filepath.Join(tmpDir, "include"), nil)
require.NoError(t, err)
err = dumper.Close()
require.NoError(t, err)
files := extractTarFileNames(t, &buf)
expected := []string{
"include/exclude2", "include/exclude2/a-2",
"include/a", "include/sub", "include/sub/b",
"include/exclude1", "include/exclude1/a-1",
}
assert.Equal(t, sortStrings(expected), sortStrings(files))
})
}
func extractTarFileNames(t *testing.T, buf *bytes.Buffer) (fileNames []string) {
tr := tar.NewReader(buf)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
require.NoError(t, err, "Error reading tar archive")
fileNames = append(fileNames, hdr.Name)
}
return fileNames
}