gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package dump
  4. import (
  5. "archive/tar"
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "os"
  10. "path/filepath"
  11. "sort"
  12. "testing"
  13. "time"
  14. "code.gitea.io/gitea/modules/timeutil"
  15. "github.com/stretchr/testify/assert"
  16. "github.com/stretchr/testify/require"
  17. )
  18. func TestPrepareFileNameAndType(t *testing.T) {
  19. defer timeutil.MockSet(time.Unix(1234, 0))()
  20. test := func(argFile, argType, expFile, expType string) {
  21. outFile, outType := PrepareFileNameAndType(argFile, argType)
  22. assert.Equal(t,
  23. fmt.Sprintf("outFile=%s, outType=%s", expFile, expType),
  24. fmt.Sprintf("outFile=%s, outType=%s", outFile, outType),
  25. "argFile=%s, argType=%s", argFile, argType,
  26. )
  27. }
  28. test("", "", "gitea-dump-1234.zip", "zip")
  29. test("", "tar.gz", "gitea-dump-1234.tar.gz", "tar.gz")
  30. test("", "no-such", "", "")
  31. test("-", "", "-", "zip")
  32. test("-", "tar.gz", "-", "tar.gz")
  33. test("-", "no-such", "", "")
  34. test("a", "", "a", "zip")
  35. test("a", "tar.gz", "a", "tar.gz")
  36. test("a", "no-such", "", "")
  37. test("a.zip", "", "a.zip", "zip")
  38. test("a.zip", "tar.gz", "a.zip", "tar.gz")
  39. test("a.zip", "no-such", "", "")
  40. test("a.tar.gz", "", "a.tar.gz", "zip")
  41. test("a.tar.gz", "tar.gz", "a.tar.gz", "tar.gz")
  42. test("a.tar.gz", "no-such", "", "")
  43. }
  44. func TestIsSubDir(t *testing.T) {
  45. tmpDir := t.TempDir()
  46. _ = os.MkdirAll(filepath.Join(tmpDir, "include/sub"), 0o755)
  47. isSub, err := IsSubdir(filepath.Join(tmpDir, "include"), filepath.Join(tmpDir, "include"))
  48. assert.NoError(t, err)
  49. assert.True(t, isSub)
  50. isSub, err = IsSubdir(filepath.Join(tmpDir, "include"), filepath.Join(tmpDir, "include/sub"))
  51. assert.NoError(t, err)
  52. assert.True(t, isSub)
  53. isSub, err = IsSubdir(filepath.Join(tmpDir, "include/sub"), filepath.Join(tmpDir, "include"))
  54. assert.NoError(t, err)
  55. assert.False(t, isSub)
  56. }
  57. func TestDumperIntegration(t *testing.T) {
  58. var buf bytes.Buffer
  59. dumper, err := NewDumper(t.Context(), "zip", &buf)
  60. require.NoError(t, err)
  61. tmpDir := t.TempDir()
  62. _ = os.WriteFile(filepath.Join(tmpDir, "test.txt"), nil, 0o644)
  63. f, _ := os.Open(filepath.Join(tmpDir, "test.txt"))
  64. fi, _ := f.Stat()
  65. err = dumper.AddFileByReader(f, fi, "test.txt")
  66. require.NoError(t, err)
  67. err = dumper.Close()
  68. require.NoError(t, err)
  69. assert.Positive(t, buf.Len())
  70. }
  71. func TestDumper(t *testing.T) {
  72. tmpDir := t.TempDir()
  73. _ = os.MkdirAll(filepath.Join(tmpDir, "include/exclude1"), 0o755)
  74. _ = os.MkdirAll(filepath.Join(tmpDir, "include/exclude2"), 0o755)
  75. _ = os.MkdirAll(filepath.Join(tmpDir, "include/sub"), 0o755)
  76. _ = os.WriteFile(filepath.Join(tmpDir, "include/a"), nil, 0o644)
  77. _ = os.WriteFile(filepath.Join(tmpDir, "include/sub/b"), nil, 0o644)
  78. _ = os.WriteFile(filepath.Join(tmpDir, "include/exclude1/a-1"), nil, 0o644)
  79. _ = os.WriteFile(filepath.Join(tmpDir, "include/exclude2/a-2"), nil, 0o644)
  80. sortStrings := func(s []string) []string {
  81. sort.Strings(s)
  82. return s
  83. }
  84. t.Run("IncludesWithExcludes", func(t *testing.T) {
  85. var buf bytes.Buffer
  86. dumper, err := NewDumper(t.Context(), "tar", &buf)
  87. require.NoError(t, err)
  88. dumper.GlobalExcludeAbsPath(filepath.Join(tmpDir, "include/exclude1"))
  89. err = dumper.AddRecursiveExclude("include", filepath.Join(tmpDir, "include"), []string{filepath.Join(tmpDir, "include/exclude2")})
  90. require.NoError(t, err)
  91. err = dumper.Close()
  92. require.NoError(t, err)
  93. files := extractTarFileNames(t, &buf)
  94. expected := []string{"include/a", "include/sub", "include/sub/b"}
  95. assert.Equal(t, sortStrings(expected), sortStrings(files))
  96. })
  97. t.Run("IncludesAll", func(t *testing.T) {
  98. var buf bytes.Buffer
  99. dumper, err := NewDumper(t.Context(), "tar", &buf)
  100. require.NoError(t, err)
  101. err = dumper.AddRecursiveExclude("include", filepath.Join(tmpDir, "include"), nil)
  102. require.NoError(t, err)
  103. err = dumper.Close()
  104. require.NoError(t, err)
  105. files := extractTarFileNames(t, &buf)
  106. expected := []string{
  107. "include/exclude2", "include/exclude2/a-2",
  108. "include/a", "include/sub", "include/sub/b",
  109. "include/exclude1", "include/exclude1/a-1",
  110. }
  111. assert.Equal(t, sortStrings(expected), sortStrings(files))
  112. })
  113. }
  114. func extractTarFileNames(t *testing.T, buf *bytes.Buffer) (fileNames []string) {
  115. tr := tar.NewReader(buf)
  116. for {
  117. hdr, err := tr.Next()
  118. if err == io.EOF {
  119. break
  120. }
  121. require.NoError(t, err, "Error reading tar archive")
  122. fileNames = append(fileNames, hdr.Name)
  123. }
  124. return fileNames
  125. }