@@ -6,16 +6,18 @@ import { fileURLToPath } from 'url';
66import { parseJsonxl , style } from './helpers.js' ;
77
88const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
9- const pkgJson = path . join ( __dirname , '../package.json ' ) ;
10- const pkgJsonData = JSON . parse ( fs . readFileSync ( pkgJson ) ) ;
11- const fixture = fs . readFileSync ( path . join ( __dirname , 'color-fixture.json' ) , 'utf8' ) ;
9+ const queryFilename = path . join ( __dirname , 'query.jora ' ) ;
10+ const fixtureFilename = path . join ( __dirname , 'fixture.json' ) ;
11+ const fixture = fs . readFileSync ( fixtureFilename , 'utf8' ) ;
1212const fixtureData = JSON . parse ( fixture ) ;
13- const fixtureJsonxlFilename = path . join ( __dirname , './ fixture.jsonxl' ) ;
13+ const fixtureJsonxlFilename = path . join ( __dirname , 'fixture.jsonxl' ) ;
1414const fixtureJsonxl = fs . readFileSync ( fixtureJsonxlFilename ) ;
1515const fixtureJsonxlData = parseJsonxl ( fixtureJsonxl ) ;
1616const fixtureJsonxlJson = JSON . stringify ( fixtureJsonxlData ) ;
17- const fixtureExpected = fs . readFileSync ( path . join ( __dirname , 'color-fixture.expected' ) , 'utf8' ) . trim ( ) ;
18- const fixtureExpectedCompact = fs . readFileSync ( path . join ( __dirname , 'color-fixture.compact.expected' ) , 'utf8' ) . trim ( ) ;
17+ const colorFixture = fs . readFileSync ( path . join ( __dirname , 'color-fixture.json' ) , 'utf8' ) ;
18+ const colorFixtureData = JSON . parse ( colorFixture ) ;
19+ const colorFixtureExpected = fs . readFileSync ( path . join ( __dirname , 'color-fixture.expected' ) , 'utf8' ) . trim ( ) ;
20+ const colorFixtureExpectedCompact = fs . readFileSync ( path . join ( __dirname , 'color-fixture.compact.expected' ) , 'utf8' ) . trim ( ) ;
1921const envWithForceColors = Object . assign ( { } , process . env , {
2022 FORCE_COLOR : true
2123} ) ;
@@ -116,20 +118,34 @@ describe('non-JSON primitives', () => {
116118
117119it ( 'should output version' , ( ) =>
118120 run ( '-v' )
119- . output ( pkgJsonData . version )
121+ . output ( fixtureData . version )
120122) ;
121123
122124it ( 'should read content from stdin if no file specified' , ( ) =>
123125 run ( 'version' )
124- . input ( JSON . stringify ( pkgJsonData ) )
125- . output ( JSON . stringify ( pkgJsonData . version ) )
126+ . input ( fixture )
127+ . output ( JSON . stringify ( fixtureData . version ) )
126128) ;
127129
128130it ( 'should read from file' , ( ) =>
129- run ( '-i' , pkgJson , '-q' , 'version' )
130- . output ( JSON . stringify ( pkgJsonData . version ) )
131+ run ( '-i' , fixtureFilename , '-q' , 'version' )
132+ . output ( JSON . stringify ( fixtureData . version ) )
131133) ;
132134
135+ describe ( 'query from a file' , ( ) => {
136+ it ( 'as arg' , ( ) =>
137+ run ( queryFilename )
138+ . input ( fixture )
139+ . output ( JSON . stringify ( fixtureData . version ) )
140+ ) ;
141+
142+ it ( 'as option' , ( ) =>
143+ run ( '-q' , queryFilename )
144+ . input ( fixture )
145+ . output ( JSON . stringify ( fixtureData . version ) )
146+ ) ;
147+ } ) ;
148+
133149describe ( 'jsonxl' , ( ) => {
134150 it ( 'should read from jsonxl file' , ( ) =>
135151 run ( '-i' , fixtureJsonxlFilename , '-q' , 'version' )
@@ -158,14 +174,14 @@ describe('jsonxl', () => {
158174describe ( 'pretty print' , function ( ) {
159175 it ( 'indentation should be 4 spaces by default' , ( ) =>
160176 run ( 'dependencies.keys()' , '-p' )
161- . input ( JSON . stringify ( pkgJsonData ) )
162- . output ( JSON . stringify ( Object . keys ( pkgJsonData . dependencies ) , null , 4 ) )
177+ . input ( fixture )
178+ . output ( JSON . stringify ( Object . keys ( fixtureData . dependencies ) , null , 4 ) )
163179 ) ;
164180
165181 it ( 'indentation should be as specified' , ( ) =>
166182 run ( 'dependencies.keys()' , '-p' , '3' )
167- . input ( JSON . stringify ( pkgJsonData ) )
168- . output ( JSON . stringify ( Object . keys ( pkgJsonData . dependencies ) , null , 3 ) )
183+ . input ( fixture )
184+ . output ( JSON . stringify ( Object . keys ( fixtureData . dependencies ) , null , 3 ) )
169185 ) ;
170186} ) ;
171187
@@ -194,10 +210,10 @@ describe('errors', function() {
194210
195211// FIXME: skip colored output tests for Windows since no way currently to pass custom env variable (FORCE_COLOR) to a child process
196212// FIXME: --color temporary disabled
197- ( false && process . platform !== 'win32' ? describe : describe . skip ) ( 'colored output' , function ( ) {
213+ ( process . platform !== 'win32' ? describe : describe . skip ) ( 'colored output' , function ( ) {
198214 const tests = {
199- string : style ( 'STRING' , JSON . stringify ( fixtureData . string ) ) ,
200- number : style ( 'NUMBER' , fixtureData . number ) ,
215+ string : style ( 'STRING' , JSON . stringify ( colorFixtureData . string ) ) ,
216+ number : style ( 'NUMBER' , colorFixtureData . number ) ,
201217 emptyArray : style ( 'LEFT_BRACKET' , '[' , 'RIGHT_BRACKET' , ']' ) ,
202218 emptyObject : style ( 'LEFT_BRACE' , '{' , 'RIGHT_BRACE' , '}' ) ,
203219 singlePropObject : style ( 'LEFT_BRACE' , '{' , 'STRING_KEY' , '"' , 'STRING_KEY_CONTENT' , 'foo' , 'STRING_KEY' , '"' , 'COLON' , ':' , 'STRING' , '"test"' , 'RIGHT_BRACE' , '}' ) ,
@@ -209,7 +225,7 @@ describe('errors', function() {
209225 Object . keys ( tests ) . forEach ( key => {
210226 it ( key , ( ) =>
211227 runWithForceColors ( key )
212- . input ( fixture )
228+ . input ( colorFixture )
213229 . output ( tests [ key ] )
214230 ) ;
215231 } ) ;
@@ -230,21 +246,21 @@ describe('errors', function() {
230246 // FORCE_COLOR=true node bin/jora <test/color-fixture.json >test/color-fixture.compact.expected
231247 it ( 'compact' , ( ) =>
232248 runWithForceColors ( )
233- . input ( fixture )
234- . output ( fixtureExpectedCompact )
249+ . input ( colorFixture )
250+ . output ( colorFixtureExpectedCompact )
235251 ) ;
236252
237253 // FORCE_COLOR=true node bin/jora -p <test/color-fixture.json >test/color-fixture.expected
238254 it ( 'pretty print' , ( ) =>
239255 runWithForceColors ( '-p' )
240- . input ( fixture )
241- . output ( fixtureExpected )
256+ . input ( colorFixture )
257+ . output ( colorFixtureExpected )
242258 ) ;
243259 } ) ;
244260
245261 it ( '--no-color should suppress output coloring' , ( ) =>
246262 runWithForceColors ( '--no-color' )
247- . input ( fixture )
248- . output ( JSON . stringify ( fixtureData ) )
263+ . input ( colorFixture )
264+ . output ( JSON . stringify ( colorFixtureData ) )
249265 ) ;
250266} ) ;
0 commit comments