@@ -678,4 +678,79 @@ describe("oasDiffChangelog", () => {
678678 expect ( execStub . called ) . to . be . true ;
679679 expect ( result ) . to . equal ( 1 ) ; // Changes should be reported
680680 } ) ;
681+
682+ it ( "should normalize directory names when normalize-directory-names flag is passed" , async ( ) => {
683+ const execStub = createMockExec ( ) ;
684+ execStub . onSecondCall ( ) . callsArgWith ( 1 , null , "changes in api-v1" , "" ) ;
685+
686+ const fsStub = createMockFs ( ) ;
687+ // Setup directory structure with versioned directory names
688+ setupDirectoryStructure ( fsStub , [
689+ { path : "base" , contents : [ "api-v1.0.16" , "api-v2.1.5" ] } ,
690+ { path : "base/api-v1.0.16" , contents : [ "exchange.json" , "spec.yaml" ] } ,
691+ { path : "base/api-v2.1.5" , contents : [ "exchange.json" , "spec.yaml" ] } ,
692+ { path : "new" , contents : [ "api-v1.0.17" , "api-v2.1.6" ] } ,
693+ { path : "new/api-v1.0.17" , contents : [ "exchange.json" , "spec.yaml" ] } ,
694+ { path : "new/api-v2.1.6" , contents : [ "exchange.json" , "spec.yaml" ] } ,
695+ ] ) ;
696+
697+ const oasDiff = createOasDiffProxy ( execStub , fsStub ) ;
698+
699+ const baseApi = "base" ;
700+ const newApi = "new" ;
701+ const flags = {
702+ "out-file" : "output.txt" ,
703+ dir : true ,
704+ "normalize-directory-names" : true ,
705+ } ;
706+
707+ await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
708+
709+ expect ( fsStub . writeFile . called ) . to . be . true ;
710+ const writtenContent = fsStub . writeFile . args [ 0 ] [ 1 ] ;
711+
712+ // Verify that the function completes successfully with the normalize-directory-names flag enabled
713+ expect ( writtenContent ) . to . be . a ( "string" ) ;
714+ expect ( writtenContent . length ) . to . be . greaterThan ( 0 ) ;
715+
716+ // The test verifies that the normalize-directory-names flag is being processed
717+ // and that the function completes successfully with the flag enabled
718+ } ) ;
719+
720+ it ( "should handle fs.readdir error in findYamlFiles function" , async ( ) => {
721+ const consoleWarnSpy = sinon . spy ( console , "warn" ) ;
722+ const execStub = createMockExec ( ) ;
723+ execStub . onSecondCall ( ) . callsArgWith ( 1 , null , "changes in api-v1" , "" ) ;
724+
725+ const fsStub = createMockFs ( ) ;
726+ setupDirectoryStructure ( fsStub , [
727+ { path : "base" , contents : [ "api-v1" ] } ,
728+ { path : "base/api-v1" , contents : [ "exchange.json" , "spec.yaml" ] } ,
729+ { path : "new" , contents : [ "api-v1" ] } ,
730+ { path : "new/api-v1" , contents : [ "exchange.json" , "spec.yaml" ] } ,
731+ ] ) ;
732+
733+ // Make readdir fail for the new/api-v1 directory to trigger the error handling branch
734+ fsStub . readdir
735+ . withArgs ( "new/api-v1" )
736+ . rejects ( new Error ( "Permission denied" ) ) ;
737+
738+ const oasDiff = createOasDiffProxy ( execStub , fsStub ) ;
739+
740+ const baseApi = "base" ;
741+ const newApi = "new" ;
742+ const flags = {
743+ dir : true ,
744+ } ;
745+
746+ await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
747+
748+ // Verify that the function handles the error gracefully
749+ expect ( consoleWarnSpy . called ) . to . be . true ;
750+ expect ( consoleWarnSpy . args [ 0 ] [ 0 ] ) . to . include (
751+ "Warning: Could not read directory new/api-v1:"
752+ ) ;
753+ expect ( consoleWarnSpy . args [ 0 ] [ 1 ] ) . to . include ( "Permission denied" ) ;
754+ consoleWarnSpy . restore ( ) ;
755+ } ) ;
681756} ) ;
0 commit comments