summaryrefslogtreecommitdiffstats
path: root/pkg/integration/components/git.go
blob: f11c3342cfe409902ebe1e98614a8f1a9a40ada3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package components

import (
	"fmt"
	"strings"
)

type Git struct {
	*assertionHelper
	shell *Shell
}

func (self *Git) CurrentBranchName(expectedName string) *Git {
	return self.assert([]string{"git", "rev-parse", "--abbrev-ref", "HEAD"}, expectedName)
}

func (self *Git) TagNamesAt(ref string, expectedNames []string) *Git {
	return self.assert([]string{"git", "tag", "--sort=v:refname", "--points-at", ref}, strings.Join(expectedNames, "\n"))
}

func (self *Git) assert(cmdArgs []string, expected string) *Git {
	self.assertWithRetries(func() (bool, string) {
		output, err := self.shell.runCommandWithOutput(cmdArgs)
		if err != nil {
			return false, fmt.Sprintf("Unexpected error running command: `%v`. Error: %s", cmdArgs, err.Error())
		}
		actual := strings.TrimSpace(output)
		return actual == expected, fmt.Sprintf("Expected current branch name to be '%s', but got '%s'", expected, actual)
	})

	return self
}