File: vcs_test.go

package info (click to toggle)
golang-github-hashicorp-atlas-go 0.0~git20170808.8261ea0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 448 kB
  • sloc: sh: 262; makefile: 20
file content (262 lines) | stat: -rw-r--r-- 5,853 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package archive

import (
	"bytes"
	"os"
	"os/exec"
	"path/filepath"
	"reflect"
	"runtime"
	"testing"
)

func setupGitFixtures(t *testing.T) (string, func()) {
	testDir := testFixture("archive-git")
	oldName := filepath.Join(testDir, "DOTgit")
	newName := filepath.Join(testDir, ".git")

	cleanup := func() {
		os.Rename(newName, oldName)
		// Windows leaves an empty folder lying around afterward
		if runtime.GOOS == "windows" {
			os.Remove(newName)
		}
	}

	// We call this BEFORE and after each setup for tests that make lower-level
	// calls like runCommand
	cleanup()

	if err := os.Rename(oldName, newName); err != nil {
		t.Fatal(err)
	}

	return testDir, cleanup
}

func TestVCSPreflight(t *testing.T) {
	if !testHasGit {
		t.Skip("git not found")
	}

	testDir, cleanup := setupGitFixtures(t)
	defer cleanup()

	if err := vcsPreflight(testDir); err != nil {
		t.Fatal(err)
	}
}

func TestGitBranch(t *testing.T) {
	if !testHasGit {
		t.Skip("git not found")
	}

	testDir, cleanup := setupGitFixtures(t)
	defer cleanup()

	branch, err := gitBranch(testDir)
	if err != nil {
		t.Fatal(err)
	}

	expected := "master"
	if branch != expected {
		t.Fatalf("expected %q to be %q", branch, expected)
	}
}

func TestGitBranch_detached(t *testing.T) {
	if !testHasGit {
		t.Skip("git not found")
	}

	testDir := testFixture("archive-git")
	oldName := filepath.Join(testDir, "DOTgit")
	newName := filepath.Join(testDir, ".git")
	pwd, err := os.Getwd()
	if err != nil {
		t.Fatalf("err: %#v", err)
	}

	// Copy and then remove the .git dir instead of moving and replacing like
	// other tests, since the checkout below is going to write to the reflog and
	// the index
	runCommand(t, pwd, "cp", "-r", oldName, newName)
	defer runCommand(t, pwd, "rm", "-rf", newName)

	runCommand(t, testDir, "git", "checkout", "--detach")

	branch, err := gitBranch(testDir)
	if err != nil {
		t.Fatal(err)
	}

	if branch != "" {
		t.Fatalf("expected branch to be empty, but it was: %s", branch)
	}
}

func TestGitCommit(t *testing.T) {
	if !testHasGit {
		t.Skip("git not found")
	}

	testDir, cleanup := setupGitFixtures(t)
	defer cleanup()

	commit, err := gitCommit(testDir)
	if err != nil {
		t.Fatal(err)
	}

	expected := "7525d17cbbb56f3253a20903ffddc07c6c935c76"
	if commit != expected {
		t.Fatalf("expected %q to be %q", commit, expected)
	}
}

func TestGitRemotes(t *testing.T) {
	if !testHasGit {
		t.Skip("git not found")
	}

	testDir, cleanup := setupGitFixtures(t)
	defer cleanup()

	remotes, err := gitRemotes(testDir)
	if err != nil {
		t.Fatal(err)
	}

	expected := map[string]string{
		"remote.origin":   "https://github.com/hashicorp/origin.git",
		"remote.upstream": "https://github.com/hashicorp/upstream.git",
	}

	if !reflect.DeepEqual(remotes, expected) {
		t.Fatalf("expected %+v to be %+v", remotes, expected)
	}
}

func TestVCSMetadata_git(t *testing.T) {
	if !testHasGit {
		t.Skip("git not found")
	}

	testDir, cleanup := setupGitFixtures(t)
	defer cleanup()

	metadata, err := vcsMetadata(testDir)
	if err != nil {
		t.Fatal(err)
	}

	expected := map[string]string{
		"branch":          "master",
		"commit":          "7525d17cbbb56f3253a20903ffddc07c6c935c76",
		"remote.origin":   "https://github.com/hashicorp/origin.git",
		"remote.upstream": "https://github.com/hashicorp/upstream.git",
	}

	if !reflect.DeepEqual(metadata, expected) {
		t.Fatalf("expected %+v to be %+v", metadata, expected)
	}
}

func TestVCSMetadata_git_detached(t *testing.T) {
	if !testHasGit {
		t.Skip("git not found")
	}

	testDir := testFixture("archive-git")
	oldName := filepath.Join(testDir, "DOTgit")
	newName := filepath.Join(testDir, ".git")
	pwd, err := os.Getwd()
	if err != nil {
		t.Fatalf("err: %#v", err)
	}

	// Copy and then remove the .git dir instead of moving and replacing like
	// other tests, since the checkout below is going to write to the reflog and
	// the index
	runCommand(t, pwd, "cp", "-r", oldName, newName)
	defer runCommand(t, pwd, "rm", "-rf", newName)

	runCommand(t, testDir, "git", "checkout", "--detach")

	metadata, err := vcsMetadata(testDir)
	if err != nil {
		t.Fatal(err)
	}

	expected := map[string]string{
		"branch":          "",
		"commit":          "7525d17cbbb56f3253a20903ffddc07c6c935c76",
		"remote.origin":   "https://github.com/hashicorp/origin.git",
		"remote.upstream": "https://github.com/hashicorp/upstream.git",
	}

	if !reflect.DeepEqual(metadata, expected) {
		t.Fatalf("expected %+v to be %+v", metadata, expected)
	}
}

func TestVCSPathDetect_git(t *testing.T) {
	testDir, cleanup := setupGitFixtures(t)
	defer cleanup()

	vcs, err := vcsDetect(testDir)
	if err != nil {
		t.Errorf("VCS detection failed")
	}

	if vcs.Name != "git" {
		t.Errorf("Expected to find git; found %s", vcs.Name)
	}
}

func TestVCSPathDetect_git_failure(t *testing.T) {
	_, err := vcsDetect(testFixture("archive-flat"))
	// We expect to get an error because there is no git repo here
	if err == nil {
		t.Errorf("VCS detection failed")
	}
}

func TestVCSPathDetect_hg(t *testing.T) {
	t.Skip()
	vcs, err := vcsDetect(testFixture("archive-hg"))
	if err != nil {
		t.Errorf("VCS detection failed")
	}

	if vcs.Name != "hg" {
		t.Errorf("Expected to find hg; found %s", vcs.Name)
	}
}

func TestVCSPathDetect_hg_absolute(t *testing.T) {
	t.Skip()
	abspath, err := filepath.Abs(testFixture("archive-hg"))
	vcs, err := vcsDetect(abspath)
	if err != nil {
		t.Errorf("VCS detection failed")
	}

	if vcs.Name != "hg" {
		t.Errorf("Expected to find hg; found %s", vcs.Name)
	}
}

func runCommand(t *testing.T, path, command string, args ...string) {
	var stderr, stdout bytes.Buffer
	cmd := exec.Command(command, args...)
	cmd.Dir = path
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	if err := cmd.Run(); err != nil {
		t.Fatalf("error running command: %s\nstdout: %s\nstderr: %s",
			err, stdout.String(), stderr.String())
	}
}