/
github_test.go
120 lines (102 loc) · 3.23 KB
/
github_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
package client
import (
"fmt"
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestNewGitHubClient(t *testing.T) {
t.Run("good urls", func(t *testing.T) {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{
API: "https://github.mycompany.com/api",
Upload: "https://github.mycompany.com/upload",
},
})
_, err := NewGitHub(ctx, ctx.Token)
require.NoError(t, err)
})
t.Run("bad api url", func(t *testing.T) {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{
API: "://github.mycompany.com/api",
Upload: "https://github.mycompany.com/upload",
},
})
_, err := NewGitHub(ctx, ctx.Token)
require.EqualError(t, err, `parse "://github.mycompany.com/api": missing protocol scheme`)
})
t.Run("bad upload url", func(t *testing.T) {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{
API: "https://github.mycompany.com/api",
Upload: "not a url:4994",
},
})
_, err := NewGitHub(ctx, ctx.Token)
require.EqualError(t, err, `parse "not a url:4994": first path segment in URL cannot contain colon`)
})
t.Run("template", func(t *testing.T) {
githubURL := "https://github.mycompany.com"
ctx := context.New(config.Project{
Env: []string{
fmt.Sprintf("GORELEASER_TEST_GITHUB_URLS_API=%s/api", githubURL),
fmt.Sprintf("GORELEASER_TEST_GITHUB_URLS_UPLOAD=%s/upload", githubURL),
},
GitHubURLs: config.GitHubURLs{
API: "{{ .Env.GORELEASER_TEST_GITHUB_URLS_API }}",
Upload: "{{ .Env.GORELEASER_TEST_GITHUB_URLS_UPLOAD }}",
},
})
client, err := NewGitHub(ctx, ctx.Token)
require.NoError(t, err)
githubClient, ok := client.(*githubClient)
require.True(t, ok)
require.Equal(t, githubURL+"/api", githubClient.client.BaseURL.String())
require.Equal(t, githubURL+"/upload", githubClient.client.UploadURL.String())
})
}
func TestGitHubUploadReleaseIDNotInt(t *testing.T) {
ctx := context.New(config.Project{})
client, err := NewGitHub(ctx, ctx.Token)
require.NoError(t, err)
require.EqualError(
t,
client.Upload(ctx, "blah", &artifact.Artifact{}, nil),
`strconv.ParseInt: parsing "blah": invalid syntax`,
)
}
func TestGitHubReleaseURLTemplate(t *testing.T) {
ctx := context.New(config.Project{
GitHubURLs: config.GitHubURLs{
// default URL would otherwise be set via pipe/defaults
Download: DefaultGitHubDownloadURL,
},
Release: config.Release{
GitHub: config.Repo{
Owner: "owner",
Name: "name",
},
},
})
client, err := NewGitHub(ctx, ctx.Token)
require.NoError(t, err)
urlTpl, err := client.ReleaseURLTemplate(ctx)
require.NoError(t, err)
expectedURL := "https://github.com/owner/name/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
require.Equal(t, expectedURL, urlTpl)
}
func TestGitHubCreateReleaseWrongNameTemplate(t *testing.T) {
ctx := context.New(config.Project{
Release: config.Release{
NameTemplate: "{{.dddddddddd",
},
})
client, err := NewGitHub(ctx, ctx.Token)
require.NoError(t, err)
str, err := client.CreateRelease(ctx, "")
require.Empty(t, str)
require.EqualError(t, err, `template: tmpl:1: unclosed action`)
}