Added creation of .gitignore for bricks project with cache dir path

This commit is contained in:
Shreyas Goenka 2022-11-03 21:06:21 +01:00
parent 0601e114fd
commit 1505c63426
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
8 changed files with 92 additions and 31 deletions

View File

@ -22,7 +22,8 @@ func TestDiff(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
defer f2.Close() defer f2.Close()
fileSet := git.NewFileSet(projectDir) fileSet, err := git.NewFileSet(projectDir, false)
assert.NoError(t, err)
files, err := fileSet.All() files, err := fileSet.All()
assert.NoError(t, err) assert.NoError(t, err)
state := Snapshot{ state := Snapshot{

View File

@ -9,6 +9,7 @@ import (
"time" "time"
ignore "github.com/sabhiram/go-gitignore" ignore "github.com/sabhiram/go-gitignore"
"golang.org/x/exp/slices"
) )
type File struct { type File struct {
@ -36,29 +37,39 @@ type FileSet struct {
ignore *ignore.GitIgnore ignore *ignore.GitIgnore
} }
// GetFileSet retrieves FileSet from Git repository checkout root
// or panics if no root is detected.
func GetFileSet() (FileSet, error) {
root, err := Root()
return NewFileSet(root), err
}
// Retuns FileSet for the repository located at `root` // Retuns FileSet for the repository located at `root`
func NewFileSet(root string) FileSet { func NewFileSet(root string, isProjectRoot bool) (*FileSet, error) {
lines := []string{".git", ".bricks"} gitIgnoreMatchers := []string{}
rawIgnore, err := os.ReadFile(fmt.Sprintf("%s/.gitignore", root)) gitIgnorePath := fmt.Sprintf("%s/.gitignore", root)
rawIgnore, err := os.ReadFile(gitIgnorePath)
if err == nil { if err == nil {
// add entries from .gitignore if the file exists (did read correctly) // add entries from .gitignore if the file exists (did read correctly)
for _, line := range strings.Split(string(rawIgnore), "\n") { for _, line := range strings.Split(string(rawIgnore), "\n") {
// underlying library doesn't behave well with Rule 5 of .gitignore, // underlying library doesn't behave well with Rule 5 of .gitignore,
// hence this workaround // hence this workaround
lines = append(lines, strings.Trim(line, "/")) gitIgnoreMatchers = append(gitIgnoreMatchers, strings.Trim(line, "/"))
} }
} }
return FileSet{
root: root, if isProjectRoot && !slices.Contains(gitIgnoreMatchers, ".databricks") {
ignore: ignore.CompileIgnoreLines(lines...), f, err := os.OpenFile(gitIgnorePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
_, err = f.WriteString("\n/.databricks/")
if err != nil {
return nil, err
}
gitIgnoreMatchers = append(gitIgnoreMatchers, ".databricks")
} }
syncIgnoreMatchers := []string{".git"}
return &FileSet{
root: root,
ignore: ignore.CompileIgnoreLines(append(gitIgnoreMatchers, syncIgnoreMatchers...)...),
}, nil
} }
// Return root for fileset. // Return root for fileset.

View File

@ -1,6 +1,7 @@
package git package git
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -26,7 +27,8 @@ func TestRecusiveListFile(t *testing.T) {
// config file is returned // config file is returned
// .gitignore is not because we explictly ignore it in .gitignore // .gitignore is not because we explictly ignore it in .gitignore
fileSet := NewFileSet(projectDir) fileSet, err := NewFileSet(projectDir, false)
assert.NoError(t, err)
files, err := fileSet.RecursiveListFiles(projectDir) files, err := fileSet.RecursiveListFiles(projectDir)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, files, 1) assert.Len(t, files, 1)
@ -63,7 +65,8 @@ func TestFileSetNonCleanRoot(t *testing.T) {
// Path simplification is done by most filepath functions. // Path simplification is done by most filepath functions.
root := "./../git" root := "./../git"
require.NotEqual(t, root, filepath.Clean(root)) require.NotEqual(t, root, filepath.Clean(root))
fs := NewFileSet(root) fs, err := NewFileSet(root, false)
assert.NoError(t, err)
files, err := fs.All() files, err := fs.All()
require.NoError(t, err) require.NoError(t, err)

9
go.mod
View File

@ -13,11 +13,14 @@ require (
github.com/spf13/cobra v1.5.0 // Apache 2.0 github.com/spf13/cobra v1.5.0 // Apache 2.0
github.com/stretchr/testify v1.8.0 // MIT github.com/stretchr/testify v1.8.0 // MIT
github.com/whilp/git-urls v1.0.0 // MIT github.com/whilp/git-urls v1.0.0 // MIT
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // BSD-3-Clause golang.org/x/mod v0.6.0 // BSD-3-Clause
gopkg.in/ini.v1 v1.67.0 // Apache 2.0 gopkg.in/ini.v1 v1.67.0 // Apache 2.0
) )
require golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 require (
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29
)
require ( require (
cloud.google.com/go/compute v1.6.1 // indirect cloud.google.com/go/compute v1.6.1 // indirect
@ -33,7 +36,7 @@ require (
go.opencensus.io v0.23.0 // indirect go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect
golang.org/x/oauth2 v0.0.0-20220628200809-02e64fa58f26 // indirect golang.org/x/oauth2 v0.0.0-20220628200809-02e64fa58f26 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.3.7 // indirect golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/api v0.82.0 // indirect google.golang.org/api v0.82.0 // indirect

9
go.sum
View File

@ -252,6 +252,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE=
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@ -277,8 +279,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o= golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@ -413,8 +415,9 @@ golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View File

@ -67,7 +67,10 @@ func Initialize(ctx context.Context, root, env string) (context.Context, error)
return nil, fmt.Errorf("environment [%s] not defined", env) return nil, fmt.Errorf("environment [%s] not defined", env)
} }
fileSet := git.NewFileSet(root) fileSet, err := git.NewFileSet(root, true)
if err != nil {
return ctx, err
}
p := project{ p := project{
root: root, root: root,
@ -75,7 +78,7 @@ func Initialize(ctx context.Context, root, env string) (context.Context, error)
config: &config, config: &config,
environment: &environment, environment: &environment,
fileSet: &fileSet, fileSet: fileSet,
} }
p.initializeWorkspacesClient(ctx) p.initializeWorkspacesClient(ctx)

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -16,7 +17,7 @@ func TestProjectInitialize(t *testing.T) {
assert.Equal(t, Get(ctx).config.Name, "dev") assert.Equal(t, Get(ctx).config.Name, "dev")
} }
func TestProjectCacheDirErrorsIfNoGitIgnoreFile(t *testing.T) { func TestProjectInitializationCreatesGitIgnoreIfAbsent(t *testing.T) {
// create project root with databricks.yml // create project root with databricks.yml
projectDir := t.TempDir() projectDir := t.TempDir()
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml")) f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
@ -26,29 +27,60 @@ func TestProjectCacheDirErrorsIfNoGitIgnoreFile(t *testing.T) {
ctx, err := Initialize(context.Background(), projectDir, DefaultEnvironment) ctx, err := Initialize(context.Background(), projectDir, DefaultEnvironment)
assert.NoError(t, err) assert.NoError(t, err)
gitIgnorePath := filepath.Join(projectDir, ".gitignore")
assert.FileExists(t, gitIgnorePath)
fileBytes, err := os.ReadFile(gitIgnorePath)
assert.NoError(t, err)
assert.Contains(t, string(fileBytes), ".databricks")
prj := Get(ctx) prj := Get(ctx)
_, err = prj.CacheDir() _, err = prj.CacheDir()
assert.Error(t, err, "please add /.databricks/ to .gitignore") assert.NoError(t, err)
} }
func TestProjectCacheDirErrorsIfGitIgnoreEntryAbsent(t *testing.T) { func TestProjectInitializationAddsCacheDirToGitIgnore(t *testing.T) {
// create project root with databricks.yml // create project root with databricks.yml
projectDir := t.TempDir() projectDir := t.TempDir()
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml")) f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
assert.NoError(t, err) assert.NoError(t, err)
defer f1.Close() defer f1.Close()
// create empty .gitignore gitIgnorePath := filepath.Join(projectDir, ".gitignore")
f2, err := os.Create(filepath.Join(projectDir, ".gitignore")) f2, err := os.Create(gitIgnorePath)
assert.NoError(t, err) assert.NoError(t, err)
defer f2.Close() defer f2.Close()
ctx, err := Initialize(context.Background(), projectDir, DefaultEnvironment) ctx, err := Initialize(context.Background(), projectDir, DefaultEnvironment)
assert.NoError(t, err) assert.NoError(t, err)
fileBytes, err := os.ReadFile(gitIgnorePath)
assert.NoError(t, err)
assert.Contains(t, string(fileBytes), ".databricks")
prj := Get(ctx) prj := Get(ctx)
_, err = prj.CacheDir() _, err = prj.CacheDir()
assert.Error(t, err, "please add /.databricks/ to .gitignore") assert.NoError(t, err)
}
func TestProjectInitializationDoesNotAddCacheDirToGitIgnoreIfAlreadyPresent(t *testing.T) {
// create project root with databricks.yml
projectDir := t.TempDir()
f1, err := os.Create(filepath.Join(projectDir, "databricks.yml"))
assert.NoError(t, err)
defer f1.Close()
gitIgnorePath := filepath.Join(projectDir, ".gitignore")
err = os.WriteFile(gitIgnorePath, []byte("/.databricks/"), 0o644)
assert.NoError(t, err)
_, err = Initialize(context.Background(), projectDir, DefaultEnvironment)
assert.NoError(t, err)
fileBytes, err := os.ReadFile(gitIgnorePath)
assert.NoError(t, err)
assert.Equal(t, 1, strings.Count(string(fileBytes), ".databricks"))
} }
func TestProjectCacheDir(t *testing.T) { func TestProjectCacheDir(t *testing.T) {

5
project/testdata/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/.databricks/
/.databricks/
/.databricks/
/.databricks/
/.databricks/