@@ -193,10 +193,10 @@ describe("oasDiffChangelog", () => {
193193 execStub . callsArgWith ( 1 , null , "version 1.0.0" , "" ) ;
194194 execStub
195195 . onSecondCall ( )
196- . callsArgWith ( 1 , null , '{"changes": "in api-v1"}' , "" ) ;
196+ . callsArgWith ( 1 , null , '[ {"changes": "in api-v1"}] ' , "" ) ;
197197 execStub
198198 . onThirdCall ( )
199- . callsArgWith ( 1 , null , '{"changes": "in api-v2"}' , "" ) ;
199+ . callsArgWith ( 1 , null , '[ {"changes": "in api-v2"}] ' , "" ) ;
200200
201201 const fsStub = {
202202 readdir : sinon . stub ( ) . returns ( [ "api-v1" , "api-v2" ] ) ,
@@ -226,13 +226,11 @@ describe("oasDiffChangelog", () => {
226226 expect ( writtenContent ) . to . be . an ( "array" ) . with . lengthOf ( 2 ) ;
227227 expect ( writtenContent [ 0 ] ) . to . deep . equal ( {
228228 directory : "api-v1" ,
229- status : "modified" ,
230- changes : { changes : "in api-v1" } ,
229+ changes : [ { changes : "in api-v1" } ] ,
231230 } ) ;
232231 expect ( writtenContent [ 1 ] ) . to . deep . equal ( {
233232 directory : "api-v2" ,
234- status : "modified" ,
235- changes : { changes : "in api-v2" } ,
233+ changes : [ { changes : "in api-v2" } ] ,
236234 } ) ;
237235 } ) ;
238236
@@ -526,6 +524,102 @@ describe("oasDiffChangelog", () => {
526524 } ) ;
527525 } ) ;
528526
527+ it ( "should not include directories with empty changes in JSON format" , async ( ) => {
528+ const execStub = sinon . stub ( ) ;
529+ execStub . callsArgWith ( 1 , null , "version 1.0.0" , "" ) ;
530+ execStub
531+ . onSecondCall ( )
532+ . callsArgWith ( 1 , null , '[{"changes": "in api-v1"}]' , "" ) ;
533+ execStub . onThirdCall ( ) . callsArgWith ( 1 , null , "[]" , "" ) ; // empty array
534+
535+ const fsStub = {
536+ readdir : sinon . stub ( ) . returns ( [ "api-v1" , "api-v2" ] ) ,
537+ stat : sinon . stub ( ) . returns ( { isDirectory : ( ) => true } ) ,
538+ writeJson : sinon . stub ( ) ,
539+ } ;
540+
541+ const oasDiff = pq ( "./oasDiff" , {
542+ child_process : {
543+ exec : execStub ,
544+ } ,
545+ "fs-extra" : fsStub ,
546+ } ) ;
547+
548+ const baseApi = "base" ;
549+ const newApi = "new" ;
550+ const flags = {
551+ "out-file" : "output.json" ,
552+ format : "json" ,
553+ dir : true ,
554+ } ;
555+
556+ await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
557+
558+ expect ( fsStub . writeJson . called ) . to . be . true ;
559+ const writtenContent = fsStub . writeJson . args [ 0 ] [ 1 ] ;
560+ expect ( writtenContent ) . to . be . an ( "array" ) . with . lengthOf ( 1 ) ;
561+ expect ( writtenContent [ 0 ] ) . to . deep . equal ( {
562+ directory : "api-v1" ,
563+ changes : [ { changes : "in api-v1" } ] ,
564+ } ) ;
565+ } ) ;
566+
567+ it ( "should not include empty results in single file JSON mode" , async ( ) => {
568+ const execStub = sinon . stub ( ) ;
569+ execStub . callsArgWith ( 1 , null , "" , "" ) ; // version check
570+ execStub . onSecondCall ( ) . callsArgWith ( 1 , null , "[]" , "" ) ; // empty array result
571+
572+ const fsStub = {
573+ readdir : sinon . stub ( ) . returns ( [ "api-v1" ] ) ,
574+ stat : sinon . stub ( ) . returns ( { isDirectory : ( ) => true } ) ,
575+ } ;
576+
577+ const oasDiff = pq ( "./oasDiff" , {
578+ child_process : {
579+ exec : execStub ,
580+ } ,
581+ "fs-extra" : fsStub ,
582+ } ) ;
583+
584+ // Arrange
585+ const baseApi = "base.yaml" ;
586+ const newApi = "new.yaml" ;
587+ const flags = { format : "json" } ;
588+ const result = await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
589+
590+ expect ( execStub . called ) . to . be . true ;
591+ expect ( result ) . to . equal ( 0 ) ; // No changes should be reported
592+ } ) ;
593+
594+ it ( "should include non-empty results in single file JSON mode" , async ( ) => {
595+ const execStub = sinon . stub ( ) ;
596+ execStub . callsArgWith ( 1 , null , "" , "" ) ; // version check
597+ execStub
598+ . onSecondCall ( )
599+ . callsArgWith ( 1 , null , '[{"change": "something"}]' , "" ) ; // non-empty array result
600+
601+ const fsStub = {
602+ readdir : sinon . stub ( ) . returns ( [ "api-v1" ] ) ,
603+ stat : sinon . stub ( ) . returns ( { isDirectory : ( ) => true } ) ,
604+ } ;
605+
606+ const oasDiff = pq ( "./oasDiff" , {
607+ child_process : {
608+ exec : execStub ,
609+ } ,
610+ "fs-extra" : fsStub ,
611+ } ) ;
612+
613+ // Arrange
614+ const baseApi = "base.yaml" ;
615+ const newApi = "new.yaml" ;
616+ const flags = { format : "json" } ;
617+ const result = await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
618+
619+ expect ( execStub . called ) . to . be . true ;
620+ expect ( result ) . to . equal ( 1 ) ; // Changes should be reported
621+ } ) ;
622+
529623 it ( "should throw an error if oasdiff is not installed" , async ( ) => {
530624 const execStub = sinon . stub ( ) ;
531625 execStub . callsArgWith ( 1 , new Error ( "oasdiff not installed" ) ) ;
0 commit comments