mirror of https://github.com/databricks/cli.git
Fix bundle git branch validation (#645)
## Changes This PR: 1. Fixes the computation logic for `ActualBranch`. An error in the earlier logic caused the validation mutator to be a no-op. 2. Makes the `.git` string a global var. This is useful to configure in tests. 3. Adds e2e test for the validation mutator. ## Tests Unit test
This commit is contained in:
parent
81ee031a04
commit
d6f626912f
|
@ -24,17 +24,20 @@ func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// load branch name if undefined
|
||||
if b.Config.Bundle.Git.Branch == "" {
|
||||
|
||||
// Read branch name of current checkout
|
||||
branch, err := repo.CurrentBranch()
|
||||
if err != nil {
|
||||
log.Warnf(ctx, "failed to load current branch: %s", err)
|
||||
} else {
|
||||
b.Config.Bundle.Git.Branch = branch
|
||||
if err == nil {
|
||||
b.Config.Bundle.Git.ActualBranch = branch
|
||||
if b.Config.Bundle.Git.Branch == "" {
|
||||
// Only load branch if there's no user defined value
|
||||
b.Config.Bundle.Git.Inferred = true
|
||||
b.Config.Bundle.Git.Branch = branch
|
||||
}
|
||||
} else {
|
||||
log.Warnf(ctx, "failed to load current branch: %s", err)
|
||||
}
|
||||
|
||||
// load commit hash if undefined
|
||||
if b.Config.Bundle.Git.Commit == "" {
|
||||
commit, err := repo.LatestCommit()
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
package config_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAutoLoad(t *testing.T) {
|
||||
b := load(t, "./autoload_git")
|
||||
assert.True(t, b.Config.Bundle.Git.Inferred)
|
||||
assert.Contains(t, b.Config.Bundle.Git.OriginURL, "/cli")
|
||||
}
|
||||
|
||||
func TestManuallySetBranch(t *testing.T) {
|
||||
b := loadEnvironment(t, "./autoload_git", "production")
|
||||
assert.False(t, b.Config.Bundle.Git.Inferred)
|
||||
assert.Equal(t, "main", b.Config.Bundle.Git.Branch)
|
||||
assert.Contains(t, b.Config.Bundle.Git.OriginURL, "/cli")
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
ref: refs/heads/feature-b
|
|
@ -0,0 +1,4 @@
|
|||
bundle:
|
||||
name: "Dancing Feet"
|
||||
git:
|
||||
branch: "feature-a"
|
|
@ -0,0 +1,39 @@
|
|||
package config_tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/databricks/cli/bundle"
|
||||
"github.com/databricks/cli/bundle/config/mutator"
|
||||
"github.com/databricks/cli/libs/git"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGitAutoLoad(t *testing.T) {
|
||||
b := load(t, "./autoload_git")
|
||||
assert.True(t, b.Config.Bundle.Git.Inferred)
|
||||
assert.Contains(t, b.Config.Bundle.Git.OriginURL, "/cli")
|
||||
}
|
||||
|
||||
func TestGitManuallySetBranch(t *testing.T) {
|
||||
b := loadEnvironment(t, "./autoload_git", "production")
|
||||
assert.False(t, b.Config.Bundle.Git.Inferred)
|
||||
assert.Equal(t, "main", b.Config.Bundle.Git.Branch)
|
||||
assert.Contains(t, b.Config.Bundle.Git.OriginURL, "/cli")
|
||||
}
|
||||
|
||||
func TestGitBundleBranchValidation(t *testing.T) {
|
||||
git.GitDirectoryName = ".mock-git"
|
||||
t.Cleanup(func() {
|
||||
git.GitDirectoryName = ".git"
|
||||
})
|
||||
|
||||
b := load(t, "./git_branch_validation")
|
||||
assert.False(t, b.Config.Bundle.Git.Inferred)
|
||||
assert.Equal(t, "feature-a", b.Config.Bundle.Git.Branch)
|
||||
assert.Equal(t, "feature-b", b.Config.Bundle.Git.ActualBranch)
|
||||
|
||||
err := bundle.Apply(context.Background(), b, mutator.ValidateGitDetails())
|
||||
assert.ErrorContains(t, err, "not on the right Git branch:")
|
||||
}
|
|
@ -12,6 +12,8 @@ import (
|
|||
|
||||
const gitIgnoreFileName = ".gitignore"
|
||||
|
||||
var GitDirectoryName = ".git"
|
||||
|
||||
// Repository represents a Git repository or a directory
|
||||
// that could later be initialized as Git repository.
|
||||
type Repository struct {
|
||||
|
@ -45,7 +47,7 @@ func (r *Repository) Root() string {
|
|||
|
||||
func (r *Repository) CurrentBranch() (string, error) {
|
||||
// load .git/HEAD
|
||||
ref, err := LoadReferenceFile(filepath.Join(r.rootPath, ".git", "HEAD"))
|
||||
ref, err := LoadReferenceFile(filepath.Join(r.rootPath, GitDirectoryName, "HEAD"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -62,7 +64,7 @@ func (r *Repository) CurrentBranch() (string, error) {
|
|||
|
||||
func (r *Repository) LatestCommit() (string, error) {
|
||||
// load .git/HEAD
|
||||
ref, err := LoadReferenceFile(filepath.Join(r.rootPath, ".git", "HEAD"))
|
||||
ref, err := LoadReferenceFile(filepath.Join(r.rootPath, GitDirectoryName, "HEAD"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -81,7 +83,7 @@ func (r *Repository) LatestCommit() (string, error) {
|
|||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
branchHeadRef, err := LoadReferenceFile(filepath.Join(r.rootPath, ".git", branchHeadPath))
|
||||
branchHeadRef, err := LoadReferenceFile(filepath.Join(r.rootPath, GitDirectoryName, branchHeadPath))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -186,7 +188,7 @@ func NewRepository(path string) (*Repository, error) {
|
|||
}
|
||||
|
||||
real := true
|
||||
rootPath, err := folders.FindDirWithLeaf(path, ".git")
|
||||
rootPath, err := folders.FindDirWithLeaf(path, GitDirectoryName)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in New Issue