2022-09-14 15:50:29 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2023-04-12 10:04:10 +00:00
|
|
|
"os"
|
2024-05-30 07:41:50 +00:00
|
|
|
"path"
|
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"
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
"github.com/databricks/cli/libs/vfs"
|
2022-09-14 15:50:29 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
func testFileSetAll(t *testing.T, root string) {
|
|
|
|
fileSet, err := NewFileSet(vfs.MustNew(root))
|
2023-01-27 15:04:58 +00:00
|
|
|
require.NoError(t, err)
|
2023-08-21 07:35:02 +00:00
|
|
|
files, err := fileSet.All()
|
2023-01-27 15:04:58 +00:00
|
|
|
require.NoError(t, err)
|
2023-08-21 07:35:02 +00:00
|
|
|
require.Len(t, files, 3)
|
2024-05-30 07:41:50 +00:00
|
|
|
assert.Equal(t, path.Join("a", "b", "world.txt"), files[0].Relative)
|
|
|
|
assert.Equal(t, path.Join("a", "hello.txt"), files[1].Relative)
|
|
|
|
assert.Equal(t, path.Join("databricks.yml"), files[2].Relative)
|
2023-08-21 07:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileSetListAllInRepo(t *testing.T) {
|
|
|
|
testFileSetAll(t, "./testdata")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileSetListAllInTempDir(t *testing.T) {
|
|
|
|
testFileSetAll(t, copyTestdata(t, "./testdata"))
|
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.
|
2024-05-30 07:41:50 +00:00
|
|
|
fileSet, err := NewFileSet(vfs.MustNew("./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)
|
2023-08-21 07:35:02 +00:00
|
|
|
assert.Len(t, files, 3)
|
2022-09-16 13:13:49 +00:00
|
|
|
}
|
2023-04-12 10:04:10 +00:00
|
|
|
|
2023-08-21 07:35:02 +00:00
|
|
|
func TestFileSetAddsCacheDirToGitIgnore(t *testing.T) {
|
2023-04-12 10:04:10 +00:00
|
|
|
projectDir := t.TempDir()
|
2024-05-30 07:41:50 +00:00
|
|
|
fileSet, err := NewFileSet(vfs.MustNew(projectDir))
|
2023-04-12 10:04:10 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-08-21 07:35:02 +00:00
|
|
|
func TestFileSetDoesNotCacheDirToGitIgnoreIfAlreadyPresent(t *testing.T) {
|
2023-04-12 10:04:10 +00:00
|
|
|
projectDir := t.TempDir()
|
|
|
|
gitIgnorePath := filepath.Join(projectDir, ".gitignore")
|
|
|
|
|
2024-05-30 07:41:50 +00:00
|
|
|
fileSet, err := NewFileSet(vfs.MustNew(projectDir))
|
2023-04-12 10:04:10 +00:00
|
|
|
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"))
|
|
|
|
}
|