Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 146 additions & 36 deletions src/diff/oasDiff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ import sinon from "sinon";
const pq = proxyquire.noCallThru();

describe("oasDiffChangelog", () => {
it("should execute oasdiff command with correct parameters", async () => {
const stub = sinon.stub();
stub.onCall(0).returns("version 1.0.0");
stub.onCall(1).returns("");
it("should execute oasdiff command with correct parameters for single file mode", async () => {
const execSyncStub = sinon.stub();
execSyncStub.onCall(0).returns("version 1.0.0"); // version check
execSyncStub.onCall(1).returns(""); // diff result

const fsStub = {
readdir: sinon.stub().returns(["api-v1"]),
stat: sinon.stub().returns({ isDirectory: () => true }),
};

const oasDiff = pq("./oasDiff", {
child_process: {
execSync: stub,
execSync: execSyncStub,
},
"fs-extra": fsStub,
});

// Arrange
Expand All @@ -28,7 +34,38 @@ describe("oasDiffChangelog", () => {
const flags = {};
const result = await oasDiff.oasDiffChangelog(baseApi, newApi, flags);

expect(stub.called).to.be.true;
expect(execSyncStub.called).to.be.true;
expect(execSyncStub.args[1][0]).to.equal(
'oasdiff changelog "base.yaml" "new.yaml"'
);
expect(result).to.equal(0);
});

it("should execute oasdiff command with correct parameters for directory mode", async () => {
const execSyncStub = sinon.stub();
execSyncStub.onCall(0).returns("version 1.0.0"); // version check
execSyncStub.onCall(1).returns(""); // diff result

const fsStub = {
readdir: sinon.stub().returns(["api-v1"]),
stat: sinon.stub().returns({ isDirectory: () => true }),
};

const oasDiff = pq("./oasDiff", {
child_process: {
execSync: execSyncStub,
},
"fs-extra": fsStub,
});

// Arrange
const baseApi = "base";
const newApi = "new";
const flags = { dir: true };
const result = await oasDiff.oasDiffChangelog(baseApi, newApi, flags);

expect(execSyncStub.called).to.be.true;
expect(execSyncStub.args[1][0]).to.include('"base/api-v1/**/*.yaml"');
expect(result).to.equal(0);
});

Expand All @@ -37,15 +74,20 @@ describe("oasDiffChangelog", () => {
execSyncStub.onCall(0).returns("version 1.0.0");
execSyncStub.onCall(1).returns("mock oasdiff change");

const fsStub = {
readdir: sinon.stub().returns(["api-v1"]),
stat: sinon.stub().returns({ isDirectory: () => true }),
};

const oasDiff = pq("./oasDiff", {
child_process: {
execSync: execSyncStub,
},
"fs-extra": fsStub,
});

// Arrange
const baseApi = "base.yaml";
const newApi = "new.yaml";
const baseApi = "base";
const newApi = "new";
const flags = {};
const result = await oasDiff.oasDiffChangelog(baseApi, newApi, flags);

Expand All @@ -58,15 +100,20 @@ describe("oasDiffChangelog", () => {
execSyncStub.onCall(0).returns("version 1.0.0");
execSyncStub.onCall(1).throws(new Error("mock oasdiff error"));

const fsStub = {
readdir: sinon.stub().returns(["api-v1"]),
stat: sinon.stub().returns({ isDirectory: () => true }),
};

const oasDiff = pq("./oasDiff", {
child_process: {
execSync: execSyncStub,
},
"fs-extra": fsStub,
});

// Arrange
const baseApi = "base.yaml";
const newApi = "new.yaml";
const baseApi = "base";
const newApi = "new";
const flags = {};
const result = await oasDiff.oasDiffChangelog(baseApi, newApi, flags);

Expand All @@ -77,78 +124,141 @@ describe("oasDiffChangelog", () => {
it("should run oasdiff in directory mode when the --dir flag is provided", async () => {
const execSyncStub = sinon.stub();
execSyncStub.onCall(0).returns("version 1.0.0");
execSyncStub.onCall(1).returns("a change");
execSyncStub.onCall(1).returns("a minor change");

const fsStub = {
readdir: sinon.stub().returns(["api-v1"]),
stat: sinon.stub().returns({ isDirectory: () => true }),
};

const oasDiff = pq("./oasDiff", {
child_process: {
execSync: execSyncStub,
},
"fs-extra": fsStub,
});

const baseApi = "base.yaml";
const newApi = "new.yaml";
const baseApi = "base";
const newApi = "new";
const flags = {
dir: true,
};
await oasDiff.oasDiffChangelog(baseApi, newApi, flags);

expect(execSyncStub.called).to.be.true;
expect(execSyncStub.args[1][0]).to.equal(
'oasdiff changelog --composed "base.yaml/**/*.yaml" "new.yaml/**/*.yaml"'
'oasdiff changelog --composed "base/api-v1/**/*.yaml" "new/api-v1/**/*.yaml"'
);
});

it("should save the changes to a file when the --out-file flag is provided", async () => {
it("should concatenate results from multiple directories in text format", async () => {
const execSyncStub = sinon.stub();
execSyncStub.onCall(0).returns("version 1.0.0");
execSyncStub.onCall(1).returns("a change");
const fsStub = sinon.stub();
execSyncStub.onCall(1).returns("changes in api-v1");
execSyncStub.onCall(2).returns("changes in api-v2");

const fsStub = {
readdir: sinon.stub().returns(["api-v1", "api-v2"]),
stat: sinon.stub().returns({ isDirectory: () => true }),
writeFile: sinon.stub(),
};

const oasDiff = pq("./oasDiff", {
child_process: {
execSync: execSyncStub,
},
"fs-extra": {
writeFile: fsStub,
},
"fs-extra": fsStub,
});

// Arrange
const baseApi = "base.yaml";
const newApi = "new.yaml";
const baseApi = "base";
const newApi = "new";
const flags = {
"out-file": "output.txt",
dir: true,
};

await oasDiff.oasDiffChangelog(baseApi, newApi, flags);
expect(fsStub.called).to.be.true;

expect(fsStub.writeFile.called).to.be.true;
const writtenContent = fsStub.writeFile.args[0][1];
expect(writtenContent).to.include("=== Changes in api-v1 ===");
expect(writtenContent).to.include("changes in api-v1");
expect(writtenContent).to.include("=== Changes in api-v2 ===");
expect(writtenContent).to.include("changes in api-v2");
});

it("should save the changes to a jsonfile when the --out-file flag is provided and format is json", async () => {
it("should concatenate results from multiple directories in JSON format", async () => {
const execSyncStub = sinon.stub();
execSyncStub.onCall(0).returns("version 1.0.0");
execSyncStub.onCall(1).returns('{"change": "a change"}');
const fsStub = sinon.stub();
execSyncStub.onCall(1).returns('{"changes": "in api-v1"}');
execSyncStub.onCall(2).returns('{"changes": "in api-v2"}');

const fsStub = {
readdir: sinon.stub().returns(["api-v1", "api-v2"]),
stat: sinon.stub().returns({ isDirectory: () => true }),
writeJson: sinon.stub(),
};

const oasDiff = pq("./oasDiff", {
child_process: {
execSync: execSyncStub,
},
"fs-extra": {
writeJson: fsStub,
},
"fs-extra": fsStub,
});

// Arrange
const baseApi = "base.yaml";
const newApi = "new.yaml";
const baseApi = "base";
const newApi = "new";
const flags = {
"out-file": "output.json",
format: "json",
dir: true,
};

await oasDiff.oasDiffChangelog(baseApi, newApi, flags);

expect(fsStub.writeJson.called).to.be.true;
const writtenContent = fsStub.writeJson.args[0][1];
expect(writtenContent).to.be.an("array").with.lengthOf(2);
expect(writtenContent[0]).to.deep.include({
changes: "in api-v1",
directory: "api-v1",
});
expect(writtenContent[1]).to.deep.include({
changes: "in api-v2",
directory: "api-v2",
});
});

it("should skip non-directory entries", async () => {
const execSyncStub = sinon.stub();
execSyncStub.onCall(0).returns("version 1.0.0");
execSyncStub.onCall(1).returns("changes in api-v1");

const fsStub = {
readdir: sinon.stub().returns(["api-v1", "not-a-dir.txt"]),
stat: sinon.stub(),
writeFile: sinon.stub(),
};
// First call for api-v1 returns isDirectory true
fsStub.stat.onCall(0).returns({ isDirectory: () => true });
// Second call for not-a-dir.txt returns isDirectory false
fsStub.stat.onCall(1).returns({ isDirectory: () => false });

const oasDiff = pq("./oasDiff", {
child_process: {
execSync: execSyncStub,
},
"fs-extra": fsStub,
});

const baseApi = "base";
const newApi = "new";
const flags = {};

await oasDiff.oasDiffChangelog(baseApi, newApi, flags);
expect(fsStub.called).to.be.true;

// Should only call execSync twice (once for version check, once for api-v1)
expect(execSyncStub.callCount).to.equal(2);
});

it("should throw an error if oasdiff is not installed", () => {
Expand Down
Loading