forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownloader_test.go
119 lines (97 loc) · 3.33 KB
/
downloader_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
package pack
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/buildpack/pack/internal/mocks"
"github.com/onsi/gomega/ghttp"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
h "github.com/buildpack/pack/testhelpers"
)
func TestDownloader(t *testing.T) {
spec.Run(t, "Downloader", testDownloader, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testDownloader(t *testing.T, when spec.G, it spec.S) {
when("#Download", func() {
var (
err error
tmpDir string
cacheDir string
subject *Downloader
)
it.Before(func() {
if runtime.GOOS == "windows" {
t.Skip("do not run on windows")
}
tmpDir, err = ioutil.TempDir("", "")
h.AssertNil(t, err)
cacheDir, err = ioutil.TempDir("", "")
h.AssertNil(t, err)
subject = NewDownloader(mocks.NewMockLogger(ioutil.Discard), cacheDir)
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(tmpDir))
h.AssertNil(t, os.RemoveAll(cacheDir))
})
it("download from a relative directory", func() {
out, err := subject.Download(filepath.Join("testdata", "downloader", "dirA"))
h.AssertNil(t, err)
h.AssertNotEq(t, out, "")
h.AssertDirContainsFileWithContents(t, out, "file.txt", "some file contents")
})
it("download from a relative tgz", func() {
out, err := subject.Download(filepath.Join("testdata", "downloader", "dirA.tgz"))
h.AssertNil(t, err)
h.AssertNotEq(t, out, "")
h.AssertDirContainsFileWithContents(t, out, "file.txt", "some file contents")
})
it("download from an absolute directory", func() {
absPath, err := filepath.Abs(filepath.Join("testdata", "downloader", "dirA"))
h.AssertNil(t, err)
out, err := subject.Download(absPath)
h.AssertNil(t, err)
h.AssertNotEq(t, out, "")
h.AssertDirContainsFileWithContents(t, out, "file.txt", "some file contents")
})
it("download from an absolute tgz", func() {
absPath, err := filepath.Abs(filepath.Join("testdata", "downloader", "dirA.tgz"))
h.AssertNil(t, err)
out, err := subject.Download(absPath)
h.AssertNil(t, err)
h.AssertNotEq(t, out, "")
h.AssertDirContainsFileWithContents(t, out, "file.txt", "some file contents")
})
it("download from a 'file://' URI directory", func() {
absPath, err := filepath.Abs(filepath.Join("testdata", "downloader", "dirA"))
h.AssertNil(t, err)
out, err := subject.Download("file://" + absPath)
h.AssertNil(t, err)
h.AssertNotEq(t, out, "")
h.AssertDirContainsFileWithContents(t, out, "file.txt", "some file contents")
})
it("download from a 'file://' URI tgz", func() {
absPath, err := filepath.Abs(filepath.Join("testdata", "downloader", "dirA.tgz"))
h.AssertNil(t, err)
out, err := subject.Download("file://" + absPath)
h.AssertNil(t, err)
h.AssertNotEq(t, out, "")
h.AssertDirContainsFileWithContents(t, out, "file.txt", "some file contents")
})
it("download from a 'http(s)://' URI tgz", func() {
server := ghttp.NewServer()
server.AppendHandlers(func(w http.ResponseWriter, r *http.Request) {
path := filepath.Join("testdata", r.URL.Path)
http.ServeFile(w, r, path)
})
defer server.Close()
out, err := subject.Download(server.URL() + "/downloader/dirA.tgz")
h.AssertNil(t, err)
h.AssertNotEq(t, out, "")
h.AssertDirContainsFileWithContents(t, out, "file.txt", "some file contents")
})
})
}