|
9 | 9 | import { expect } from "chai"; |
10 | 10 | import proxyquire from "proxyquire"; |
11 | 11 | import sinon from "sinon"; |
| 12 | +import { consoleStub } from "../../testResources/setup"; |
12 | 13 |
|
13 | 14 | const pq = proxyquire.noCallThru(); |
14 | 15 |
|
@@ -678,4 +679,84 @@ describe("oasDiffChangelog", () => { |
678 | 679 | expect(execStub.called).to.be.true; |
679 | 680 | expect(result).to.equal(1); // Changes should be reported |
680 | 681 | }); |
| 682 | + |
| 683 | + it("should normalize directory names when disable-normalize-directory-names flag is false", async () => { |
| 684 | + const execStub = createMockExec(); |
| 685 | + execStub.onSecondCall().callsArgWith(1, null, "changes in api-1", ""); |
| 686 | + |
| 687 | + const fsStub = createMockFs(); |
| 688 | + // Setup directory structure with versioned directory names |
| 689 | + setupDirectoryStructure(fsStub, [ |
| 690 | + { path: "base", contents: ["api-1.0.16", "api-2.1.5"] }, |
| 691 | + { path: "base/api-1.0.16", contents: ["exchange.json", "spec.yaml"] }, |
| 692 | + { path: "base/api-2.1.5", contents: ["exchange.json", "spec.yaml"] }, |
| 693 | + { path: "new", contents: ["api-1.0.17", "api-2.1.6"] }, |
| 694 | + { path: "new/api-1.0.17", contents: ["exchange.json", "spec.yaml"] }, |
| 695 | + { path: "new/api-2.1.6", contents: ["exchange.json", "spec.yaml"] }, |
| 696 | + ]); |
| 697 | + |
| 698 | + const oasDiff = createOasDiffProxy(execStub, fsStub); |
| 699 | + |
| 700 | + const baseApi = "base"; |
| 701 | + const newApi = "new"; |
| 702 | + const flags = { |
| 703 | + "out-file": "output.txt", |
| 704 | + dir: true, |
| 705 | + "disable-normalize-directory-names": false, |
| 706 | + }; |
| 707 | + |
| 708 | + await oasDiff.oasDiffChangelog(baseApi, newApi, flags); |
| 709 | + |
| 710 | + expect(fsStub.writeFile.called).to.be.true; |
| 711 | + const writtenContent = fsStub.writeFile.args[0][1]; |
| 712 | + |
| 713 | + // Verify that the function completes successfully with the normalize-directory-names flag enabled |
| 714 | + expect(writtenContent).to.be.a("string"); |
| 715 | + expect(writtenContent.length).to.be.greaterThan(0); |
| 716 | + |
| 717 | + // Verify that the console.log stub captured the "Processing directory pair" messages |
| 718 | + expect(consoleStub.called).to.be.true; |
| 719 | + // flattens the array of arrays into a single array of strings |
| 720 | + const allMessages = consoleStub.args.map((args) => args[0]); |
| 721 | + expect(allMessages).to.include("Processing directory pair: api-1"); |
| 722 | + expect(allMessages).to.include("Processing directory pair: api-2"); |
| 723 | + consoleStub.restore(); |
| 724 | + }); |
| 725 | + |
| 726 | + it("should handle fs.readdir error in findYamlFiles function", async () => { |
| 727 | + const consoleWarnSpy = sinon.spy(console, "warn"); |
| 728 | + const execStub = createMockExec(); |
| 729 | + execStub.onSecondCall().callsArgWith(1, null, "changes in api-v1", ""); |
| 730 | + |
| 731 | + const fsStub = createMockFs(); |
| 732 | + setupDirectoryStructure(fsStub, [ |
| 733 | + { path: "base", contents: ["api-v1"] }, |
| 734 | + { path: "base/api-v1", contents: ["exchange.json", "spec.yaml"] }, |
| 735 | + { path: "new", contents: ["api-v1"] }, |
| 736 | + { path: "new/api-v1", contents: ["exchange.json", "spec.yaml"] }, |
| 737 | + ]); |
| 738 | + |
| 739 | + // Make readdir fail for the new/api-v1 directory to trigger the error handling branch |
| 740 | + fsStub.readdir |
| 741 | + .withArgs("new/api-v1") |
| 742 | + .rejects(new Error("Permission denied")); |
| 743 | + |
| 744 | + const oasDiff = createOasDiffProxy(execStub, fsStub); |
| 745 | + |
| 746 | + const baseApi = "base"; |
| 747 | + const newApi = "new"; |
| 748 | + const flags = { |
| 749 | + dir: true, |
| 750 | + }; |
| 751 | + |
| 752 | + await oasDiff.oasDiffChangelog(baseApi, newApi, flags); |
| 753 | + |
| 754 | + // Verify that the function handles the error gracefully |
| 755 | + expect(consoleWarnSpy.called).to.be.true; |
| 756 | + expect(consoleWarnSpy.args[0][0]).to.include( |
| 757 | + "Warning: Could not read directory new/api-v1:" |
| 758 | + ); |
| 759 | + expect(consoleWarnSpy.args[0][1]).to.include("Permission denied"); |
| 760 | + consoleWarnSpy.restore(); |
| 761 | + }); |
681 | 762 | }); |
0 commit comments