2024-03-29 06:55:01 -05:00
|
|
|
// 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"
|
2024-07-30 14:41:10 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-03-29 06:55:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
assert.Empty(t, archiver.addedFiles)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Single file", func(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
err := os.WriteFile(dir+"/example", nil, 0o666)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
|
|
|
|
t.Run("No exclude", func(t *testing.T) {
|
|
|
|
archiver := &mockArchiver{}
|
|
|
|
|
|
|
|
err = addRecursiveExclude(archiver, "", dir, nil, false)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
assert.Len(t, archiver.addedFiles, 1)
|
2024-06-17 10:43:08 -05:00
|
|
|
assert.Contains(t, archiver.addedFiles, "example")
|
2024-03-29 06:55:01 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("With exclude", func(t *testing.T) {
|
|
|
|
archiver := &mockArchiver{}
|
|
|
|
|
|
|
|
err = addRecursiveExclude(archiver, "", dir, []string{dir + "/example"}, false)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
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)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
err = os.WriteFile(dir+"/deep/nested/folder/example", nil, 0o666)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
err = os.WriteFile(dir+"/deep/nested/folder/another-file", nil, 0o666)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
|
|
|
|
t.Run("No exclude", func(t *testing.T) {
|
|
|
|
archiver := &mockArchiver{}
|
|
|
|
|
|
|
|
err = addRecursiveExclude(archiver, "", dir, nil, false)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
assert.Len(t, archiver.addedFiles, 5)
|
2024-06-17 10:43:08 -05:00
|
|
|
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")
|
2024-03-29 06:55:01 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Exclude first directory", func(t *testing.T) {
|
|
|
|
archiver := &mockArchiver{}
|
|
|
|
|
|
|
|
err = addRecursiveExclude(archiver, "", dir, []string{dir + "/deep"}, false)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
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)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
assert.Len(t, archiver.addedFiles, 2)
|
2024-06-17 10:43:08 -05:00
|
|
|
assert.Contains(t, archiver.addedFiles, "deep")
|
|
|
|
assert.Contains(t, archiver.addedFiles, "deep/nested")
|
2024-03-29 06:55:01 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Exclude file", func(t *testing.T) {
|
|
|
|
archiver := &mockArchiver{}
|
|
|
|
|
|
|
|
err = addRecursiveExclude(archiver, "", dir, []string{dir + "/deep/nested/folder/example"}, false)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2024-03-29 06:55:01 -05:00
|
|
|
assert.Len(t, archiver.addedFiles, 4)
|
2024-06-17 10:43:08 -05:00
|
|
|
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")
|
2024-03-29 06:55:01 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|