|
const child_process = require("child_process"); |
|
const { bumpVersion } = require("./bumpVersion"); |
|
|
|
jest.mock("child_process", () => { |
|
let execMockResults = {}; |
|
return { |
|
exec: (arg, cb) => { |
|
for (let [prefix, result] of Object.entries(execMockResults)) { |
|
if (arg.startsWith(prefix)) { |
|
if (result instanceof Error) return cb(result, null, null); |
|
return cb(null, result, null); |
|
} |
|
} |
|
return "mock"; |
|
}, |
|
__setExecResults(map) { |
|
execMockResults = map; |
|
}, |
|
}; |
|
}); |
|
|
|
describe("bumpVersion", () => { |
|
beforeEach(() => { |
|
child_process.__setExecResults({ |
|
"git describe --tags --abbrev=0": "v2.0", |
|
"git rev-parse HEAD": "sha1", |
|
"git rev-list -1": "sha2", |
|
}); |
|
}); |
|
|
|
it("should increase major version", async () => { |
|
const nextVersion = await bumpVersion("major", "v"); |
|
expect(nextVersion).toBe("v3.0"); |
|
}); |
|
|
|
it("should increase minor version", async () => { |
|
const nextVersion = await bumpVersion("minor", "v"); |
|
expect(nextVersion).toBe("v2.1"); |
|
}); |
|
|
|
it("should accept custom prefix", async () => { |
|
child_process.__setExecResults({ |
|
"git describe --tags --abbrev=0": "test2.0", |
|
"git rev-parse HEAD": "sha1", |
|
"git rev-list -1": "sha2", |
|
}); |
|
let nextVersion = await bumpVersion("major", "test"); |
|
expect(nextVersion).toBe("test3.0"); |
|
nextVersion = await bumpVersion("minor", "test"); |
|
expect(nextVersion).toBe("test2.1"); |
|
}); |
|
|
|
it("should default to v1.0 if no tag present", async () => { |
|
child_process.__setExecResults({ |
|
"git describe --tags --abbrev=0": new Error("no tag"), |
|
"git rev-parse HEAD": "sha1", |
|
"git rev-list -1": "sha2", |
|
}); |
|
expect(await bumpVersion("major", "v")).toBe("v1.0"); |
|
}); |
|
|
|
it("should default to $prefix1.0 if no tag present and prefix passed", async () => { |
|
child_process.__setExecResults({ |
|
"git describe --tags --abbrev=0": new Error("no tag"), |
|
"git rev-parse HEAD": "sha1", |
|
"git rev-list -1": "sha2", |
|
}); |
|
expect(await bumpVersion("major", "test")).toBe("test1.0"); |
|
expect(await bumpVersion("minor", "test")).toBe("test0.1"); |
|
}); |
|
|
|
it("should throw if HEAD already has a version tag", () => { |
|
child_process.__setExecResults({ |
|
"git describe --tags --abbrev=0": "v2.0", |
|
"git rev-parse HEAD": "sha1", |
|
"git rev-list -1": "sha1", |
|
}); |
|
expect(bumpVersion("major", "v")).rejects.toThrow( |
|
"ExistingTagPointsToHEAD" |
|
); |
|
}); |
|
}); |