forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_hg_test.go
143 lines (118 loc) · 3.11 KB
/
get_hg_test.go
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
package getter
import (
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
var testHasHg bool
func init() {
if _, err := exec.LookPath("hg"); err == nil {
testHasHg = true
}
}
func TestHgGetter_impl(t *testing.T) {
var _ Getter = new(HgGetter)
}
func TestHgGetter(t *testing.T) {
if !testHasHg {
t.Log("hg not found, skipping")
t.Skip()
}
g := new(HgGetter)
dst := tempDir(t)
// With a dir that doesn't exist
if err := g.Get(dst, testModuleURL("basic-hg")); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestHgGetter_branch(t *testing.T) {
if !testHasHg {
t.Log("hg not found, skipping")
t.Skip()
}
g := new(HgGetter)
dst := tempDir(t)
url := testModuleURL("basic-hg")
q := url.Query()
q.Add("rev", "test-branch")
url.RawQuery = q.Encode()
if err := g.Get(dst, url); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
mainPath := filepath.Join(dst, "main_branch.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
// Get again should work
if err := g.Get(dst, url); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
mainPath = filepath.Join(dst, "main_branch.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestHgGetter_GetFile(t *testing.T) {
if !testHasHg {
t.Log("hg not found, skipping")
t.Skip()
}
g := new(HgGetter)
dst := tempTestFile(t)
defer os.RemoveAll(filepath.Dir(dst))
// Download
if err := g.GetFile(dst, testModuleURL("basic-hg/foo.txt")); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
if _, err := os.Stat(dst); err != nil {
t.Fatalf("err: %s", err)
}
assertContents(t, dst, "Hello\n")
}
func TestHgGetter_HgArgumentsNotAllowed(t *testing.T) {
if !testHasHg {
t.Log("hg not found, skipping")
t.Skip()
}
g := new(HgGetter)
// If arguments are allowed in the destination, this Get call will fail
dst := "--config=alias.clone=!false"
defer os.RemoveAll(dst)
err := g.Get(dst, testModuleURL("basic-hg"))
if err != nil {
t.Fatalf("Expected no err, got: %s", err)
}
dst = tempDir(t)
// Test arguments passed into the `rev` parameter
// This clone call will fail regardless, but an exit code of 1 indicates
// that the `false` command executed
// We are expecting an hg parse error
err = g.Get(dst, testModuleURL("basic-hg?rev=--config=alias.update=!false"))
if err != nil {
if !strings.Contains(err.Error(), "hg: parse error") {
t.Fatalf("Expected no err, got: %s", err)
}
}
dst = tempDir(t)
// Test arguments passed in the repository URL
// This Get call will fail regardless, but it should fail
// because the repository can't be found.
// Other failures indicate that hg interpretted the argument passed in the URL
err = g.Get(dst, &url.URL{Path: "--config=alias.clone=false"})
if err != nil {
if !strings.Contains(err.Error(), "repository --config=alias.clone=false not found") {
t.Fatalf("Expected no err, got: %s", err)
}
}
}