-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetter_test.go
108 lines (77 loc) · 2.35 KB
/
getter_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
package hclconfig
import (
"fmt"
"os"
"path"
"testing"
"github.com/stretchr/testify/require"
)
type getterCall struct {
src string
dest string
working string
}
func setupMockGetter(t *testing.T, err error) (Getter, *[]getterCall) {
calls := &[]getterCall{}
g := &GoGetter{
get: func(src, dest, working string) error {
*calls = append(*calls, getterCall{
src: src,
dest: dest,
working: working,
})
return err
},
}
return g, calls
}
func TestGetterDoesNothingWhenFolderExistsAndIgnoreCacheFalse(t *testing.T) {
dest := t.TempDir()
downloadPath := path.Join(dest, "github.com_test")
os.MkdirAll(downloadPath, os.ModePerm)
g, calls := setupMockGetter(t, nil)
_, err := g.Get("github.com/test", dest, false)
require.NoError(t, err)
require.Len(t, *calls, 0)
}
func TestGetterCallsGetWhenFolderExistsAndIgnoreCacheTrue(t *testing.T) {
dest := t.TempDir()
g, calls := setupMockGetter(t, nil)
_, err := g.Get("github.com/test", dest, true)
require.NoError(t, err)
require.Len(t, *calls, 1)
}
func TestGetterCallsGetWithURLEncodedOutputFolder(t *testing.T) {
g, calls := setupMockGetter(t, nil)
_, err := g.Get("github.com/jumppad-labs/hclconfig?ref=7271da1cd14778d3762304954d7061cc753da204", "/mycache", false)
require.NoError(t, err)
require.Len(t, *calls, 1)
require.Equal(t, "/mycache/github.com_jumppad-labs_hclconfig_ref=7271da1cd14778d3762304954d7061cc753da204", (*calls)[0].dest)
}
func TestGetterReturnsFullDownloadPath(t *testing.T) {
dest := t.TempDir()
downloadPath := path.Join(dest, "github.com_test")
g, calls := setupMockGetter(t, nil)
path, err := g.Get("github.com/test", dest, true)
require.NoError(t, err)
require.Len(t, *calls, 1)
require.Equal(t, downloadPath, path)
}
func TestGetterReturnsErrorWhenUnableToDownload(t *testing.T) {
dest := t.TempDir()
g, calls := setupMockGetter(t, fmt.Errorf("unable to download"))
_, err := g.Get("github.com/test", dest, true)
require.Error(t, err)
require.Len(t, *calls, 1)
}
func TestGetterFunctionalTest(t *testing.T) {
dest := t.TempDir()
if os.Getenv("ACC_TEST") != "1" {
return
}
g := NewGoGetter()
download, err := g.Get("github.com/jumppad-labs/hclconfig?ref=7271da1cd14778d3762304954d7061cc753da204", dest, false)
require.NoError(t, err)
require.DirExists(t, download)
require.FileExists(t, path.Join(download, "README.md"))
}