forked from drone-plugins/drone-pypi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
152 lines (143 loc) · 3.4 KB
/
main_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
144
145
146
147
148
149
150
151
152
package main
import (
"bytes"
"os"
"strings"
"testing"
"github.com/drone/drone-go/drone"
)
// TestPublish checks if this module can successfully publish a PyPI
// package. A simple module is included in the `testdata` directory.
//
// To run this test against the PyPI test server:
//
// 1. register a new account (https://wiki.python.org/moin/TestPyPI)
// 2. Export DRONE_PYPI_PATH, DRONE_PYPI_REPOSITORY, DRONE_PYPI_USERNAME,
// DRONE_PYPI_PASSWORD, and DRONE_PYPI_DISTRIBUTIONS
// 3. Run the test suite
//
// For example:
//
// $ export DRONE_PYPI_PATH=testdata
// $ export DRONE_PYPI_REPOSITORY=https://testpypi.python.org/pypi
// $ export DRONE_PYPI_USERNAME=drone_pypi_test
// $ export DRONE_PYPI_PASSWORD=$uper$ecretPassword
// $ export DRONE_PYPI_DISTRIBUTIONS=sdist
// $ go test -run TestPublish
//
// > NOTE: PyPI will refuse to upload the same version of a module twice,
// > however setup.py still returns zero to the shell so this appears as a
// > successful test.
func TestPublish(t *testing.T) {
w := drone.Workspace{Path: os.Getenv("DRONE_PYPI_PATH")}
repository := os.Getenv("DRONE_PYPI_REPOSITORY")
username := os.Getenv("DRONE_PYPI_USERNAME")
password := os.Getenv("DRONE_PYPI_PASSWORD")
v := Params{
Repository: &repository,
Username: &username,
Password: &password,
Distributions: strings.Split(os.Getenv("DRONE_PYPI_DISTRIBUTIONS"), " "),
}
if w.Path == "" {
t.Skip("DRONE_PYPI_PATH not set")
}
err := v.Deploy(&w)
if err != nil {
t.Error(err)
}
}
// TestConfig checks if a PyPI configuration file can be generated.
func TestConfig(t *testing.T) {
testdata := []struct {
repository *string
username *string
password *string
exp string
}{
{
nil,
nil,
nil,
`[distutils]
index-servers =
pypi
[pypi]
repository: https://pypi.python.org/pypi
username: guido
password: secret
`,
},
{
sPtr("https://pypi.example.com"),
nil,
nil,
`[distutils]
index-servers =
pypi
[pypi]
repository: https://pypi.example.com
username: guido
password: secret
`,
},
{
nil,
sPtr("jqhacker"),
sPtr("supersecret"),
`[distutils]
index-servers =
pypi
[pypi]
repository: https://pypi.python.org/pypi
username: jqhacker
password: supersecret
`,
},
}
for i, data := range testdata {
v := Params{
Repository: data.repository,
Username: data.username,
Password: data.password,
Distributions: []string{},
}
var b bytes.Buffer
v.WriteConfig(&b)
if b.String() != data.exp {
t.Errorf("Case %d: Expected %s, got %s\n", i, data.exp, b.String())
}
}
}
// TestUpload checks if a distutils upload command can be properly
// formatted.
func TestUpload(t *testing.T) {
testdata := []struct {
distributions []string
exp []string
}{
{
[]string{},
[]string{"python", "setup.py", "sdist", "upload", "-r", "pypi"},
},
{
[]string{"sdist", "bdist_wheel"},
[]string{"python", "setup.py", "sdist", "bdist_wheel", "upload", "-r", "pypi"},
},
}
for i, data := range testdata {
v := Params{Distributions: data.distributions}
c := v.Upload()
if len(c.Args) != len(data.exp) {
t.Errorf("Case %d: Expected %d, got %d", i, len(data.exp), len(c.Args))
}
for i := range c.Args {
if c.Args[i] != data.exp[i] {
t.Errorf("Case %d: Expected %s, got %s", i, strings.Join(data.exp, " "), strings.Join(c.Args, " "))
}
}
}
}
func sPtr(s string) *string {
return &s
}