@@ -2,6 +2,7 @@ const assert = require('assert');
22const fs = require ( 'fs' ) ;
33const newline = require ( 'newline' ) ;
44const sortPackageJson = require ( './' ) ;
5+ const { execFile } = require ( 'child_process' ) ;
56
67fs . readFile ( './package.json' , 'utf8' , ( error , contents ) => {
78 if ( error ) {
@@ -117,3 +118,76 @@ fs.readFile('./package.json', 'utf8', (error, contents) => {
117118 ) ;
118119} ) ;
119120
121+
122+ // CLI `--check` flag tests
123+
124+ // make sure `--check` not fixing file
125+ // support `-c` as well
126+ const orignal = fs . readFileSync ( 'fixtures/not-sorted-1/package.json' , 'utf8' ) ;
127+ execFile (
128+ 'node' ,
129+ [ 'index.js' , 'fixtures/not-sorted-1/package.json' , '-c' ] ,
130+ ( error , stdout , stderr ) => {
131+ assert . notEqual (
132+ orignal ,
133+ sortPackageJson ( orignal ) ,
134+ 'fixtures/not-sorted-1/package.json should be a unsorted file.'
135+ ) ;
136+ assert . equal (
137+ orignal ,
138+ fs . readFileSync ( 'fixtures/not-sorted-1/package.json' , 'utf8' ) ,
139+ 'file should not fixed when --check is enabled.'
140+ ) ;
141+ assert . equal ( error . code , 1 , 'error.code should equals to unsorted file length' ) ;
142+ assert . equal ( stderr , '' ) ;
143+ assert . equal ( stdout . trim ( ) , 'fixtures/not-sorted-1/package.json\n1 file is not sorted.' ) ;
144+ }
145+ ) ;
146+
147+
148+ execFile (
149+ 'node' ,
150+ [ 'index.js' , 'fixtures/not-sorted-*/package.json' , '--check' ] ,
151+ ( error , stdout , stderr ) => {
152+ assert . equal ( error . code , 2 ) ;
153+ assert . equal ( stderr , '' ) ;
154+ assert . equal ( stdout . includes ( 'fixtures/not-sorted-1/package.json' ) , true ) ;
155+ assert . equal ( stdout . includes ( 'fixtures/not-sorted-2/package.json' ) , true ) ;
156+ assert . equal ( stdout . includes ( '2 files are not sorted.' ) , true ) ;
157+ }
158+ ) ;
159+
160+ execFile (
161+ 'node' ,
162+ [ 'index.js' , 'fixtures/sorted-1/package.json' , '--check' ] ,
163+ ( error , stdout , stderr ) => {
164+ assert . equal ( error , null ) ;
165+ assert . equal ( stderr , '' ) ;
166+ assert . equal ( stdout . trim ( ) , 'file is sorted.' ) ;
167+ }
168+ ) ;
169+
170+ execFile (
171+ 'node' ,
172+ [ 'index.js' , 'fixtures/sorted-*/package.json' , '--check' ] ,
173+ ( error , stdout , stderr ) => {
174+ assert . equal ( error , null ) ;
175+ assert . equal ( stderr , '' ) ;
176+ assert . equal ( stdout . trim ( ) , 'all files are sorted.' ) ;
177+ }
178+ ) ;
179+
180+ execFile (
181+ 'node' ,
182+ [ 'index.js' , 'fixtures/*/package.json' , '--check' ] ,
183+ ( error , stdout , stderr ) => {
184+ assert . equal ( error . code , 3 ) ;
185+ assert . equal ( stderr , '' ) ;
186+ assert . equal ( stdout . includes ( 'fixtures/sorted-1/package.json' ) , false ) ;
187+ assert . equal ( stdout . includes ( 'fixtures/sorted-2/package.json' ) , false ) ;
188+ assert . equal ( stdout . includes ( 'fixtures/not-sorted-1/package.json' ) , true ) ;
189+ assert . equal ( stdout . includes ( 'fixtures/not-sorted-2/package.json' ) , true ) ;
190+ assert . equal ( stdout . includes ( 'fixtures/another-not-sorted/package.json' ) , true ) ;
191+ assert . equal ( stdout . includes ( '3 files are not sorted.' ) , true ) ;
192+ }
193+ ) ;
0 commit comments