2022-09-14 15:50:29 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2023-04-12 10:04:10 +00:00
|
|
|
"os"
|
2022-09-14 15:50:29 +00:00
|
|
|
"path/filepath"
|
2023-04-12 10:04:10 +00:00
|
|
|
"strings"
|
2022-09-14 15:50:29 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-09-16 13:13:49 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-09-14 15:50:29 +00:00
|
|
|
)
|
|
|
|
|
2023-01-27 15:04:58 +00:00
|
|
|
func TestFileSetRecursiveListFiles(t *testing.T) {
|
|
|
|
fileSet, err := NewFileSet("./testdata")
|
|
|
|
require.NoError(t, err)
|
|
|
|
files, err := fileSet.RecursiveListFiles("./testdata")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, files, 6)
|
|
|
|
assert.Equal(t, filepath.Join(".gitignore"), files[0].Relative)
|
|
|
|
assert.Equal(t, filepath.Join("a", ".gitignore"), files[1].Relative)
|
|
|
|
assert.Equal(t, filepath.Join("a", "b", ".gitignore"), files[2].Relative)
|
|
|
|
assert.Equal(t, filepath.Join("a", "b", "world.txt"), files[3].Relative)
|
|
|
|
assert.Equal(t, filepath.Join("a", "hello.txt"), files[4].Relative)
|
|
|
|
assert.Equal(t, filepath.Join("databricks.yml"), files[5].Relative)
|
2022-09-14 15:50:29 +00:00
|
|
|
}
|
2022-09-16 13:13:49 +00:00
|
|
|
|
|
|
|
func TestFileSetNonCleanRoot(t *testing.T) {
|
|
|
|
// Test what happens if the root directory can be simplified.
|
|
|
|
// Path simplification is done by most filepath functions.
|
2023-01-27 15:04:58 +00:00
|
|
|
// This should yield the same result as above test.
|
|
|
|
fileSet, err := NewFileSet("./testdata/../testdata")
|
2022-09-16 13:13:49 +00:00
|
|
|
require.NoError(t, err)
|
2023-01-27 15:04:58 +00:00
|
|
|
files, err := fileSet.All()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, files, 6)
|
2022-09-16 13:13:49 +00:00
|
|
|
}
|
2023-04-12 10:04:10 +00:00
|
|
|
|
|
|
|
func TestFilesetAddsCacheDirToGitIgnore(t *testing.T) {
|
|
|
|
projectDir := t.TempDir()
|
|
|
|
fileSet, err := NewFileSet(projectDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
fileSet.EnsureValidGitIgnoreExists()
|
|
|
|
|
|
|
|
gitIgnorePath := filepath.Join(projectDir, ".gitignore")
|
|
|
|
assert.FileExists(t, gitIgnorePath)
|
|
|
|
fileBytes, err := os.ReadFile(gitIgnorePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, string(fileBytes), ".databricks")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilesetDoesNotCacheDirToGitIgnoreIfAlreadyPresent(t *testing.T) {
|
|
|
|
projectDir := t.TempDir()
|
|
|
|
gitIgnorePath := filepath.Join(projectDir, ".gitignore")
|
|
|
|
|
|
|
|
fileSet, err := NewFileSet(projectDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = os.WriteFile(gitIgnorePath, []byte(".databricks"), 0o644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fileSet.EnsureValidGitIgnoreExists()
|
|
|
|
|
|
|
|
b, err := os.ReadFile(gitIgnorePath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, strings.Count(string(b), ".databricks"))
|
|
|
|
}
|