Skip to content

Commit

Permalink
Add git version documentation, rename worktree methods, and handle ex…
Browse files Browse the repository at this point in the history
…isting worktree (#133)

Signed-off-by: Jacob LeGrone <git@jacob.work>
  • Loading branch information
jlegrone authored and unguiculus committed Mar 28, 2019
1 parent ea18dc6 commit b060fc8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 30 additions & 22 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
//
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)
}

Expand Down
16 changes: 4 additions & 12 deletions pkg/chart/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package chart

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

Expand All @@ -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) {
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/tool/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit b060fc8

Please sign in to comment.