@@ -11,15 +11,21 @@ import sinon from "sinon";
1111const pq = proxyquire . noCallThru ( ) ;
1212
1313describe ( "oasDiffChangelog" , ( ) => {
14- it ( "should execute oasdiff command with correct parameters" , async ( ) => {
15- const stub = sinon . stub ( ) ;
16- stub . onCall ( 0 ) . returns ( "version 1.0.0" ) ;
17- stub . onCall ( 1 ) . returns ( "" ) ;
14+ it ( "should execute oasdiff command with correct parameters for single file mode" , async ( ) => {
15+ const execSyncStub = sinon . stub ( ) ;
16+ execSyncStub . onCall ( 0 ) . returns ( "version 1.0.0" ) ; // version check
17+ execSyncStub . onCall ( 1 ) . returns ( "" ) ; // diff result
18+
19+ const fsStub = {
20+ readdir : sinon . stub ( ) . returns ( [ "api-v1" ] ) ,
21+ stat : sinon . stub ( ) . returns ( { isDirectory : ( ) => true } ) ,
22+ } ;
1823
1924 const oasDiff = pq ( "./oasDiff" , {
2025 child_process : {
21- execSync : stub ,
26+ execSync : execSyncStub ,
2227 } ,
28+ "fs-extra" : fsStub ,
2329 } ) ;
2430
2531 // Arrange
@@ -28,7 +34,36 @@ describe("oasDiffChangelog", () => {
2834 const flags = { } ;
2935 const result = await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
3036
31- expect ( stub . called ) . to . be . true ;
37+ expect ( execSyncStub . called ) . to . be . true ;
38+ expect ( execSyncStub . args [ 1 ] [ 0 ] ) . to . equal ( 'oasdiff changelog "base.yaml" "new.yaml"' ) ;
39+ expect ( result ) . to . equal ( 0 ) ;
40+ } ) ;
41+
42+ it ( "should execute oasdiff command with correct parameters for directory mode" , async ( ) => {
43+ const execSyncStub = sinon . stub ( ) ;
44+ execSyncStub . onCall ( 0 ) . returns ( "version 1.0.0" ) ; // version check
45+ execSyncStub . onCall ( 1 ) . returns ( "" ) ; // diff result
46+
47+ const fsStub = {
48+ readdir : sinon . stub ( ) . returns ( [ "api-v1" ] ) ,
49+ stat : sinon . stub ( ) . returns ( { isDirectory : ( ) => true } ) ,
50+ } ;
51+
52+ const oasDiff = pq ( "./oasDiff" , {
53+ child_process : {
54+ execSync : execSyncStub ,
55+ } ,
56+ "fs-extra" : fsStub ,
57+ } ) ;
58+
59+ // Arrange
60+ const baseApi = "base" ;
61+ const newApi = "new" ;
62+ const flags = { dir : true } ;
63+ const result = await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
64+
65+ expect ( execSyncStub . called ) . to . be . true ;
66+ expect ( execSyncStub . args [ 1 ] [ 0 ] ) . to . include ( '"base/api-v1/**/*.yaml"' ) ;
3267 expect ( result ) . to . equal ( 0 ) ;
3368 } ) ;
3469
@@ -37,15 +72,20 @@ describe("oasDiffChangelog", () => {
3772 execSyncStub . onCall ( 0 ) . returns ( "version 1.0.0" ) ;
3873 execSyncStub . onCall ( 1 ) . returns ( "mock oasdiff change" ) ;
3974
75+ const fsStub = {
76+ readdir : sinon . stub ( ) . returns ( [ "api-v1" ] ) ,
77+ stat : sinon . stub ( ) . returns ( { isDirectory : ( ) => true } ) ,
78+ } ;
79+
4080 const oasDiff = pq ( "./oasDiff" , {
4181 child_process : {
4282 execSync : execSyncStub ,
4383 } ,
84+ "fs-extra" : fsStub ,
4485 } ) ;
4586
46- // Arrange
47- const baseApi = "base.yaml" ;
48- const newApi = "new.yaml" ;
87+ const baseApi = "base" ;
88+ const newApi = "new" ;
4989 const flags = { } ;
5090 const result = await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
5191
@@ -58,15 +98,20 @@ describe("oasDiffChangelog", () => {
5898 execSyncStub . onCall ( 0 ) . returns ( "version 1.0.0" ) ;
5999 execSyncStub . onCall ( 1 ) . throws ( new Error ( "mock oasdiff error" ) ) ;
60100
101+ const fsStub = {
102+ readdir : sinon . stub ( ) . returns ( [ "api-v1" ] ) ,
103+ stat : sinon . stub ( ) . returns ( { isDirectory : ( ) => true } ) ,
104+ } ;
105+
61106 const oasDiff = pq ( "./oasDiff" , {
62107 child_process : {
63108 execSync : execSyncStub ,
64109 } ,
110+ "fs-extra" : fsStub ,
65111 } ) ;
66112
67- // Arrange
68- const baseApi = "base.yaml" ;
69- const newApi = "new.yaml" ;
113+ const baseApi = "base" ;
114+ const newApi = "new" ;
70115 const flags = { } ;
71116 const result = await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
72117
@@ -79,76 +124,137 @@ describe("oasDiffChangelog", () => {
79124 execSyncStub . onCall ( 0 ) . returns ( "version 1.0.0" ) ;
80125 execSyncStub . onCall ( 1 ) . returns ( "a change" ) ;
81126
127+ const fsStub = {
128+ readdir : sinon . stub ( ) . returns ( [ "api-v1" ] ) ,
129+ stat : sinon . stub ( ) . returns ( { isDirectory : ( ) => true } ) ,
130+ } ;
131+
82132 const oasDiff = pq ( "./oasDiff" , {
83133 child_process : {
84134 execSync : execSyncStub ,
85135 } ,
136+ "fs-extra" : fsStub ,
86137 } ) ;
87138
88- const baseApi = "base.yaml " ;
89- const newApi = "new.yaml " ;
139+ const baseApi = "base" ;
140+ const newApi = "new" ;
90141 const flags = {
91142 dir : true ,
92143 } ;
93144 await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
94145
95146 expect ( execSyncStub . called ) . to . be . true ;
96147 expect ( execSyncStub . args [ 1 ] [ 0 ] ) . to . equal (
97- 'oasdiff changelog --composed "base.yaml/ **/*.yaml" "new.yaml /**/*.yaml"'
148+ 'oasdiff changelog --composed "base/api-v1/ **/*.yaml" "new/api-v1 /**/*.yaml"'
98149 ) ;
99150 } ) ;
100151
101- it ( "should save the changes to a file when the --out-file flag is provided " , async ( ) => {
152+ it ( "should concatenate results from multiple directories in text format " , async ( ) => {
102153 const execSyncStub = sinon . stub ( ) ;
103154 execSyncStub . onCall ( 0 ) . returns ( "version 1.0.0" ) ;
104- execSyncStub . onCall ( 1 ) . returns ( "a change" ) ;
105- const fsStub = sinon . stub ( ) ;
155+ execSyncStub . onCall ( 1 ) . returns ( "changes in api-v1" ) ;
156+ execSyncStub . onCall ( 2 ) . returns ( "changes in api-v2" ) ;
157+
158+ const fsStub = {
159+ readdir : sinon . stub ( ) . returns ( [ "api-v1" , "api-v2" ] ) ,
160+ stat : sinon . stub ( ) . returns ( { isDirectory : ( ) => true } ) ,
161+ writeFile : sinon . stub ( ) ,
162+ } ;
106163
107164 const oasDiff = pq ( "./oasDiff" , {
108165 child_process : {
109166 execSync : execSyncStub ,
110167 } ,
111- "fs-extra" : {
112- writeFile : fsStub ,
113- } ,
168+ "fs-extra" : fsStub ,
114169 } ) ;
115170
116- // Arrange
117- const baseApi = "base.yaml" ;
118- const newApi = "new.yaml" ;
171+ const baseApi = "base" ;
172+ const newApi = "new" ;
119173 const flags = {
120174 "out-file" : "output.txt" ,
121175 } ;
122176
123177 await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
124- expect ( fsStub . called ) . to . be . true ;
178+
179+ expect ( fsStub . writeFile . called ) . to . be . true ;
180+ const writtenContent = fsStub . writeFile . args [ 0 ] [ 1 ] ;
181+ expect ( writtenContent ) . to . include ( "=== Changes in api-v1 ===" ) ;
182+ expect ( writtenContent ) . to . include ( "changes in api-v1" ) ;
183+ expect ( writtenContent ) . to . include ( "=== Changes in api-v2 ===" ) ;
184+ expect ( writtenContent ) . to . include ( "changes in api-v2" ) ;
125185 } ) ;
126186
127- it ( "should save the changes to a jsonfile when the --out-file flag is provided and format is json " , async ( ) => {
187+ it ( "should concatenate results from multiple directories in JSON format" , async ( ) => {
128188 const execSyncStub = sinon . stub ( ) ;
129189 execSyncStub . onCall ( 0 ) . returns ( "version 1.0.0" ) ;
130- execSyncStub . onCall ( 1 ) . returns ( '{"change": "a change"}' ) ;
131- const fsStub = sinon . stub ( ) ;
190+ execSyncStub . onCall ( 1 ) . returns ( '{"changes": "in api-v1"}' ) ;
191+ execSyncStub . onCall ( 2 ) . returns ( '{"changes": "in api-v2"}' ) ;
192+
193+ const fsStub = {
194+ readdir : sinon . stub ( ) . returns ( [ "api-v1" , "api-v2" ] ) ,
195+ stat : sinon . stub ( ) . returns ( { isDirectory : ( ) => true } ) ,
196+ writeJson : sinon . stub ( ) ,
197+ } ;
132198
133199 const oasDiff = pq ( "./oasDiff" , {
134200 child_process : {
135201 execSync : execSyncStub ,
136202 } ,
137- "fs-extra" : {
138- writeJson : fsStub ,
139- } ,
203+ "fs-extra" : fsStub ,
140204 } ) ;
141205
142- // Arrange
143- const baseApi = "base.yaml" ;
144- const newApi = "new.yaml" ;
206+ const baseApi = "base" ;
207+ const newApi = "new" ;
145208 const flags = {
146209 "out-file" : "output.json" ,
147210 format : "json" ,
148211 } ;
149212
150213 await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
151- expect ( fsStub . called ) . to . be . true ;
214+
215+ expect ( fsStub . writeJson . called ) . to . be . true ;
216+ const writtenContent = fsStub . writeJson . args [ 0 ] [ 1 ] ;
217+ expect ( writtenContent ) . to . be . an ( "array" ) . with . lengthOf ( 2 ) ;
218+ expect ( writtenContent [ 0 ] ) . to . deep . include ( {
219+ changes : "in api-v1" ,
220+ directory : "api-v1" ,
221+ } ) ;
222+ expect ( writtenContent [ 1 ] ) . to . deep . include ( {
223+ changes : "in api-v2" ,
224+ directory : "api-v2" ,
225+ } ) ;
226+ } ) ;
227+
228+ it ( "should skip non-directory entries" , async ( ) => {
229+ const execSyncStub = sinon . stub ( ) ;
230+ execSyncStub . onCall ( 0 ) . returns ( "version 1.0.0" ) ;
231+ execSyncStub . onCall ( 1 ) . returns ( "changes in api-v1" ) ;
232+
233+ const fsStub = {
234+ readdir : sinon . stub ( ) . returns ( [ "api-v1" , "not-a-dir.txt" ] ) ,
235+ stat : sinon . stub ( ) ,
236+ writeFile : sinon . stub ( ) ,
237+ } ;
238+ // First call for api-v1 returns isDirectory true
239+ fsStub . stat . onCall ( 0 ) . returns ( { isDirectory : ( ) => true } ) ;
240+ // Second call for not-a-dir.txt returns isDirectory false
241+ fsStub . stat . onCall ( 1 ) . returns ( { isDirectory : ( ) => false } ) ;
242+
243+ const oasDiff = pq ( "./oasDiff" , {
244+ child_process : {
245+ execSync : execSyncStub ,
246+ } ,
247+ "fs-extra" : fsStub ,
248+ } ) ;
249+
250+ const baseApi = "base" ;
251+ const newApi = "new" ;
252+ const flags = { } ;
253+
254+ await oasDiff . oasDiffChangelog ( baseApi , newApi , flags ) ;
255+
256+ // Should only call execSync twice (once for version check, once for api-v1)
257+ expect ( execSyncStub . callCount ) . to . equal ( 2 ) ;
152258 } ) ;
153259
154260 it ( "should throw an error if oasdiff is not installed" , ( ) => {
0 commit comments