@@ -10,9 +10,22 @@ var processor = require('./processor')()
10
10
11
11
var exec = promisify ( cp . exec )
12
12
13
- test ( 'diff()' , function ( t ) {
14
- var current = process . cwd ( )
15
- var range = process . env . TRAVIS_COMMIT_RANGE
13
+ var range = process . env . TRAVIS_COMMIT_RANGE
14
+ var sha = process . env . GITHUB_SHA
15
+ var base = process . env . GITHUB_BASE_REF
16
+ var head = process . env . GITHUB_HEAD_REF
17
+
18
+ // Remove potential variables that we’re testing on CIs.
19
+ delete process . env . TRAVIS_COMMIT_RANGE
20
+ delete process . env . GITHUB_SHA
21
+ delete process . env . GITHUB_BASE_REF
22
+ delete process . env . GITHUB_HEAD_REF
23
+
24
+ var current = process . cwd ( )
25
+
26
+ process . chdir ( path . join ( current , 'test' ) )
27
+
28
+ test ( 'diff() (travis)' , function ( t ) {
16
29
var stepOne = [
17
30
'Lorem ipsum dolor sit amet.' ,
18
31
'' ,
@@ -25,27 +38,11 @@ test('diff()', function (t) {
25
38
var initial
26
39
var final
27
40
28
- delete process . env . TRAVIS_COMMIT_RANGE
29
-
30
41
t . plan ( 7 )
31
42
32
- process . chdir ( path . join ( current , 'test' ) )
33
-
34
43
exec ( 'git init' )
35
44
// Set up.
36
- . then ( ( ) => {
37
- return exec ( 'git config --global user.email' ) . catch ( oncatch )
38
-
39
- function oncatch ( ) {
40
- return exec ( 'git config --global user.email [email protected] ' ) . then (
41
- onemail
42
- )
43
- }
44
-
45
- function onemail ( ) {
46
- return exec ( 'git config --global user.name Ex Ample' )
47
- }
48
- } )
45
+ . then ( ( ) => exec ( 'git config --global user.email' ) . catch ( setEmailAndName ) )
49
46
// Add initial file.
50
47
. then ( ( ) =>
51
48
processor . process ( vfile ( { path : 'example.txt' , contents : stepOne } ) )
@@ -64,16 +61,15 @@ test('diff()', function (t) {
64
61
. then ( ( ) => exec ( 'git rev-parse HEAD' ) )
65
62
. then ( ( result ) => {
66
63
initial = result . stdout . trim ( )
67
- return vfile . write ( { path : 'example.txt' , contents : stepTwo } )
68
64
} )
69
- // Changed files.
65
+ // Change files.
66
+ . then ( ( ) => vfile . write ( { path : 'example.txt' , contents : stepTwo } ) )
70
67
. then ( ( ) => exec ( 'git add example.txt' ) )
71
68
. then ( ( ) => exec ( 'git commit -m two' ) )
72
69
. then ( ( ) => exec ( 'git rev-parse HEAD' ) )
73
70
. then ( ( result ) => {
74
71
final = result . stdout . trim ( )
75
72
process . env . TRAVIS_COMMIT_RANGE = [ initial , final ] . join ( '...' )
76
-
77
73
return processor . process ( vfile ( { path : 'example.txt' , contents : stepTwo } ) )
78
74
} )
79
75
. then ( ( file ) => {
@@ -82,24 +78,22 @@ test('diff()', function (t) {
82
78
[ 'example.txt:5:1-5:6: No lorem!' ] ,
83
79
'should show only messages for changed lines'
84
80
)
85
-
86
- return file
87
81
} )
88
82
// Again!
89
- . then ( ( ) => {
90
- return processor . process ( vfile ( { path : 'example.txt' , contents : stepTwo } ) )
91
- } )
83
+ . then ( ( ) =>
84
+ processor . process ( vfile ( { path : 'example.txt' , contents : stepTwo } ) )
85
+ )
92
86
. then ( ( file ) => {
93
87
t . deepEqual (
94
88
file . messages . map ( String ) ,
95
89
[ 'example.txt:5:1-5:6: No lorem!' ] ,
96
90
'should not recheck (coverage for optimisations)'
97
91
)
98
92
} )
99
- // Unstages files.
100
- . then ( ( ) => {
101
- return processor . process ( vfile ( { path : 'missing.txt' , contents : other } ) )
102
- } )
93
+ // Unstaged files.
94
+ . then ( ( ) =>
95
+ processor . process ( vfile ( { path : 'missing.txt' , contents : other } ) )
96
+ )
103
97
. then ( ( file ) => {
104
98
t . deepEqual ( file . messages . map ( String ) , [ ] , 'should ignore unstaged files' )
105
99
} )
@@ -136,17 +130,111 @@ test('diff()', function (t) {
136
130
137
131
return processor . process ( vfile ( { path : 'new.txt' , contents : other } ) )
138
132
} )
133
+ . then (
134
+ ( ) => t . pass ( 'should pass' ) ,
135
+ ( error ) => t . ifErr ( error , 'should not fail' )
136
+ )
139
137
// Restore
140
138
. then ( restore , restore )
139
+
140
+ function restore ( ) {
141
+ delete process . env . TRAVIS_COMMIT_RANGE
142
+ return rimraf ( '.git' )
143
+ . then ( ( ) => rimraf ( 'new.txt' ) )
144
+ . then ( ( ) => rimraf ( 'example.txt' ) )
145
+ }
146
+ } )
147
+
148
+ test ( 'diff() (GitHub Actions)' , function ( t ) {
149
+ var stepOne = [
150
+ 'Lorem ipsum dolor sit amet.' ,
151
+ '' ,
152
+ 'Lorem ipsum. Dolor sit amet.' ,
153
+ ''
154
+ ] . join ( '\n' )
155
+ var stepTwo = stepOne + '\nLorem ipsum.\n'
156
+ var stepThree = 'Lorem.\n\n' + stepOne + '\nAlpha bravo.\n'
157
+ var stepFour = stepThree + '\nIpsum lorem.\n'
158
+ var main
159
+
160
+ t . plan ( 3 )
161
+
162
+ exec ( 'git init' )
163
+ // Set up.
164
+ . then ( ( ) => exec ( 'git config --global user.email' ) . catch ( setEmailAndName ) )
165
+ // Add initial file.
166
+ . then ( ( ) => vfile . write ( { path : 'example.txt' , contents : stepOne } ) )
167
+ . then ( ( ) => exec ( 'git add example.txt' ) )
168
+ . then ( ( ) => exec ( 'git commit -m one' ) )
169
+ // Change file.
170
+ . then ( ( ) => vfile . write ( { path : 'example.txt' , contents : stepTwo } ) )
171
+ . then ( ( ) => exec ( 'git add example.txt' ) )
172
+ . then ( ( ) => exec ( 'git commit -m two' ) )
173
+ . then ( ( ) => exec ( 'git rev-parse HEAD' ) )
174
+ . then ( ( result ) => {
175
+ process . env . GITHUB_SHA = result . stdout . trim ( )
176
+ return processor . process ( vfile ( { path : 'example.txt' , contents : stepTwo } ) )
177
+ } )
178
+ . then ( ( file ) => {
179
+ t . deepEqual (
180
+ file . messages . map ( String ) ,
181
+ [ 'example.txt:5:1-5:6: No lorem!' ] ,
182
+ 'should show only messages for this commit'
183
+ )
184
+ } )
185
+ // A PR.
186
+ . then ( ( ) => exec ( 'git branch --show-current' ) )
187
+ . then ( ( result ) => {
188
+ main = result . stdout . trim ( )
189
+ exec ( 'git checkout -b other-branch' )
190
+ } )
191
+ // Change file.
192
+ . then ( ( ) => vfile . write ( { path : 'example.txt' , contents : stepThree } ) )
193
+ . then ( ( ) => exec ( 'git add example.txt' ) )
194
+ . then ( ( ) => exec ( 'git commit -m three' ) )
195
+ . then ( ( ) => vfile . write ( { path : 'example.txt' , contents : stepFour } ) )
196
+ . then ( ( ) => exec ( 'git add example.txt' ) )
197
+ . then ( ( ) => exec ( 'git commit -m four' ) )
198
+ . then ( ( ) => exec ( 'git rev-parse HEAD' ) )
199
+ . then ( ( result ) => {
200
+ process . env . GITHUB_SHA = result . stdout . trim ( )
201
+ process . env . GITHUB_BASE_REF = 'refs/heads/' + main
202
+ process . env . GITHUB_HEAD_REF = 'refs/heads/other-branch'
203
+ return processor . process ( vfile ( { path : 'example.txt' , contents : stepFour } ) )
204
+ } )
205
+ . then ( ( file ) => {
206
+ t . deepEqual (
207
+ file . messages . map ( String ) ,
208
+ [ 'example.txt:1:1-1:6: No lorem!' , 'example.txt:9:7-9:12: No lorem!' ] ,
209
+ 'should deal with PRs'
210
+ )
211
+ } )
212
+ // Restore
141
213
. then (
142
214
( ) => t . pass ( 'should pass' ) ,
143
215
( error ) => t . ifErr ( error , 'should not fail' )
144
216
)
217
+ . then ( restore , restore )
145
218
146
219
function restore ( ) {
147
- process . env . TRAVIS_COMMIT_RANGE = range
148
220
return rimraf ( '.git' )
149
221
. then ( ( ) => rimraf ( 'new.txt' ) )
150
222
. then ( ( ) => rimraf ( 'example.txt' ) )
151
223
}
152
224
} )
225
+
226
+ process . on ( 'exit' , function ( ) {
227
+ process . env . TRAVIS_COMMIT_RANGE = range
228
+ process . env . GITHUB_SHA = sha
229
+ process . env . GITHUB_BASE_REF = base
230
+ process . env . GITHUB_HEAD_REF = head
231
+ process . chdir ( path . join ( current ) )
232
+ } )
233
+
234
+ function setEmailAndName ( ) {
235
+ return exec ( 'git config --global user.email [email protected] ' ) . then ( setEmail )
236
+ }
237
+
238
+ function setEmail ( ) {
239
+ return exec ( 'git config --global user.name Ex Ample' )
240
+ }
0 commit comments