2
2
import { resolve } from 'path' ;
3
3
import nock from 'nock' ;
4
4
import {
5
- testEnv ,
6
- spyOnStdout ,
7
- getOctokit ,
8
- generateContext ,
9
- stdoutCalledWith ,
10
- disableNetConnect ,
11
- getApiFixture ,
5
+ testEnv ,
6
+ spyOnStdout ,
7
+ getOctokit ,
8
+ generateContext ,
9
+ stdoutCalledWith ,
10
+ disableNetConnect ,
11
+ getApiFixture ,
12
12
} from '@technote-space/github-action-test-helper' ;
13
13
import { Logger } from '@technote-space/github-action-helper' ;
14
14
import { execute } from '../src/process' ;
@@ -17,127 +17,127 @@ const rootDir = resolve(__dirname, '..');
17
17
const fixturesDir = resolve ( __dirname , 'fixtures' ) ;
18
18
19
19
beforeEach ( ( ) => {
20
- Logger . resetForTesting ( ) ;
20
+ Logger . resetForTesting ( ) ;
21
21
} ) ;
22
22
23
23
describe ( 'execute' , ( ) => {
24
- disableNetConnect ( nock ) ;
25
- testEnv ( rootDir ) ;
26
-
27
- it ( 'should do nothing 1' , async ( ) => {
28
- process . env . INPUT_EXCLUDE_MERGED = 'true' ;
29
-
30
- const mockStdout = spyOnStdout ( ) ;
31
-
32
- await execute ( new Logger ( ) , getOctokit ( ) , generateContext ( { owner : 'hello' , repo : 'world' , event : 'push' } , {
33
- payload : {
34
- 'head_commit' : {
35
- message : 'Merge pull request #260 from test' ,
36
- } ,
37
- } ,
38
- } ) ) ;
39
-
40
- stdoutCalledWith ( mockStdout , [
41
- '> This is not target context.' ,
42
- ] ) ;
43
- } ) ;
44
-
45
- it ( 'should do nothing 2' , async ( ) => {
46
- process . env . GITHUB_RUN_ID = '123' ;
47
-
48
- const mockStdout = spyOnStdout ( ) ;
49
- nock ( 'https://api.github.com' )
50
- . get ( '/repos/hello/world/actions/runs/123' )
51
- . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.get' ) )
52
- . get ( '/repos/hello/world/actions/workflows/30433642/runs?status=in_progress&branch=release%2Fv1.2.3&event=pull_request' )
53
- . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.list' ) ) ;
54
-
55
- await execute ( new Logger ( ) , getOctokit ( ) , generateContext ( { owner : 'hello' , repo : 'world' , event : 'pull_request' } , {
56
- payload : {
57
- 'pull_request' : {
58
- head : {
59
- ref : 'release/v1.2.3' ,
60
- } ,
61
- } ,
62
- } ,
63
- } ) ) ;
64
-
65
- stdoutCalledWith ( mockStdout , [
66
- '> run id: 123' ,
67
- '> workflow id: 30433642' ,
68
- 'target event: \x1b[32;40;0mpull_request\x1b[0m' ,
69
- 'target branch: \x1b[32;40;0mrelease/v1.2.3\x1b[0m' ,
70
- '' ,
71
- '> \x1b[33;40;0mcurrent run not found\x1b[0m' ,
72
- ] ) ;
73
- } ) ;
74
-
75
- it ( 'should do nothing 3' , async ( ) => {
76
- process . env . GITHUB_RUN_ID = '30433643' ;
77
-
78
- const mockStdout = spyOnStdout ( ) ;
79
- nock ( 'https://api.github.com' )
80
- . get ( '/repos/hello/world/actions/runs/30433643' )
81
- . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.get' ) )
82
- . get ( '/repos/hello/world/actions/workflows/30433642/runs?status=in_progress&branch=release%2Fv1.2.3&event=pull_request' )
83
- . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.list' ) ) ;
84
-
85
- await execute ( new Logger ( ) , getOctokit ( ) , generateContext ( { owner : 'hello' , repo : 'world' , event : 'pull_request' } , {
86
- payload : {
87
- 'pull_request' : {
88
- head : {
89
- ref : 'release/v1.2.3' ,
90
- } ,
91
- } ,
92
- } ,
93
- } ) ) ;
94
-
95
- stdoutCalledWith ( mockStdout , [
96
- '> run id: 30433643' ,
97
- '> workflow id: 30433642' ,
98
- 'target event: \x1b[32;40;0mpull_request\x1b[0m' ,
99
- 'target branch: \x1b[32;40;0mrelease/v1.2.3\x1b[0m' ,
100
- '' ,
101
- '> \x1b[33;40;0mnewer job exists\x1b[0m' ,
102
- ] ) ;
103
- } ) ;
104
-
105
- it ( 'should cancel jobs' , async ( ) => {
106
- process . env . INPUT_EXCLUDE_MERGED = 'true' ;
107
- process . env . GITHUB_RUN_ID = '30433645' ;
108
-
109
- const mockStdout = spyOnStdout ( ) ;
110
- nock ( 'https://api.github.com' )
111
- . get ( '/repos/hello/world/actions/runs/30433645' )
112
- . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.get' ) )
113
- . get ( '/repos/hello/world/actions/workflows/30433642/runs?status=in_progress&branch=release%2Fv1.2.3&event=pull_request' )
114
- . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.list' ) )
115
- . post ( '/repos/hello/world/actions/runs/30433643/cancel' )
116
- . reply ( 202 )
117
- . post ( '/repos/hello/world/actions/runs/30433642/cancel' )
118
- . reply ( 202 ) ;
119
-
120
- await execute ( new Logger ( ) , getOctokit ( ) , generateContext ( { owner : 'hello' , repo : 'world' , event : 'pull_request' } , {
121
- payload : {
122
- 'pull_request' : {
123
- head : {
124
- ref : 'release/v1.2.3' ,
125
- } ,
126
- } ,
127
- } ,
128
- } ) ) ;
129
-
130
- stdoutCalledWith ( mockStdout , [
131
- '> run id: 30433645' ,
132
- '> workflow id: 30433642' ,
133
- 'target event: \x1b[32;40;0mpull_request\x1b[0m' ,
134
- 'target branch: \x1b[32;40;0mrelease/v1.2.3\x1b[0m' ,
135
- '' ,
136
- '::group::Cancelling...' ,
137
- 'cancel: 30433642' ,
138
- 'cancel: 30433643' ,
139
- '> total: 2' ,
140
- '::endgroup::' ,
141
- ] ) ;
142
- } ) ;
24
+ disableNetConnect ( nock ) ;
25
+ testEnv ( rootDir ) ;
26
+
27
+ it ( 'should do nothing 1' , async ( ) => {
28
+ process . env . INPUT_EXCLUDE_MERGED = 'true' ;
29
+
30
+ const mockStdout = spyOnStdout ( ) ;
31
+
32
+ await execute ( new Logger ( ) , getOctokit ( ) , generateContext ( { owner : 'hello' , repo : 'world' , event : 'push' } , {
33
+ payload : {
34
+ 'head_commit' : {
35
+ message : 'Merge pull request #260 from test' ,
36
+ } ,
37
+ } ,
38
+ } ) ) ;
39
+
40
+ stdoutCalledWith ( mockStdout , [
41
+ '> This is not target context.' ,
42
+ ] ) ;
43
+ } ) ;
44
+
45
+ it ( 'should do nothing 2' , async ( ) => {
46
+ process . env . GITHUB_RUN_ID = '123' ;
47
+
48
+ const mockStdout = spyOnStdout ( ) ;
49
+ nock ( 'https://api.github.com' )
50
+ . get ( '/repos/hello/world/actions/runs/123' )
51
+ . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.get' ) )
52
+ . get ( '/repos/hello/world/actions/workflows/30433642/runs?status=in_progress&branch=release%2Fv1.2.3&event=pull_request' )
53
+ . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.list' ) ) ;
54
+
55
+ await execute ( new Logger ( ) , getOctokit ( ) , generateContext ( { owner : 'hello' , repo : 'world' , event : 'pull_request' } , {
56
+ payload : {
57
+ 'pull_request' : {
58
+ head : {
59
+ ref : 'release/v1.2.3' ,
60
+ } ,
61
+ } ,
62
+ } ,
63
+ } ) ) ;
64
+
65
+ stdoutCalledWith ( mockStdout , [
66
+ '> run id: 123' ,
67
+ '> workflow id: 30433642' ,
68
+ 'target event: \x1b[32;40;0mpull_request\x1b[0m' ,
69
+ 'target branch: \x1b[32;40;0mrelease/v1.2.3\x1b[0m' ,
70
+ '' ,
71
+ '> \x1b[33;40;0mcurrent run not found\x1b[0m' ,
72
+ ] ) ;
73
+ } ) ;
74
+
75
+ it ( 'should do nothing 3' , async ( ) => {
76
+ process . env . GITHUB_RUN_ID = '30433643' ;
77
+
78
+ const mockStdout = spyOnStdout ( ) ;
79
+ nock ( 'https://api.github.com' )
80
+ . get ( '/repos/hello/world/actions/runs/30433643' )
81
+ . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.get' ) )
82
+ . get ( '/repos/hello/world/actions/workflows/30433642/runs?status=in_progress&branch=release%2Fv1.2.3&event=pull_request' )
83
+ . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.list' ) ) ;
84
+
85
+ await execute ( new Logger ( ) , getOctokit ( ) , generateContext ( { owner : 'hello' , repo : 'world' , event : 'pull_request' } , {
86
+ payload : {
87
+ 'pull_request' : {
88
+ head : {
89
+ ref : 'release/v1.2.3' ,
90
+ } ,
91
+ } ,
92
+ } ,
93
+ } ) ) ;
94
+
95
+ stdoutCalledWith ( mockStdout , [
96
+ '> run id: 30433643' ,
97
+ '> workflow id: 30433642' ,
98
+ 'target event: \x1b[32;40;0mpull_request\x1b[0m' ,
99
+ 'target branch: \x1b[32;40;0mrelease/v1.2.3\x1b[0m' ,
100
+ '' ,
101
+ '> \x1b[33;40;0mnewer job exists\x1b[0m' ,
102
+ ] ) ;
103
+ } ) ;
104
+
105
+ it ( 'should cancel jobs' , async ( ) => {
106
+ process . env . INPUT_EXCLUDE_MERGED = 'true' ;
107
+ process . env . GITHUB_RUN_ID = '30433645' ;
108
+
109
+ const mockStdout = spyOnStdout ( ) ;
110
+ nock ( 'https://api.github.com' )
111
+ . get ( '/repos/hello/world/actions/runs/30433645' )
112
+ . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.get' ) )
113
+ . get ( '/repos/hello/world/actions/workflows/30433642/runs?status=in_progress&branch=release%2Fv1.2.3&event=pull_request' )
114
+ . reply ( 200 , ( ) => getApiFixture ( fixturesDir , 'workflow-run.list' ) )
115
+ . post ( '/repos/hello/world/actions/runs/30433643/cancel' )
116
+ . reply ( 202 )
117
+ . post ( '/repos/hello/world/actions/runs/30433642/cancel' )
118
+ . reply ( 202 ) ;
119
+
120
+ await execute ( new Logger ( ) , getOctokit ( ) , generateContext ( { owner : 'hello' , repo : 'world' , event : 'pull_request' } , {
121
+ payload : {
122
+ 'pull_request' : {
123
+ head : {
124
+ ref : 'release/v1.2.3' ,
125
+ } ,
126
+ } ,
127
+ } ,
128
+ } ) ) ;
129
+
130
+ stdoutCalledWith ( mockStdout , [
131
+ '> run id: 30433645' ,
132
+ '> workflow id: 30433642' ,
133
+ 'target event: \x1b[32;40;0mpull_request\x1b[0m' ,
134
+ 'target branch: \x1b[32;40;0mrelease/v1.2.3\x1b[0m' ,
135
+ '' ,
136
+ '::group::Cancelling...' ,
137
+ 'cancel: 30433642' ,
138
+ 'cancel: 30433643' ,
139
+ '> total: 2' ,
140
+ '::endgroup::' ,
141
+ ] ) ;
142
+ } ) ;
143
143
} ) ;
0 commit comments