forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_test.go
More file actions
106 lines (91 loc) · 2.44 KB
/
http_test.go
File metadata and controls
106 lines (91 loc) · 2.44 KB
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
package download
import (
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_List(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/123/artifacts"),
httpmock.StringResponse(`{
"total_count": 2,
"artifacts": [
{"name": "artifact-1"},
{"name": "artifact-2"}
]
}`))
api := &apiPlatform{
client: &http.Client{Transport: reg},
repo: ghrepo.New("OWNER", "REPO"),
}
artifacts, err := api.List("123")
require.NoError(t, err)
require.Equal(t, 2, len(artifacts))
assert.Equal(t, "artifact-1", artifacts[0].Name)
assert.Equal(t, "artifact-2", artifacts[1].Name)
}
func Test_List_perRepository(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/actions/artifacts"),
httpmock.StringResponse(`{}`))
api := &apiPlatform{
client: &http.Client{Transport: reg},
repo: ghrepo.New("OWNER", "REPO"),
}
_, err := api.List("")
require.NoError(t, err)
}
func Test_Download(t *testing.T) {
tmpDir := t.TempDir()
destDir := filepath.Join(tmpDir, "artifact")
reg := &httpmock.Registry{}
defer reg.Verify(t)
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/actions/artifacts/12345/zip"),
httpmock.FileResponse("./fixtures/myproject.zip"))
api := &apiPlatform{
client: &http.Client{Transport: reg},
}
err := api.Download("https://api.github.com/repos/OWNER/REPO/actions/artifacts/12345/zip", destDir)
require.NoError(t, err)
var paths []string
parentPrefix := tmpDir + string(filepath.Separator)
err = filepath.Walk(tmpDir, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if p == tmpDir {
return nil
}
entry := strings.TrimPrefix(p, parentPrefix)
if info.IsDir() {
entry += "/"
} else if info.Mode()&0111 != 0 {
entry += "(X)"
}
paths = append(paths, entry)
return nil
})
require.NoError(t, err)
sort.Strings(paths)
assert.Equal(t, []string{
"artifact/",
filepath.Join("artifact", "bin") + "/",
filepath.Join("artifact", "bin", "myexe"),
filepath.Join("artifact", "readme.md"),
filepath.Join("artifact", "src") + "/",
filepath.Join("artifact", "src", "main.go"),
filepath.Join("artifact", "src", "util.go"),
}, paths)
}