11'use strict'
22
3- var fs = require ( 'fs ' )
3+ var cp = require ( 'child_process ' )
44var path = require ( 'path' )
5- var trough = require ( 'trough' )
5+ var promisify = require ( 'util' ) . promisify
66var test = require ( 'tape' )
7- var execa = require ( 'execa' )
8- var rimraf = require ( 'rimraf' )
7+ var rimraf = promisify ( require ( 'rimraf' ) )
98var vfile = require ( 'to-vfile' )
109var processor = require ( './processor' ) ( )
1110
11+ var exec = promisify ( cp . exec )
12+
1213test ( 'diff()' , function ( t ) {
1314 var current = process . cwd ( )
1415 var range = process . env . TRAVIS_COMMIT_RANGE
@@ -22,167 +23,130 @@ test('diff()', function (t) {
2223 var stepThree = 'Lorem.\n' + stepOne + '\nLorem.'
2324 var other = 'Lorem ipsum.'
2425 var initial
26+ var final
2527
2628 delete process . env . TRAVIS_COMMIT_RANGE
2729
2830 t . plan ( 7 )
2931
3032 process . chdir ( path . join ( current , 'test' ) )
3133
32- trough ( )
33- . use ( function ( ) {
34- return execa ( 'git' , [ 'init' ] )
35- } )
36- . use ( function ( result , next ) {
37- var file = vfile ( { path : 'example.txt' , contents : stepOne } )
38-
39- processor . process ( file , function ( err ) {
40- if ( err ) {
41- return next ( err )
42- }
43-
44- t . deepEqual (
45- file . messages . map ( String ) ,
46- [ 'example.txt:1:1-1:6: No lorem!' , 'example.txt:3:1-3:6: No lorem!' ] ,
47- 'should set messages'
48- )
34+ exec ( 'git init' )
35+ // Set up.
36+ . then ( ( ) => {
37+ return exec ( 'git config --global user.email' ) . catch ( oncatch )
4938
50- fs . writeFile ( file . path , file . contents , next )
51- } )
52- } )
53- . use ( function ( ) {
54- return execa ( 'git' , [ 'config' , '--global' , 'user.email' ] ) . catch (
55- function ( ) {
56- return execa ( 'git' , [
57- 'config' ,
58- '--global' ,
59- 'user.email' ,
60- 61- ] ) . then ( function ( ) {
62- return execa ( 'git' , [ 'config' , '--global' , 'user.name' , 'Ex Ample' ] )
63- } )
64- }
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+ } )
49+ // Add initial file.
50+ . then ( ( ) =>
51+ processor . process ( vfile ( { path : 'example.txt' , contents : stepOne } ) )
52+ )
53+ . then ( ( file ) => {
54+ t . deepEqual (
55+ file . messages . map ( String ) ,
56+ [ 'example.txt:1:1-1:6: No lorem!' , 'example.txt:3:1-3:6: No lorem!' ] ,
57+ 'should set messages'
6558 )
66- } )
67- . use ( function ( ) {
68- return execa ( 'git' , [ 'add' , 'example.txt' ] )
69- } )
70- . use ( function ( ) {
71- return execa ( 'git' , [ 'commit' , '-m' , 'one' ] )
72- } )
73- . use ( function ( ) {
74- return execa ( 'git' , [ 'rev-parse' , 'HEAD' ] )
75- } )
76- . use ( function ( result , next ) {
77- var file = vfile ( { path : 'example.txt' , contents : stepTwo } )
7859
79- initial = result . stdout
60+ return vfile . write ( file )
61+ } )
62+ . then ( ( ) => exec ( 'git add example.txt' ) )
63+ . then ( ( ) => exec ( 'git commit -m one' ) )
64+ . then ( ( ) => exec ( 'git rev-parse HEAD' ) )
65+ . then ( ( result ) => {
66+ initial = result . stdout . trim ( )
67+ return vfile . write ( { path : 'example.txt' , contents : stepTwo } )
68+ } )
69+ // Changed files.
70+ . then ( ( ) => exec ( 'git add example.txt' ) )
71+ . then ( ( ) => exec ( 'git commit -m two' ) )
72+ . then ( ( ) => exec ( 'git rev-parse HEAD' ) )
73+ . then ( ( result ) => {
74+ final = result . stdout . trim ( )
75+ process . env . TRAVIS_COMMIT_RANGE = [ initial , final ] . join ( '...' )
76+
77+ return processor . process ( vfile ( { path : 'example.txt' , contents : stepTwo } ) )
78+ } )
79+ . then ( ( file ) => {
80+ t . deepEqual (
81+ file . messages . map ( String ) ,
82+ [ 'example.txt:5:1-5:6: No lorem!' ] ,
83+ 'should show only messages for changed lines'
84+ )
8085
81- fs . writeFile ( file . path , file . contents , next )
86+ return file
8287 } )
83- . use ( function ( ) {
84- return execa ( 'git' , [ 'add' , 'example.txt' ] )
88+ // Again!
89+ . then ( ( ) => {
90+ return processor . process ( vfile ( { path : 'example.txt' , contents : stepTwo } ) )
8591 } )
86- . use ( function ( ) {
87- return execa ( 'git' , [ 'commit' , '-m' , 'two' ] )
92+ . then ( ( file ) => {
93+ t . deepEqual (
94+ file . messages . map ( String ) ,
95+ [ 'example.txt:5:1-5:6: No lorem!' ] ,
96+ 'should not recheck (coverage for optimisations)'
97+ )
8898 } )
89- . use ( function ( ) {
90- return execa ( 'git' , [ 'rev-parse' , 'HEAD' ] )
99+ // Unstages files.
100+ . then ( ( ) => {
101+ return processor . process ( vfile ( { path : 'missing.txt' , contents : other } ) )
91102 } )
92- . use ( function ( result , next ) {
93- var file = vfile ( { path : 'example.txt' , contents : stepTwo } )
94-
95- process . env . TRAVIS_COMMIT_RANGE = [ initial , result . stdout ] . join ( '...' )
96-
97- processor . process ( file , function ( err ) {
98- t . deepEqual (
99- file . messages . join ( '' ) ,
100- 'example.txt:5:1-5:6: No lorem!' ,
101- 'should show only messages for changed lines'
102- )
103-
104- next ( err )
105- } )
103+ . then ( ( file ) => {
104+ t . deepEqual ( file . messages . map ( String ) , [ ] , 'should ignore unstaged files' )
106105 } )
107- . use ( function ( result , next ) {
108- var file = vfile ( { path : 'example.txt' , contents : stepTwo } )
109-
110- processor . process ( file , function ( err ) {
111- t . deepEqual (
112- file . messages . join ( '' ) ,
113- 'example.txt:5:1-5:6: No lorem!' ,
114- 'should not recheck (coverage for optimisations)'
115- )
106+ // New file.
107+ . then ( ( ) => vfile . write ( { path : 'example.txt' , contents : stepThree } ) )
108+ . then ( ( ) => vfile . write ( { path : 'new.txt' , contents : other } ) )
109+ . then ( ( ) => exec ( 'git add example.txt new.txt' ) )
110+ . then ( ( ) => exec ( 'git commit -m three' ) )
111+ . then ( ( ) => exec ( 'git rev-parse HEAD' ) )
112+ . then ( ( result ) => {
113+ final = result . stdout . trim ( )
116114
117- next ( err )
118- } )
119- } )
120- . use ( function ( result , next ) {
121- var file = vfile ( { path : 'missing.txt' , contents : other } )
115+ process . env . TRAVIS_COMMIT_RANGE = [ initial , final ] . join ( '...' )
122116
123- processor . process ( file , function ( err ) {
124- t . deepEqual ( file . messages , [ ] , 'should ignore unstaged files' )
125- next ( err )
126- } )
127- } )
128- . use ( function ( result , next ) {
129- var file = vfile ( { path : 'new.txt' , contents : other } )
130- fs . writeFile ( file . path , file . contents , next )
131- } )
132- . use ( function ( result , next ) {
133- var file = vfile ( { path : 'example.txt' , contents : stepThree } )
134- fs . writeFile ( file . path , file . contents , next )
135- } )
136- . use ( function ( ) {
137- return execa ( 'git' , [ 'add' , 'example.txt' , 'new.txt' ] )
138- } )
139- . use ( function ( ) {
140- return execa ( 'git' , [ 'commit' , '-m' , 'three' ] )
141- } )
142- . use ( function ( ) {
143- return execa ( 'git' , [ 'rev-parse' , 'HEAD' ] )
117+ return processor . process (
118+ vfile ( { path : 'example.txt' , contents : stepThree } )
119+ )
144120 } )
145- . use ( function ( result , next ) {
146- var file = vfile ( { path : 'example.txt' , contents : stepTwo } )
147-
148- process . env . TRAVIS_COMMIT_RANGE = [ initial , result . stdout ] . join ( '...' )
149-
150- processor . process ( file , function ( err ) {
151- t . deepEqual (
152- file . messages . map ( String ) ,
153- [ 'example.txt:1:1-1:6: No lorem!' , 'example.txt:5:1-5:6: No lorem!' ] ,
154- 'should deal with multiple patches'
155- )
121+ . then ( ( file ) => {
122+ t . deepEqual (
123+ file . messages . map ( String ) ,
124+ [ 'example.txt:1:1-1:6: No lorem!' , 'example.txt:6:1-6:6: No lorem!' ] ,
125+ 'should deal with multiple patches'
126+ )
156127
157- next ( err )
158- } )
128+ return processor . process ( vfile ( { path : 'new.txt' , contents : other } ) )
159129 } )
160- . use ( function ( result , next ) {
161- var file = vfile ( { path : 'new.txt' , contents : other } )
162-
163- processor . process ( file , function ( err ) {
164- t . deepEqual (
165- file . messages . join ( '' ) ,
166- 'new.txt:1:1-1:6: No lorem!' ,
167- 'should deal with new files'
168- )
130+ . then ( ( file ) => {
131+ t . deepEqual (
132+ file . messages . map ( String ) ,
133+ [ 'new.txt:1:1-1:6: No lorem!' ] ,
134+ 'should deal with new files'
135+ )
169136
170- next ( err )
171- } )
172- } )
173- . use ( function ( ) {
174- process . env . TRAVIS_COMMIT_RANGE = range
175- } )
176- . use ( function ( result , next ) {
177- rimraf ( '.git' , next )
178- } )
179- . use ( function ( result , next ) {
180- rimraf ( 'new.txt' , next )
181- } )
182- . use ( function ( result , next ) {
183- rimraf ( 'example.txt' , next )
184- } )
185- . run ( function ( err ) {
186- t . ifErr ( err , 'should not fail' )
187- } )
137+ return processor . process ( vfile ( { path : 'new.txt' , contents : other } ) )
138+ } )
139+ // Restore
140+ . then ( restore , restore )
141+ . then (
142+ ( ) => t . pass ( 'should pass' ) ,
143+ ( error ) => t . ifErr ( error , 'should not fail' )
144+ )
145+
146+ function restore ( ) {
147+ process . env . TRAVIS_COMMIT_RANGE = range
148+ return rimraf ( '.git' )
149+ . then ( ( ) => rimraf ( 'new.txt' ) )
150+ . then ( ( ) => rimraf ( 'example.txt' ) )
151+ }
188152} )
0 commit comments