File: vcs_git_test.go

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (123 lines) | stat: -rw-r--r-- 2,175 bytes parent folder | download | duplicates (3)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package funcs

import (
	"os"
	"os/exec"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
	"github.com/zclconf/go-cty/cty"
)

var testHasGit bool

func init() {
	if _, err := exec.LookPath("git"); err == nil {
		testHasGit = true
	}
}

func TestVCSGit(t *testing.T) {
	if !testHasGit {
		t.Skip("git not installed")
		return
	}

	cases := []struct {
		Name     string
		Fixture  string
		Subdir   string
		Func     func(*VCSGit, []cty.Value, cty.Type) (cty.Value, error)
		Args     []cty.Value
		Expected cty.Value
		Error    string
	}{
		{
			"hash: HEAD commit",
			"git-commits",
			"",
			(*VCSGit).refHashFunc,
			nil,
			cty.StringVal("380afd697abe993b89bfa08d8dd8724d6a513ba1"),
			"",
		},

		{
			"tag",
			"git-tag",
			"",
			(*VCSGit).refTagFunc,
			nil,
			cty.StringVal("hello"),
			"",
		},

		{
			"tag no tags",
			"git-commits",
			"",
			(*VCSGit).refTagFunc,
			nil,
			cty.StringVal(""),
			"",
		},

		{
			"remote doesn't exist",
			"git-commits",
			"",
			(*VCSGit).remoteUrlFunc,
			[]cty.Value{cty.StringVal("origin")},
			cty.UnknownVal(cty.String),
			"",
		},

		{
			"remote exists",
			"git-remote",
			"",
			(*VCSGit).remoteUrlFunc,
			[]cty.Value{cty.StringVal("origin")},
			cty.StringVal("https://github.com/hashicorp/example.git"),
			"",
		},
	}

	for _, tt := range cases {
		t.Run(tt.Name, func(t *testing.T) {
			require := require.New(t)

			path := filepath.Join("testdata", tt.Fixture)
			testGitFixture(t, path)
			if tt.Subdir != "" {
				path = filepath.Join(path, tt.Subdir)
			}

			s := &VCSGit{Path: path}
			result, err := tt.Func(s, tt.Args, cty.String)
			if tt.Error != "" {
				require.Error(err)
				require.Contains(err.Error(), tt.Error)
				return
			}

			require.NoError(err)
			require.True(tt.Expected.RawEquals(result), result.GoString())
		})
	}
}

func testGitFixture(t *testing.T, path string) {
	t.Helper()

	// Look for a DOTgit
	original := filepath.Join(path, "DOTgit")
	_, err := os.Stat(original)
	require.NoError(t, err)

	// Rename it
	newPath := filepath.Join(path, ".git")
	require.NoError(t, os.Rename(original, newPath))
	t.Cleanup(func() { os.Rename(newPath, original) })
}