1
- import tempWrite from 'temp-write' ;
2
1
import globby from 'globby' ;
2
+ import fs from 'fs' ;
3
+ import mime from 'mime-types' ;
4
+ import { getRepoInfo } from 'shipjs-lib' ;
5
+ import Octokit from '@octokit/rest' ;
3
6
import createGitHubRelease from '../createGitHubRelease' ;
4
- import { run } from '../../../util' ;
5
7
import { hubInstalled , hubConfigured } from '../../../helper' ;
6
8
jest . mock ( 'temp-write' ) ;
9
+ jest . mock ( '@octokit/rest' ) ;
7
10
jest . mock ( 'globby' ) ;
11
+ jest . mock ( 'shipjs-lib' ) ;
12
+ jest . mock ( 'fs' ) ;
13
+ jest . mock ( 'mime-types' ) ;
8
14
9
15
const getDefaultParams = ( {
10
16
assetsToUpload,
@@ -20,91 +26,180 @@ const getDefaultParams = ({
20
26
dryRun : false ,
21
27
} ) ;
22
28
29
+ const createRelease = jest . fn ( ) . mockImplementation ( ( ) => ( {
30
+ data : {
31
+ upload_url : 'https://dummy/upload/url' , // eslint-disable-line camelcase
32
+ } ,
33
+ } ) ) ;
34
+ const uploadReleaseAsset = jest . fn ( ) ;
35
+ Octokit . mockImplementation ( function ( ) {
36
+ this . repos = { createRelease, uploadReleaseAsset } ;
37
+ } ) ;
38
+
23
39
describe ( 'createGitHubRelease' , ( ) => {
24
40
beforeEach ( ( ) => {
25
41
hubInstalled . mockImplementation ( ( ) => true ) ;
26
42
hubConfigured . mockImplementation ( ( ) => true ) ;
43
+ getRepoInfo . mockImplementation ( ( ) => ( {
44
+ owner : 'my' ,
45
+ name : 'repo' ,
46
+ } ) ) ;
47
+ fs . readFileSync = jest . fn ( ) ;
48
+ fs . statSync = jest . fn ( ) . mockImplementation ( ( ) => ( { size : 1024 } ) ) ;
49
+ mime . lookup . mockImplementation ( ( ) => 'application/zip' ) ;
50
+ globby . mockImplementation ( path => Promise . resolve ( [ path ] ) ) ;
27
51
} ) ;
28
52
29
53
it ( 'works without assets' , async ( ) => {
30
- tempWrite . sync . mockImplementation ( ( ) => `/my chan"ge"log/temp/path` ) ;
31
54
await createGitHubRelease ( getDefaultParams ( ) ) ;
32
- expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
33
- expect ( run . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
55
+ expect ( createRelease ) . toHaveBeenCalledTimes ( 1 ) ;
56
+ expect ( createRelease . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
34
57
Array [
35
58
Object {
36
- "command": "hub release create -F '/my chan\\"ge\\"log/temp/path' v1.2.3",
37
- "dir": ".",
38
- "dryRun": false,
59
+ "body": "",
60
+ "name": "v1.2.3",
61
+ "owner": "my",
62
+ "repo": "repo",
63
+ "tag_name": "v1.2.3",
39
64
},
40
65
]
41
66
` ) ;
67
+ expect ( uploadReleaseAsset ) . toHaveBeenCalledTimes ( 0 ) ;
42
68
} ) ;
43
69
44
70
it ( 'works with assets (fn)' , async ( ) => {
45
- tempWrite . sync . mockImplementation ( ( ) => `/temp/path` ) ;
46
71
await createGitHubRelease (
47
72
getDefaultParams ( {
48
73
assetsToUpload : ( ) => {
49
74
return Promise . resolve ( [ '/path1' , '/path2' ] ) ;
50
75
} ,
51
76
} )
52
77
) ;
53
- expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
54
- expect ( run . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
78
+ expect ( createRelease ) . toHaveBeenCalledTimes ( 1 ) ;
79
+ expect ( createRelease . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
55
80
Array [
56
81
Object {
57
- "command": "hub release create -F /temp/path -a /path1 -a /path2 v1.2.3",
58
- "dir": ".",
59
- "dryRun": false,
82
+ "body": "",
83
+ "name": "v1.2.3",
84
+ "owner": "my",
85
+ "repo": "repo",
86
+ "tag_name": "v1.2.3",
60
87
},
61
88
]
62
89
` ) ;
90
+ expect ( uploadReleaseAsset ) . toHaveBeenCalledTimes ( 2 ) ;
91
+ expect ( uploadReleaseAsset . mock . calls ) . toMatchInlineSnapshot ( `
92
+ Array [
93
+ Array [
94
+ Object {
95
+ "file": undefined,
96
+ "headers": Object {
97
+ "content-length": 1024,
98
+ "content-type": "application/zip",
99
+ },
100
+ "name": "path1",
101
+ "url": "https://dummy/upload/url",
102
+ },
103
+ ],
104
+ Array [
105
+ Object {
106
+ "file": undefined,
107
+ "headers": Object {
108
+ "content-length": 1024,
109
+ "content-type": "application/zip",
110
+ },
111
+ "name": "path2",
112
+ "url": "https://dummy/upload/url",
113
+ },
114
+ ],
115
+ ]
116
+ ` ) ;
63
117
} ) ;
64
118
65
119
it ( 'works with assets (list)' , async ( ) => {
66
- tempWrite . sync . mockImplementation ( ( ) => `/temp/path` ) ;
67
- globby . mockImplementation ( path => Promise . resolve ( [ path ] ) ) ;
68
120
await createGitHubRelease (
69
121
getDefaultParams ( {
70
122
assetsToUpload : [ '/path1' , '/path2' ] ,
71
123
} )
72
124
) ;
73
- expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
74
- expect ( run . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
125
+ expect ( createRelease ) . toHaveBeenCalledTimes ( 1 ) ;
126
+ expect ( createRelease . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
75
127
Array [
76
128
Object {
77
- "command": "hub release create -F /temp/path -a /path1 -a /path2 v1.2.3",
78
- "dir": ".",
79
- "dryRun": false,
129
+ "body": "",
130
+ "name": "v1.2.3",
131
+ "owner": "my",
132
+ "repo": "repo",
133
+ "tag_name": "v1.2.3",
80
134
},
81
135
]
82
136
` ) ;
137
+ expect ( uploadReleaseAsset ) . toHaveBeenCalledTimes ( 2 ) ;
138
+ expect ( uploadReleaseAsset . mock . calls ) . toMatchInlineSnapshot ( `
139
+ Array [
140
+ Array [
141
+ Object {
142
+ "file": undefined,
143
+ "headers": Object {
144
+ "content-length": 1024,
145
+ "content-type": "application/zip",
146
+ },
147
+ "name": "path1",
148
+ "url": "https://dummy/upload/url",
149
+ },
150
+ ],
151
+ Array [
152
+ Object {
153
+ "file": undefined,
154
+ "headers": Object {
155
+ "content-length": 1024,
156
+ "content-type": "application/zip",
157
+ },
158
+ "name": "path2",
159
+ "url": "https://dummy/upload/url",
160
+ },
161
+ ],
162
+ ]
163
+ ` ) ;
83
164
} ) ;
84
165
85
166
it ( 'works with assets (string)' , async ( ) => {
86
- tempWrite . sync . mockImplementation ( ( ) => `/temp/path` ) ;
87
- globby . mockImplementation ( path => Promise . resolve ( [ path ] ) ) ;
88
167
await createGitHubRelease (
89
168
getDefaultParams ( {
90
169
assetsToUpload : '/path1' ,
91
170
} )
92
171
) ;
93
- expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
94
- expect ( run . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
172
+ expect ( createRelease ) . toHaveBeenCalledTimes ( 1 ) ;
173
+ expect ( createRelease . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
95
174
Array [
96
175
Object {
97
- "command": "hub release create -F /temp/path -a /path1 v1.2.3",
98
- "dir": ".",
99
- "dryRun": false,
176
+ "body": "",
177
+ "name": "v1.2.3",
178
+ "owner": "my",
179
+ "repo": "repo",
180
+ "tag_name": "v1.2.3",
100
181
},
101
182
]
102
183
` ) ;
184
+ expect ( uploadReleaseAsset ) . toHaveBeenCalledTimes ( 1 ) ;
185
+ expect ( uploadReleaseAsset . mock . calls ) . toMatchInlineSnapshot ( `
186
+ Array [
187
+ Array [
188
+ Object {
189
+ "file": undefined,
190
+ "headers": Object {
191
+ "content-length": 1024,
192
+ "content-type": "application/zip",
193
+ },
194
+ "name": "path1",
195
+ "url": "https://dummy/upload/url",
196
+ },
197
+ ],
198
+ ]
199
+ ` ) ;
103
200
} ) ;
104
201
105
202
it ( 'works with extractChangelog' , async ( ) => {
106
- tempWrite . sync . mockImplementation ( ( ) => `/temp/path` ) ;
107
- globby . mockImplementation ( path => Promise . resolve ( [ path ] ) ) ;
108
203
const mockExtractChangelog = jest
109
204
. fn ( )
110
205
. mockImplementation ( ( { version, dir } ) => `# v${ version } (${ dir } )` ) ;
@@ -117,13 +212,15 @@ describe('createGitHubRelease', () => {
117
212
version : '1.2.3' ,
118
213
dir : '.' ,
119
214
} ) ;
120
- expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
121
- expect ( run . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
215
+ expect ( createRelease ) . toHaveBeenCalledTimes ( 1 ) ;
216
+ expect ( createRelease . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
122
217
Array [
123
218
Object {
124
- "command": "hub release create -F /temp/path v1.2.3",
125
- "dir": ".",
126
- "dryRun": false,
219
+ "body": "# v1.2.3 (.)",
220
+ "name": "v1.2.3",
221
+ "owner": "my",
222
+ "repo": "repo",
223
+ "tag_name": "v1.2.3",
127
224
},
128
225
]
129
226
` ) ;
0 commit comments