Skip to content

Commit e0da462

Browse files
committed
clean up
1 parent 1d249cf commit e0da462

File tree

3 files changed

+121
-11
lines changed

3 files changed

+121
-11
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"max-lines": [
2020
"error",
2121
{
22-
"max": 500
22+
"max": 600
2323
}
2424
],
2525
"@typescript-eslint/naming-convention": [

src/diff/oasDiff.test.ts

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, salesforce.com, inc.
2+
* Copyright (c) 2025, salesforce.com, inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: BSD-3-Clause
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
@@ -224,13 +224,15 @@ describe("oasDiffChangelog", () => {
224224
expect(fsStub.writeJson.called).to.be.true;
225225
const writtenContent = fsStub.writeJson.args[0][1];
226226
expect(writtenContent).to.be.an("array").with.lengthOf(2);
227-
expect(writtenContent[0]).to.deep.include({
228-
changes: "in api-v1",
227+
expect(writtenContent[0]).to.deep.equal({
229228
directory: "api-v1",
229+
status: "modified",
230+
changes: { changes: "in api-v1" },
230231
});
231-
expect(writtenContent[1]).to.deep.include({
232-
changes: "in api-v2",
232+
expect(writtenContent[1]).to.deep.equal({
233233
directory: "api-v2",
234+
status: "modified",
235+
changes: { changes: "in api-v2" },
234236
});
235237
});
236238

@@ -436,6 +438,94 @@ describe("oasDiffChangelog", () => {
436438
expect(writtenContent).to.not.include("=== Changes in stable-api ===");
437439
});
438440

441+
it("should report deleted APIs in JSON format", async () => {
442+
const execStub = sinon.stub();
443+
execStub.callsArgWith(1, null, "version 1.0.0", "");
444+
445+
const fsStub = {
446+
readdir: sinon.stub(),
447+
stat: sinon.stub(),
448+
writeJson: sinon.stub(),
449+
};
450+
451+
// Base has api-v1 and api-v2, new only has api-v2
452+
fsStub.readdir.onCall(0).returns(["api-v1", "api-v2"]); // base directories
453+
fsStub.readdir.onCall(1).returns(["api-v2"]); // new directories
454+
455+
// All stat calls return isDirectory true
456+
fsStub.stat.returns({ isDirectory: () => true });
457+
458+
const oasDiff = pq("./oasDiff", {
459+
child_process: {
460+
exec: execStub,
461+
},
462+
"fs-extra": fsStub,
463+
});
464+
465+
const baseApi = "base";
466+
const newApi = "new";
467+
const flags = {
468+
"out-file": "output.json",
469+
format: "json",
470+
dir: true,
471+
};
472+
473+
await oasDiff.oasDiffChangelog(baseApi, newApi, flags);
474+
475+
expect(fsStub.writeJson.called).to.be.true;
476+
const writtenContent = fsStub.writeJson.args[0][1];
477+
expect(writtenContent).to.be.an("array").with.lengthOf(1);
478+
expect(writtenContent[0]).to.deep.equal({
479+
directory: "api-v1",
480+
status: "deleted",
481+
message: "api-v1 API is deleted",
482+
});
483+
});
484+
485+
it("should report added APIs in JSON format", async () => {
486+
const execStub = sinon.stub();
487+
execStub.callsArgWith(1, null, "version 1.0.0", "");
488+
489+
const fsStub = {
490+
readdir: sinon.stub(),
491+
stat: sinon.stub(),
492+
writeJson: sinon.stub(),
493+
};
494+
495+
// Base has only api-v1, new has api-v1 and api-v2
496+
fsStub.readdir.onCall(0).returns(["api-v1"]); // base directories
497+
fsStub.readdir.onCall(1).returns(["api-v1", "api-v2"]); // new directories
498+
499+
// All stat calls return isDirectory true
500+
fsStub.stat.returns({ isDirectory: () => true });
501+
502+
const oasDiff = pq("./oasDiff", {
503+
child_process: {
504+
exec: execStub,
505+
},
506+
"fs-extra": fsStub,
507+
});
508+
509+
const baseApi = "base";
510+
const newApi = "new";
511+
const flags = {
512+
"out-file": "output.json",
513+
format: "json",
514+
dir: true,
515+
};
516+
517+
await oasDiff.oasDiffChangelog(baseApi, newApi, flags);
518+
519+
expect(fsStub.writeJson.called).to.be.true;
520+
const writtenContent = fsStub.writeJson.args[0][1];
521+
expect(writtenContent).to.be.an("array").with.lengthOf(1);
522+
expect(writtenContent[0]).to.deep.equal({
523+
directory: "api-v2",
524+
status: "added",
525+
message: "api-v2 API is added",
526+
});
527+
});
528+
439529
it("should throw an error if oasdiff is not installed", async () => {
440530
const execStub = sinon.stub();
441531
execStub.callsArgWith(1, new Error("oasdiff not installed"));

src/diff/oasDiff.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,15 @@ export async function oasDiffChangelog(baseApi: string, newApi: string, flags) {
103103
// Check if matching directory doesn't exist in new
104104
if (!newDirectories.includes(baseDir)) {
105105
console.log(`${baseDir} API is deleted`);
106-
allResults.push(`======${baseDir} API is deleted======`);
106+
if (flags.format === "json") {
107+
allResults.push({
108+
directory: baseDir,
109+
status: "deleted",
110+
message: `${baseDir} API is deleted`,
111+
});
112+
} else {
113+
allResults.push(`======${baseDir} API is deleted======`);
114+
}
107115
hasChanges = true;
108116
continue;
109117
}
@@ -125,8 +133,12 @@ export async function oasDiffChangelog(baseApi: string, newApi: string, flags) {
125133
console.log(`Changes found in ${baseDir}`);
126134
if (flags.format === "json") {
127135
const outputJson = JSON.parse(oasdiffOutput);
128-
outputJson.directory = baseDir;
129-
allResults.push(outputJson);
136+
if (outputJson.length) {
137+
allResults.push({
138+
directory: baseDir,
139+
changes: outputJson,
140+
});
141+
}
130142
} else {
131143
// For text format, add section headers
132144
const formattedOutput = `=== Changes in ${baseDir} ===\n${oasdiffOutput}`;
@@ -154,7 +166,15 @@ export async function oasDiffChangelog(baseApi: string, newApi: string, flags) {
154166
// Check if this directory doesn't exist in base (added API)
155167
if (!baseDirectories.includes(newDir)) {
156168
console.log(`${newDir} API is added`);
157-
allResults.push(`======${newDir} API is added======`);
169+
if (flags.format === "json") {
170+
allResults.push({
171+
directory: newDir,
172+
status: "added",
173+
message: `${newDir} API is added`,
174+
});
175+
} else {
176+
allResults.push(`======${newDir} API is added======`);
177+
}
158178
hasChanges = true;
159179
}
160180
}
@@ -202,7 +222,7 @@ export async function oasDiffChangelog(baseApi: string, newApi: string, flags) {
202222
export async function checkOasDiffIsInstalled() {
203223
try {
204224
await new Promise<void>((resolve, reject) => {
205-
exec(`oasdiff --version`, (error, stdout, stderr) => {
225+
exec(`oasdiff --version`, (error) => {
206226
if (error) {
207227
reject(error);
208228
} else {

0 commit comments

Comments
 (0)