File: 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 (162 lines) | stat: -rw-r--r-- 3,153 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package datasource

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

	"github.com/hashicorp/go-hclog"
	"github.com/stretchr/testify/require"

	"github.com/hashicorp/vagrant-plugin-sdk/terminal"
	"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
)

var testHasGit bool

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

func TestGitSourceOverride(t *testing.T) {
	cases := []struct {
		Name     string
		Input    *vagrant_server.Job_DataSource
		M        map[string]string
		Expected *vagrant_server.Job_DataSource
		Error    string
	}{
		{
			"nothing",
			&vagrant_server.Job_DataSource{
				Source: &vagrant_server.Job_DataSource_Git{
					Git: &vagrant_server.Job_Git{
						Url: "foo",
					},
				},
			},
			nil,
			&vagrant_server.Job_DataSource{
				Source: &vagrant_server.Job_DataSource_Git{
					Git: &vagrant_server.Job_Git{
						Url: "foo",
					},
				},
			},
			"",
		},

		{
			"ref",
			&vagrant_server.Job_DataSource{
				Source: &vagrant_server.Job_DataSource_Git{
					Git: &vagrant_server.Job_Git{
						Url: "foo",
					},
				},
			},
			map[string]string{"ref": "bar"},
			&vagrant_server.Job_DataSource{
				Source: &vagrant_server.Job_DataSource_Git{
					Git: &vagrant_server.Job_Git{
						Url: "foo",
						Ref: "bar",
					},
				},
			},
			"",
		},

		{
			"invalid",
			&vagrant_server.Job_DataSource{
				Source: &vagrant_server.Job_DataSource_Git{
					Git: &vagrant_server.Job_Git{
						Url: "foo",
					},
				},
			},
			map[string]string{"other": "bar"},
			nil,
			"other",
		},
	}

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

			var s GitSource
			err := s.Override(tt.Input, tt.M)
			if tt.Error != "" {
				require.Error(err)
				require.Contains(err.Error(), tt.Error)
				return
			}

			require.NoError(err)
			require.Equal(tt.Expected, tt.Input)
		})
	}
}

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

	require := require.New(t)

	var s GitSource
	dir, closer, err := s.Get(
		context.Background(),
		hclog.L(),
		terminal.ConsoleUI(context.Background()),
		&vagrant_server.Job_DataSource{
			Source: &vagrant_server.Job_DataSource_Git{
				Git: &vagrant_server.Job_Git{
					Url: testGitFixture(t, "git-noop"),
				},
			},
		},
		"",
	)
	require.NoError(err)
	if closer != nil {
		defer closer()
	}

	// Verify files
	_, err = os.Stat(filepath.Join(dir, "vagrant.hcl"))
	require.NoError(err)
}

// testGitFixture MUST be called before TestRunner since TestRunner
// changes our working directory.
func testGitFixture(t *testing.T, n string) string {
	t.Helper()

	// Get our full path
	wd, err := os.Getwd()
	require.NoError(t, err)
	wd, err = filepath.Abs(wd)
	require.NoError(t, err)
	path := filepath.Join(wd, "testdata", n)

	// 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) })

	return path
}