File: testutil.go

package info (click to toggle)
docker-buildx 0.19.3%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,852 kB
  • sloc: sh: 318; makefile: 73
file content (94 lines) | stat: -rw-r--r-- 2,277 bytes parent folder | download | duplicates (2)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gitutil

import (
	"os"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

func GitInit(c *Git, tb testing.TB) {
	tb.Helper()
	out, err := fakeGit(c, "init")
	require.NoError(tb, err)
	require.Contains(tb, out, "Initialized empty Git repository")
	require.NoError(tb, err)
	GitCheckoutBranch(c, tb, "main")
	_, _ = fakeGit(c, "branch", "-D", "master")
}

func GitCommit(c *Git, tb testing.TB, msg string) {
	tb.Helper()
	out, err := fakeGit(c, "commit", "--allow-empty", "-m", msg)
	require.NoError(tb, err)
	require.Contains(tb, out, "main", msg)
}

func GitTag(c *Git, tb testing.TB, tag string) {
	tb.Helper()
	out, err := fakeGit(c, "tag", tag)
	require.NoError(tb, err)
	require.Empty(tb, out)
}

func GitCheckoutBranch(c *Git, tb testing.TB, name string) {
	tb.Helper()
	out, err := fakeGit(c, "checkout", "-b", name)
	require.NoError(tb, err)
	require.Empty(tb, out)
}

func GitAdd(c *Git, tb testing.TB, files ...string) {
	tb.Helper()
	args := append([]string{"add"}, files...)
	_, err := fakeGit(c, args...)
	require.NoError(tb, err)
}

func GitSetRemote(c *Git, tb testing.TB, name string, url string) {
	tb.Helper()
	_, err := fakeGit(c, "remote", "add", name, url)
	require.NoError(tb, err)
}

func GitSetMainUpstream(c *Git, tb testing.TB, remote, target string) {
	tb.Helper()
	_, err := fakeGit(c, "fetch", "--depth", "1", remote, target)
	require.NoError(tb, err)

	_, err = fakeGit(c, "branch", "--set-upstream-to", remote+"/"+target, "main")
	require.NoError(tb, err)
}

func Mktmp(tb testing.TB) string {
	tb.Helper()
	folder := tb.TempDir()
	current, err := os.Getwd()
	require.NoError(tb, err)
	require.NoError(tb, os.Chdir(folder))
	tb.Cleanup(func() {
		require.NoError(tb, os.Chdir(current))
	})
	return folder
}

func fakeGit(c *Git, args ...string) (string, error) {
	allArgs := []string{
		"-c", "user.name=buildx",
		"-c", "user.email=buildx@docker.com",
		"-c", "commit.gpgSign=false",
		"-c", "tag.gpgSign=false",
		"-c", "log.showSignature=false",
	}
	allArgs = append(allArgs, args...)
	return c.clean(c.run(allArgs...))
}

func IsAmbiguousArgument(err error) bool {
	if err == nil {
		return false
	}
	errMsg := strings.ToLower(err.Error())
	return strings.Contains(errMsg, "use '--' to separate paths from revisions")
}