1
- /* eslint-disable @typescript-eslint/no-var-requires */
2
-
3
1
import * as process from 'process' ;
4
2
import * as path from 'path' ;
5
3
import nock from 'nock' ;
6
4
import fs from 'fs' ;
5
+ import { uploadReleaseArtifacts } from '../src/uploadReleaseArtifacts' ;
6
+ import { main } from '../src/main' ;
7
+
8
+ jest . mock ( '../src/uploadReleaseArtifacts' ) ;
7
9
8
10
describe ( 'main handler processing automatic releases' , ( ) => {
9
11
const testGhToken = 'fake-secret-token' ;
@@ -33,6 +35,8 @@ describe('main handler processing automatic releases', () => {
33
35
process . env [ 'GITHUB_ACTOR' ] = 'marvinpinto' ;
34
36
process . env [ 'GITHUB_EVENT_PATH' ] = path . join ( __dirname , 'payloads' , 'git-push.json' ) ;
35
37
process . env [ 'GITHUB_REPOSITORY' ] = 'marvinpinto/private-actions-tester' ;
38
+
39
+ uploadReleaseArtifacts . mockImplementation ( ) . mockResolvedValue ( { } ) ;
36
40
} ) ;
37
41
38
42
afterEach ( ( ) => {
@@ -44,8 +48,7 @@ describe('main handler processing automatic releases', () => {
44
48
45
49
it ( 'throws an error when "automatic_release_tag" is not supplied' , async ( ) => {
46
50
delete process . env . INPUT_AUTOMATIC_RELEASE_TAG ;
47
- const inst = require ( '../src/main' ) ;
48
- await expect ( inst . main ( ) ) . rejects . toThrow (
51
+ await expect ( main ( ) ) . rejects . toThrow (
49
52
'The parameter "automatic_release_tag" was not set and this does not appear to be a GitHub tag event. (Event: refs/heads/automatic-pre-releaser)' ,
50
53
) ;
51
54
} ) ;
@@ -62,6 +65,11 @@ describe('main handler processing automatic releases', () => {
62
65
. get ( `/repos/marvinpinto/private-actions-tester/compare/HEAD...${ testGhSHA } ` )
63
66
. reply ( 200 , compareCommitsPayload ) ;
64
67
68
+ const getRef = nock ( 'https://api.github.com' )
69
+ . matchHeader ( 'authorization' , `token ${ testGhToken } ` )
70
+ . get ( `/repos/marvinpinto/private-actions-tester/git/refs/tags/${ testInputAutomaticReleaseTag } ` )
71
+ . reply ( 404 ) ;
72
+
65
73
const listAssociatedPRs = nock ( 'https://api.github.com' )
66
74
. matchHeader ( 'authorization' , `token ${ testGhToken } ` )
67
75
. get ( `/repos/marvinpinto/private-actions-tester/commits/${ testGhSHA } /pulls` )
@@ -101,21 +109,20 @@ describe('main handler processing automatic releases', () => {
101
109
// Output env variable should be empty
102
110
expect ( process . env [ 'AUTOMATIC_RELEASES_TAG' ] ) . toBeUndefined ( ) ;
103
111
104
- const inst = require ( '../src/main' ) ;
105
- inst . uploadReleaseArtifacts = jest . fn ( ( ) => Promise . resolve ( ) ) ;
106
- await inst . main ( ) ;
112
+ await main ( ) ;
107
113
108
114
expect ( createRef . isDone ( ) ) . toBe ( true ) ;
109
115
expect ( getReleaseByTag . isDone ( ) ) . toBe ( true ) ;
110
116
expect ( deleteRelease . isDone ( ) ) . toBe ( false ) ;
111
117
expect ( createRelease . isDone ( ) ) . toBe ( true ) ;
118
+ expect ( getRef . isDone ( ) ) . toBe ( true ) ;
112
119
expect ( getCommitsSinceRelease . isDone ( ) ) . toBe ( true ) ;
113
120
expect ( listAssociatedPRs . isDone ( ) ) . toBe ( true ) ;
114
121
115
- expect ( inst . uploadReleaseArtifacts ) . toHaveBeenCalledTimes ( 1 ) ;
116
- expect ( inst . uploadReleaseArtifacts . mock . calls [ 0 ] [ 1 ] ) . toBe ( releaseUploadUrl ) ;
122
+ expect ( uploadReleaseArtifacts ) . toHaveBeenCalledTimes ( 1 ) ;
123
+ expect ( uploadReleaseArtifacts . mock . calls [ 0 ] [ 1 ] ) . toBe ( releaseUploadUrl ) ;
117
124
// Should not attempt to upload any release artifacts, as there are none
118
- expect ( inst . uploadReleaseArtifacts . mock . calls [ 0 ] [ 2 ] ) . toEqual ( [ ] ) ;
125
+ expect ( uploadReleaseArtifacts . mock . calls [ 0 ] [ 2 ] ) . toEqual ( [ ] ) ;
119
126
120
127
// Should populate the output env variable
121
128
expect ( process . env [ 'AUTOMATIC_RELEASES_TAG' ] ) . toBe ( testInputAutomaticReleaseTag ) ;
@@ -193,9 +200,7 @@ describe('main handler processing automatic releases', () => {
193
200
// Output env variable should be empty
194
201
expect ( process . env [ 'AUTOMATIC_RELEASES_TAG' ] ) . toBeUndefined ( ) ;
195
202
196
- const inst = require ( '../src/main' ) ;
197
- inst . uploadReleaseArtifacts = jest . fn ( ( ) => Promise . resolve ( ) ) ;
198
- await inst . main ( ) ;
203
+ await main ( ) ;
199
204
200
205
expect ( createRef . isDone ( ) ) . toBe ( true ) ;
201
206
expect ( updateRef . isDone ( ) ) . toBe ( true ) ;
@@ -206,9 +211,9 @@ describe('main handler processing automatic releases', () => {
206
211
expect ( getCommitsSinceRelease . isDone ( ) ) . toBe ( true ) ;
207
212
expect ( listAssociatedPRs . isDone ( ) ) . toBe ( true ) ;
208
213
209
- expect ( inst . uploadReleaseArtifacts ) . toHaveBeenCalledTimes ( 1 ) ;
210
- expect ( inst . uploadReleaseArtifacts . mock . calls [ 0 ] [ 1 ] ) . toBe ( releaseUploadUrl ) ;
211
- expect ( inst . uploadReleaseArtifacts . mock . calls [ 0 ] [ 2 ] ) . toEqual ( [ 'file1.txt' , 'file2.txt' , '*.jar' ] ) ;
214
+ expect ( uploadReleaseArtifacts ) . toHaveBeenCalledTimes ( 1 ) ;
215
+ expect ( uploadReleaseArtifacts . mock . calls [ 0 ] [ 1 ] ) . toBe ( releaseUploadUrl ) ;
216
+ expect ( uploadReleaseArtifacts . mock . calls [ 0 ] [ 2 ] ) . toEqual ( [ 'file1.txt' , 'file2.txt' , '*.jar' ] ) ;
212
217
213
218
// Should populate the output env variable
214
219
expect ( process . env [ 'AUTOMATIC_RELEASES_TAG' ] ) . toBe ( testInputAutomaticReleaseTag ) ;
0 commit comments