diff --git a/README.md b/README.md index 1844d55c..b728983c 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,11 @@ It automatically detects charts changed against the target branch. It is recommended to use the provided Docker image which can be [found on Quay](https://quay.io/helmpack/chart-testing/). It comes with all necessary tools installed. -* Helm (http://helm.sh) -* yamllint (/~https://github.com/adrienverge/yamllint) -* yamale (/~https://github.com/23andMe/Yamale) -* kubectl (https://kubernetes.io/docs/reference/kubectl/overview/) -* Tooling for your cluster +* [Helm](http://helm.sh) +* [Git](https://git-scm.com) (2.17.0 or later) +* [Yamllint](/~https://github.com/adrienverge/yamllint) +* [Yamale](/~https://github.com/23andMe/Yamale) +* [Kubectl](https://kubernetes.io/docs/reference/kubectl/overview/) ### Binary Distribution diff --git a/pkg/chart/chart.go b/pkg/chart/chart.go index b97aca79..24eca748 100644 --- a/pkg/chart/chart.go +++ b/pkg/chart/chart.go @@ -16,12 +16,12 @@ package chart import ( "fmt" + "io/ioutil" "path/filepath" "strings" - "github.com/helm/chart-testing/pkg/exec" - "github.com/helm/chart-testing/pkg/config" + "github.com/helm/chart-testing/pkg/exec" "github.com/helm/chart-testing/pkg/tool" "github.com/helm/chart-testing/pkg/util" "github.com/pkg/errors" @@ -35,9 +35,9 @@ const maxNameLength = 63 // // Show returns the contents of file on the specified remote/branch. // -// AddWorkingTree checks out the contents of the repository at a commit ref into the specified path. +// AddWorktree checks out the contents of the repository at a commit ref into the specified path. // -// RemoveWorkingTree removes the working tree at the specified path. +// RemoveWorktree removes the working tree at the specified path. // // MergeBase returns the SHA1 of the merge base of commit1 and commit2. // @@ -50,8 +50,8 @@ const maxNameLength = 63 type Git interface { FileExistsOnBranch(file string, remote string, branch string) bool Show(file string, remote string, branch string) (string, error) - AddWorkingTree(path string, ref string) error - RemoveWorkingTree(path string) error + AddWorktree(path string, ref string) error + RemoveWorktree(path string) error MergeBase(commit1 string, commit2 string) (string, error) ListChangedFilesInDirs(commit string, dirs ...string) ([]string, error) GetUrlForRemote(remote string) (string, error) @@ -215,14 +215,15 @@ func NewChart(chartPath string) (*Chart, error) { } type Testing struct { - config config.Configuration - helm Helm - kubectl Kubectl - git Git - linter Linter - accountValidator AccountValidator - directoryLister DirectoryLister - chartUtils ChartUtils + config config.Configuration + helm Helm + kubectl Kubectl + git Git + linter Linter + accountValidator AccountValidator + directoryLister DirectoryLister + chartUtils ChartUtils + previousRevisionWorktree string } // TestResults holds results and overall status @@ -253,12 +254,10 @@ func NewTesting(config config.Configuration) Testing { } } -const ctPreviousRevisionTree = "ct_previous_revision" - // computePreviousRevisionPath converts any file or directory path to the same path in the // previous revision's working tree. -func computePreviousRevisionPath(fileOrDirPath string) string { - return filepath.Join(ctPreviousRevisionTree, fileOrDirPath) +func (t *Testing) computePreviousRevisionPath(fileOrDirPath string) string { + return filepath.Join(t.previousRevisionWorktree, fileOrDirPath) } func (t *Testing) processCharts(action func(chart *Chart) TestResult) ([]TestResult, error) { @@ -324,11 +323,20 @@ func (t *Testing) processCharts(action func(chart *Chart) TestResult) ([]TestRes if err != nil { return results, errors.Wrap(err, "Error identifying merge base") } - t.git.AddWorkingTree(ctPreviousRevisionTree, mergeBase) - defer t.git.RemoveWorkingTree(ctPreviousRevisionTree) + // Add worktree for the target revision + worktreePath, err := ioutil.TempDir("./", "ct_previous_revision") + if err != nil { + return results, errors.Wrap(err, "Could not create previous revision directory") + } + t.previousRevisionWorktree = worktreePath + err = t.git.AddWorktree(worktreePath, mergeBase) + if err != nil { + return results, errors.Wrap(err, "Could not create worktree for previous revision") + } + defer t.git.RemoveWorktree(worktreePath) for _, chart := range charts { - if err := t.helm.BuildDependencies(computePreviousRevisionPath(chart.Path())); err != nil { + if err := t.helm.BuildDependencies(t.computePreviousRevisionPath(chart.Path())); err != nil { // Only print error (don't exit) if building dependencies for previous revision fails. fmt.Println(errors.Wrapf(err, "Error building dependencies for previous revision of chart '%s'\n", chart)) } @@ -491,7 +499,7 @@ func (t *Testing) UpgradeChart(chart *Chart) TestResult { return result } - if oldChart, err := NewChart(computePreviousRevisionPath(chart.Path())); err == nil { + if oldChart, err := NewChart(t.computePreviousRevisionPath(chart.Path())); err == nil { result.Error = t.doUpgrade(oldChart, chart, false) } diff --git a/pkg/chart/chart_test.go b/pkg/chart/chart_test.go index 41637c41..cc9c1e2d 100644 --- a/pkg/chart/chart_test.go +++ b/pkg/chart/chart_test.go @@ -16,8 +16,6 @@ package chart import ( "fmt" - "io/ioutil" - "os" "strings" "testing" @@ -31,17 +29,11 @@ import ( type fakeGit struct{} func (g fakeGit) FileExistsOnBranch(file string, remote string, branch string) bool { - _, err := os.Open(computePreviousRevisionPath(file)) - fmt.Println(err) - return err == nil + return true } func (g fakeGit) Show(file string, remote string, branch string) (string, error) { - b, err := ioutil.ReadFile(computePreviousRevisionPath(file)) - if err != nil { - return "", err - } - return string(b), nil + return "", nil } func (g fakeGit) MergeBase(commit1 string, commit2 string) (string, error) { @@ -59,11 +51,11 @@ func (g fakeGit) ListChangedFilesInDirs(commit string, dirs ...string) ([]string }, nil } -func (g fakeGit) AddWorkingTree(path string, ref string) error { +func (g fakeGit) AddWorktree(path string, ref string) error { return nil } -func (g fakeGit) RemoveWorkingTree(path string) error { +func (g fakeGit) RemoveWorktree(path string) error { return nil } diff --git a/pkg/tool/git.go b/pkg/tool/git.go index e60c825b..8fc2f17e 100644 --- a/pkg/tool/git.go +++ b/pkg/tool/git.go @@ -38,11 +38,11 @@ func (g Git) FileExistsOnBranch(file string, remote string, branch string) bool return err == nil } -func (g Git) AddWorkingTree(path string, ref string) error { +func (g Git) AddWorktree(path string, ref string) error { return g.exec.RunProcess("git", "worktree", "add", path, ref) } -func (g Git) RemoveWorkingTree(path string) error { +func (g Git) RemoveWorktree(path string) error { return g.exec.RunProcess("git", "worktree", "remove", path) }