Skip to content

Commit f25db22

Browse files
committed
clean up
1 parent e0da462 commit f25db22

File tree

6 files changed

+110
-16
lines changed

6 files changed

+110
-16
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": 600
22+
"max": 700
2323
}
2424
],
2525
"@typescript-eslint/naming-convention": [

src/diff/diffCommand.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Rule } from "json-rules-engine";
1313
import chai from "chai";
1414
import chaiFs from "chai-fs";
1515

16-
import { DiffCommand as cmd, DiffCommand } from "./diffCommand";
16+
import { DiffCommand as cmd } from "./diffCommand";
1717
import * as diffDirectories from "./diffDirectories";
1818
import { NodeChanges } from "./changes/nodeChanges";
1919
import { ApiChanges } from "./changes/apiChanges";
@@ -24,10 +24,8 @@ import { RuleCategory } from "./ruleCategory";
2424
import * as oasDiff from "./oasDiff";
2525

2626
import proxyquire from "proxyquire";
27-
import sinon from "sinon";
2827

2928
chai.use(chaiFs);
30-
const pq = proxyquire.noCallThru();
3129

3230
const nodeChanges = new NodeChanges("test-id", ["test:type"]);
3331
nodeChanges.added = { "core:name": "oldName" };

src/diff/oasDiff.test.ts

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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"));

src/diff/oasDiff.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,19 @@ export async function oasDiffChangelog(baseApi: string, newApi: string, flags) {
133133
console.log(`Changes found in ${baseDir}`);
134134
if (flags.format === "json") {
135135
const outputJson = JSON.parse(oasdiffOutput);
136-
if (outputJson.length) {
136+
if (outputJson?.length > 0) {
137137
allResults.push({
138138
directory: baseDir,
139139
changes: outputJson,
140140
});
141+
hasChanges = true;
141142
}
142143
} else {
143144
// For text format, add section headers
144145
const formattedOutput = `=== Changes in ${baseDir} ===\n${oasdiffOutput}`;
145146
allResults.push(formattedOutput);
147+
hasChanges = true;
146148
}
147-
hasChanges = true;
148149
} else {
149150
console.log(`No changes found in ${baseDir}`);
150151
}
@@ -187,11 +188,14 @@ export async function oasDiffChangelog(baseApi: string, newApi: string, flags) {
187188
if (flags.format === "json") {
188189
// For JSON format, parse the output
189190
const outputJson = JSON.parse(oasdiffOutput);
190-
allResults.push(outputJson);
191+
if (outputJson?.length > 0) {
192+
allResults.push(outputJson);
193+
hasChanges = true;
194+
}
191195
} else {
192196
allResults.push(oasdiffOutput);
197+
hasChanges = true;
193198
}
194-
hasChanges = true;
195199
} else {
196200
console.log("No changes found");
197201
}

src/generate-from-oas/generateCommand.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { allCommonFlags } from "../common/flags";
1010
import {
1111
generateFromOas,
1212
DEFAULT_CONFIG_PACKAGE_PATH,
13-
DEFAULT_CONFIG_PATH,
1413
} from "./generateFromOas";
1514

1615
export class GenerateCommand extends Command {

src/generate-from-oas/generateFromOas.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import path from "path";
99
import { execSync } from "child_process";
10-
import { string, boolean } from "@oclif/parser/lib/flags";
1110

1211
// Path relative to project root
1312
export const DEFAULT_CONFIG_BASE_PATH =

0 commit comments

Comments
 (0)