Skip to content

Commit b060fc8

Browse files
jlegroneunguiculus
authored andcommitted
Add git version documentation, rename worktree methods, and handle existing worktree (#133)
Signed-off-by: Jacob LeGrone <[email protected]>
1 parent ea18dc6 commit b060fc8

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ It automatically detects charts changed against the target branch.
1515
It is recommended to use the provided Docker image which can be [found on Quay](https://quay.io/helmpack/chart-testing/).
1616
It comes with all necessary tools installed.
1717

18-
* Helm (http://helm.sh)
19-
* yamllint (https://github.com/adrienverge/yamllint)
20-
* yamale (https://github.com/23andMe/Yamale)
21-
* kubectl (https://kubernetes.io/docs/reference/kubectl/overview/)
22-
* Tooling for your cluster
18+
* [Helm](http://helm.sh)
19+
* [Git](https://git-scm.com) (2.17.0 or later)
20+
* [Yamllint](https://github.com/adrienverge/yamllint)
21+
* [Yamale](https://github.com/23andMe/Yamale)
22+
* [Kubectl](https://kubernetes.io/docs/reference/kubectl/overview/)
2323

2424
### Binary Distribution
2525

pkg/chart/chart.go

+30-22
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ package chart
1616

1717
import (
1818
"fmt"
19+
"io/ioutil"
1920
"path/filepath"
2021
"strings"
2122

22-
"github.com/helm/chart-testing/pkg/exec"
23-
2423
"github.com/helm/chart-testing/pkg/config"
24+
"github.com/helm/chart-testing/pkg/exec"
2525
"github.com/helm/chart-testing/pkg/tool"
2626
"github.com/helm/chart-testing/pkg/util"
2727
"github.com/pkg/errors"
@@ -35,9 +35,9 @@ const maxNameLength = 63
3535
//
3636
// Show returns the contents of file on the specified remote/branch.
3737
//
38-
// AddWorkingTree checks out the contents of the repository at a commit ref into the specified path.
38+
// AddWorktree checks out the contents of the repository at a commit ref into the specified path.
3939
//
40-
// RemoveWorkingTree removes the working tree at the specified path.
40+
// RemoveWorktree removes the working tree at the specified path.
4141
//
4242
// MergeBase returns the SHA1 of the merge base of commit1 and commit2.
4343
//
@@ -50,8 +50,8 @@ const maxNameLength = 63
5050
type Git interface {
5151
FileExistsOnBranch(file string, remote string, branch string) bool
5252
Show(file string, remote string, branch string) (string, error)
53-
AddWorkingTree(path string, ref string) error
54-
RemoveWorkingTree(path string) error
53+
AddWorktree(path string, ref string) error
54+
RemoveWorktree(path string) error
5555
MergeBase(commit1 string, commit2 string) (string, error)
5656
ListChangedFilesInDirs(commit string, dirs ...string) ([]string, error)
5757
GetUrlForRemote(remote string) (string, error)
@@ -215,14 +215,15 @@ func NewChart(chartPath string) (*Chart, error) {
215215
}
216216

217217
type Testing struct {
218-
config config.Configuration
219-
helm Helm
220-
kubectl Kubectl
221-
git Git
222-
linter Linter
223-
accountValidator AccountValidator
224-
directoryLister DirectoryLister
225-
chartUtils ChartUtils
218+
config config.Configuration
219+
helm Helm
220+
kubectl Kubectl
221+
git Git
222+
linter Linter
223+
accountValidator AccountValidator
224+
directoryLister DirectoryLister
225+
chartUtils ChartUtils
226+
previousRevisionWorktree string
226227
}
227228

228229
// TestResults holds results and overall status
@@ -253,12 +254,10 @@ func NewTesting(config config.Configuration) Testing {
253254
}
254255
}
255256

256-
const ctPreviousRevisionTree = "ct_previous_revision"
257-
258257
// computePreviousRevisionPath converts any file or directory path to the same path in the
259258
// previous revision's working tree.
260-
func computePreviousRevisionPath(fileOrDirPath string) string {
261-
return filepath.Join(ctPreviousRevisionTree, fileOrDirPath)
259+
func (t *Testing) computePreviousRevisionPath(fileOrDirPath string) string {
260+
return filepath.Join(t.previousRevisionWorktree, fileOrDirPath)
262261
}
263262

264263
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
324323
if err != nil {
325324
return results, errors.Wrap(err, "Error identifying merge base")
326325
}
327-
t.git.AddWorkingTree(ctPreviousRevisionTree, mergeBase)
328-
defer t.git.RemoveWorkingTree(ctPreviousRevisionTree)
326+
// Add worktree for the target revision
327+
worktreePath, err := ioutil.TempDir("./", "ct_previous_revision")
328+
if err != nil {
329+
return results, errors.Wrap(err, "Could not create previous revision directory")
330+
}
331+
t.previousRevisionWorktree = worktreePath
332+
err = t.git.AddWorktree(worktreePath, mergeBase)
333+
if err != nil {
334+
return results, errors.Wrap(err, "Could not create worktree for previous revision")
335+
}
336+
defer t.git.RemoveWorktree(worktreePath)
329337

330338
for _, chart := range charts {
331-
if err := t.helm.BuildDependencies(computePreviousRevisionPath(chart.Path())); err != nil {
339+
if err := t.helm.BuildDependencies(t.computePreviousRevisionPath(chart.Path())); err != nil {
332340
// Only print error (don't exit) if building dependencies for previous revision fails.
333341
fmt.Println(errors.Wrapf(err, "Error building dependencies for previous revision of chart '%s'\n", chart))
334342
}
@@ -491,7 +499,7 @@ func (t *Testing) UpgradeChart(chart *Chart) TestResult {
491499
return result
492500
}
493501

494-
if oldChart, err := NewChart(computePreviousRevisionPath(chart.Path())); err == nil {
502+
if oldChart, err := NewChart(t.computePreviousRevisionPath(chart.Path())); err == nil {
495503
result.Error = t.doUpgrade(oldChart, chart, false)
496504
}
497505

pkg/chart/chart_test.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package chart
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
20-
"os"
2119
"strings"
2220
"testing"
2321

@@ -31,17 +29,11 @@ import (
3129
type fakeGit struct{}
3230

3331
func (g fakeGit) FileExistsOnBranch(file string, remote string, branch string) bool {
34-
_, err := os.Open(computePreviousRevisionPath(file))
35-
fmt.Println(err)
36-
return err == nil
32+
return true
3733
}
3834

3935
func (g fakeGit) Show(file string, remote string, branch string) (string, error) {
40-
b, err := ioutil.ReadFile(computePreviousRevisionPath(file))
41-
if err != nil {
42-
return "", err
43-
}
44-
return string(b), nil
36+
return "", nil
4537
}
4638

4739
func (g fakeGit) MergeBase(commit1 string, commit2 string) (string, error) {
@@ -59,11 +51,11 @@ func (g fakeGit) ListChangedFilesInDirs(commit string, dirs ...string) ([]string
5951
}, nil
6052
}
6153

62-
func (g fakeGit) AddWorkingTree(path string, ref string) error {
54+
func (g fakeGit) AddWorktree(path string, ref string) error {
6355
return nil
6456
}
6557

66-
func (g fakeGit) RemoveWorkingTree(path string) error {
58+
func (g fakeGit) RemoveWorktree(path string) error {
6759
return nil
6860
}
6961

pkg/tool/git.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ func (g Git) FileExistsOnBranch(file string, remote string, branch string) bool
3838
return err == nil
3939
}
4040

41-
func (g Git) AddWorkingTree(path string, ref string) error {
41+
func (g Git) AddWorktree(path string, ref string) error {
4242
return g.exec.RunProcess("git", "worktree", "add", path, ref)
4343
}
4444

45-
func (g Git) RemoveWorkingTree(path string) error {
45+
func (g Git) RemoveWorktree(path string) error {
4646
return g.exec.RunProcess("git", "worktree", "remove", path)
4747
}
4848

0 commit comments

Comments
 (0)