summaryrefslogtreecommitdiffstats
path: root/cmd/dump_test.go
blob: 459386318f37890ea8f5b913059d104ecf686de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package cmd

import (
	"io"
	"os"
	"testing"

	"github.com/mholt/archiver/v3"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type mockArchiver struct {
	addedFiles []string
}

func (mockArchiver) Create(out io.Writer) error {
	return nil
}

func (m *mockArchiver) Write(f archiver.File) error {
	m.addedFiles = append(m.addedFiles, f.Name())
	return nil
}

func (mockArchiver) Close() error {
	return nil
}

func TestAddRecursiveExclude(t *testing.T) {
	t.Run("Empty", func(t *testing.T) {
		dir := t.TempDir()
		archiver := &mockArchiver{}

		err := addRecursiveExclude(archiver, "", dir, []string{}, false)
		require.NoError(t, err)
		assert.Empty(t, archiver.addedFiles)
	})

	t.Run("Single file", func(t *testing.T) {
		dir := t.TempDir()
		err := os.WriteFile(dir+"/example", nil, 0o666)
		require.NoError(t, err)

		t.Run("No exclude", func(t *testing.T) {
			archiver := &mockArchiver{}

			err = addRecursiveExclude(archiver, "", dir, nil, false)
			require.NoError(t, err)
			assert.Len(t, archiver.addedFiles, 1)
			assert.Contains(t, archiver.addedFiles, "example")
		})

		t.Run("With exclude", func(t *testing.T) {
			archiver := &mockArchiver{}

			err = addRecursiveExclude(archiver, "", dir, []string{dir + "/example"}, false)
			require.NoError(t, err)
			assert.Empty(t, archiver.addedFiles)
		})
	})

	t.Run("File inside directory", func(t *testing.T) {
		dir := t.TempDir()
		err := os.MkdirAll(dir+"/deep/nested/folder", 0o750)
		require.NoError(t, err)
		err = os.WriteFile(dir+"/deep/nested/folder/example", nil, 0o666)
		require.NoError(t, err)
		err = os.WriteFile(dir+"/deep/nested/folder/another-file", nil, 0o666)
		require.NoError(t, err)

		t.Run("No exclude", func(t *testing.T) {
			archiver := &mockArchiver{}

			err = addRecursiveExclude(archiver, "", dir, nil, false)
			require.NoError(t, err)
			assert.Len(t, archiver.addedFiles, 5)
			assert.Contains(t, archiver.addedFiles, "deep")
			assert.Contains(t, archiver.addedFiles, "deep/nested")
			assert.Contains(t, archiver.addedFiles, "deep/nested/folder")
			assert.Contains(t, archiver.addedFiles, "deep/nested/folder/example")
			assert.Contains(t, archiver.addedFiles, "deep/nested/folder/another-file")
		})

		t.Run("Exclude first directory", func(t *testing.T) {
			archiver := &mockArchiver{}

			err = addRecursiveExclude(archiver, "", dir, []string{dir + "/deep"}, false)
			require.NoError(t, err)
			assert.Empty(t, archiver.addedFiles)
		})

		t.Run("Exclude nested directory", func(t *testing.T) {
			archiver := &mockArchiver{}

			err = addRecursiveExclude(archiver, "", dir, []string{dir + "/deep/nested/folder"}, false)
			require.NoError(t, err)
			assert.Len(t, archiver.addedFiles, 2)
			assert.Contains(t, archiver.addedFiles, "deep")
			assert.Contains(t, archiver.addedFiles, "deep/nested")
		})

		t.Run("Exclude file", func(t *testing.T) {
			archiver := &mockArchiver{}

			err = addRecursiveExclude(archiver, "", dir, []string{dir + "/deep/nested/folder/example"}, false)
			require.NoError(t, err)
			assert.Len(t, archiver.addedFiles, 4)
			assert.Contains(t, archiver.addedFiles, "deep")
			assert.Contains(t, archiver.addedFiles, "deep/nested")
			assert.Contains(t, archiver.addedFiles, "deep/nested/folder")
			assert.Contains(t, archiver.addedFiles, "deep/nested/folder/another-file")
		})
	})
}