code.gitea.io/gitea@v1.22.3/modules/dump/dumper_test.go (about) 1 // Copyright 2024 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package dump 5 6 import ( 7 "fmt" 8 "io" 9 "os" 10 "path/filepath" 11 "sort" 12 "testing" 13 "time" 14 15 "code.gitea.io/gitea/modules/timeutil" 16 17 "github.com/mholt/archiver/v3" 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func TestPrepareFileNameAndType(t *testing.T) { 22 defer timeutil.MockSet(time.Unix(1234, 0))() 23 test := func(argFile, argType, expFile, expType string) { 24 outFile, outType := PrepareFileNameAndType(argFile, argType) 25 assert.Equal(t, 26 fmt.Sprintf("outFile=%s, outType=%s", expFile, expType), 27 fmt.Sprintf("outFile=%s, outType=%s", outFile, outType), 28 fmt.Sprintf("argFile=%s, argType=%s", argFile, argType), 29 ) 30 } 31 32 test("", "", "gitea-dump-1234.zip", "zip") 33 test("", "tar.gz", "gitea-dump-1234.tar.gz", "tar.gz") 34 test("", "no-such", "", "") 35 36 test("-", "", "-", "zip") 37 test("-", "tar.gz", "-", "tar.gz") 38 test("-", "no-such", "", "") 39 40 test("a", "", "a", "zip") 41 test("a", "tar.gz", "a", "tar.gz") 42 test("a", "no-such", "", "") 43 44 test("a.zip", "", "a.zip", "zip") 45 test("a.zip", "tar.gz", "a.zip", "tar.gz") 46 test("a.zip", "no-such", "", "") 47 48 test("a.tar.gz", "", "a.tar.gz", "zip") 49 test("a.tar.gz", "tar.gz", "a.tar.gz", "tar.gz") 50 test("a.tar.gz", "no-such", "", "") 51 } 52 53 func TestIsSubDir(t *testing.T) { 54 tmpDir := t.TempDir() 55 _ = os.MkdirAll(filepath.Join(tmpDir, "include/sub"), 0o755) 56 57 isSub, err := IsSubdir(filepath.Join(tmpDir, "include"), filepath.Join(tmpDir, "include")) 58 assert.NoError(t, err) 59 assert.True(t, isSub) 60 61 isSub, err = IsSubdir(filepath.Join(tmpDir, "include"), filepath.Join(tmpDir, "include/sub")) 62 assert.NoError(t, err) 63 assert.True(t, isSub) 64 65 isSub, err = IsSubdir(filepath.Join(tmpDir, "include/sub"), filepath.Join(tmpDir, "include")) 66 assert.NoError(t, err) 67 assert.False(t, isSub) 68 } 69 70 type testWriter struct { 71 added []string 72 } 73 74 func (t *testWriter) Create(out io.Writer) error { 75 return nil 76 } 77 78 func (t *testWriter) Write(f archiver.File) error { 79 t.added = append(t.added, f.Name()) 80 return nil 81 } 82 83 func (t *testWriter) Close() error { 84 return nil 85 } 86 87 func TestDumper(t *testing.T) { 88 sortStrings := func(s []string) []string { 89 sort.Strings(s) 90 return s 91 } 92 tmpDir := t.TempDir() 93 _ = os.MkdirAll(filepath.Join(tmpDir, "include/exclude1"), 0o755) 94 _ = os.MkdirAll(filepath.Join(tmpDir, "include/exclude2"), 0o755) 95 _ = os.MkdirAll(filepath.Join(tmpDir, "include/sub"), 0o755) 96 _ = os.WriteFile(filepath.Join(tmpDir, "include/a"), nil, 0o644) 97 _ = os.WriteFile(filepath.Join(tmpDir, "include/sub/b"), nil, 0o644) 98 _ = os.WriteFile(filepath.Join(tmpDir, "include/exclude1/a-1"), nil, 0o644) 99 _ = os.WriteFile(filepath.Join(tmpDir, "include/exclude2/a-2"), nil, 0o644) 100 101 tw := &testWriter{} 102 d := &Dumper{Writer: tw} 103 d.GlobalExcludeAbsPath(filepath.Join(tmpDir, "include/exclude1")) 104 err := d.AddRecursiveExclude("include", filepath.Join(tmpDir, "include"), []string{filepath.Join(tmpDir, "include/exclude2")}) 105 assert.NoError(t, err) 106 assert.EqualValues(t, sortStrings([]string{"include/a", "include/sub", "include/sub/b"}), sortStrings(tw.added)) 107 108 tw = &testWriter{} 109 d = &Dumper{Writer: tw} 110 err = d.AddRecursiveExclude("include", filepath.Join(tmpDir, "include"), nil) 111 assert.NoError(t, err) 112 assert.EqualValues(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)) 113 }