Skip to content

Commit fe91250

Browse files
committed
add unit tests
1 parent 637eb11 commit fe91250

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/diff/oasDiff.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
});

src/diff/oasDiff.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,17 @@ async function executeOasDiff(
251251
* @param dirs - Array of directory objects with name and path properties
252252
* @returns Copy of passed array with updated name property
253253
*/
254-
function normalizeDirectoryNames(dirs: { name: string; path: string }[]): { name: string; path: string }[] {
254+
function normalizeDirectoryNames(
255+
dirs: Array<{ name: string; path: string }>
256+
): Array<{ name: string; path: string }> {
255257
return dirs.map((dir) => ({
256258
...dir,
257259
// matches the pattern of the version number in the directory name, ie: -1.0.16
258260
// extracts the major version number if available, otherwise the original match if no major version is found
259261
name: dir.name.replace(/-\d+\.\d+\.\d+$/, (match) => {
260262
const majorVersion = match.match(/^-\d+/)?.[0];
261263
return majorVersion || match;
262-
})
264+
}),
263265
}));
264266
}
265267

0 commit comments

Comments
 (0)