-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from sliit-foss/feat/unit-tests
Added automatic versioning unit tests
- Loading branch information
Showing
9 changed files
with
256 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,8 @@ yarn-error.log* | |
|
||
.vscode | ||
|
||
packages/automatic-versioning/tmp | ||
|
||
p*/**/dist/ | ||
|
||
p*/**/.npmignore | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,152 @@ | ||
describe("automatic-versioning", () => { | ||
it("should work", () => { | ||
expect(true).toBe(true); | ||
import * as fs from "fs"; | ||
import run from "../src/utils/runner"; | ||
import { | ||
commit, | ||
getLastCommitMessage, | ||
getPackageVersion, | ||
executeVersionScript, | ||
setPackageVersion, | ||
getCurrentBranch | ||
} from "./util"; | ||
|
||
import "./setup"; | ||
|
||
describe("default", () => { | ||
it("bump patch version", async () => { | ||
await commit("Fix: test commit"); | ||
await executeVersionScript(); | ||
expect(getPackageVersion()).toBe("0.0.1"); | ||
expect(await getLastCommitMessage()).toBe("CI: @sliit-foss/automatic-versioning - patch release"); | ||
}); | ||
it("bump minor version", async () => { | ||
await commit("Feat: test commit"); | ||
await executeVersionScript(); | ||
expect(getPackageVersion()).toBe("0.1.0"); | ||
expect(await getLastCommitMessage()).toBe("CI: @sliit-foss/automatic-versioning - minor release"); | ||
}); | ||
it("bump major version", async () => { | ||
await commit("Feat!: test commit"); | ||
await executeVersionScript(); | ||
expect(getPackageVersion()).toBe("1.0.0"); | ||
expect(await getLastCommitMessage()).toBe("CI: @sliit-foss/automatic-versioning - major release"); | ||
}); | ||
it("bump patch version (with scope)", async () => { | ||
await commit("Fix(automatic-versioning): test commit"); | ||
await executeVersionScript(); | ||
expect(getPackageVersion()).toBe("0.0.1"); | ||
expect(await getLastCommitMessage()).toBe("CI: @sliit-foss/automatic-versioning - patch release"); | ||
}); | ||
it("should not bump any version", async () => { | ||
await commit("Chore: something which doesn't affect the version"); | ||
await executeVersionScript(); | ||
expect(getPackageVersion()).toBe("0.0.0"); | ||
}); | ||
it("no diff", async () => { | ||
await commit("Feat!: test commit"); | ||
await commit("Patch: empty commit", true); | ||
await executeVersionScript(); | ||
expect(getPackageVersion()).toBe("0.0.0"); | ||
}); | ||
it("recursive", async () => { | ||
await commit("Feat!: test commit"); | ||
fs.writeFileSync("./test.txt", "something which doesn't affect the version"); | ||
await commit("Merge branch 'main' of https://github.com/sliit-foss/npm-catalogue"); | ||
await executeVersionScript("--recursive"); | ||
expect(getPackageVersion()).toBe("1.0.0"); | ||
}); | ||
it("ignore prefixes", async () => { | ||
await commit("Feat: test commit"); | ||
fs.writeFileSync("./test.txt", "some automated commit"); | ||
await commit("CI: automated commit"); | ||
await executeVersionScript("--ignore-prefixes=ci --recursive"); | ||
expect(getPackageVersion()).toBe("0.1.0"); | ||
}); | ||
it("no bump in commit", async () => { | ||
await commit("Fix: test commit --no-bump"); | ||
await executeVersionScript(); | ||
expect(getPackageVersion()).toBe("0.0.0"); | ||
expect(await getLastCommitMessage()).toBe("Fix: test commit"); | ||
}); | ||
it("no commit", async () => { | ||
const commitMsg = "Fix: testing versioning without commit"; | ||
await commit(commitMsg); | ||
await executeVersionScript("--skip-commit"); | ||
expect(getPackageVersion()).toBe("0.0.1"); | ||
expect(await getLastCommitMessage()).toBe(commitMsg); | ||
}); | ||
describe("prerelease", () => { | ||
it("direct prefix", async () => { | ||
await commit("Prerelease: test commit"); | ||
await executeVersionScript(); | ||
expect(getPackageVersion()).toBe("0.0.1-0"); | ||
}); | ||
it("with custom tag", async () => { | ||
await commit("Prerelease: test commit"); | ||
await executeVersionScript("--prerelease-tag=blizzard"); | ||
expect(getPackageVersion()).toBe("0.0.1-blizzard.0"); | ||
}); | ||
describe("with prerelease branch", () => { | ||
const executePreleaseVersionScript = async () => | ||
executeVersionScript(`--prerelease-branch=${await getCurrentBranch()} --prerelease-tag=blizzard`); | ||
it("single prerelease", async () => { | ||
await commit("Feat: test commit"); | ||
await executePreleaseVersionScript(); | ||
expect(getPackageVersion()).toBe("0.1.0-blizzard.0"); | ||
}); | ||
it("multiple prereleases", async () => { | ||
await commit("Feat: test commit"); | ||
await executePreleaseVersionScript(); | ||
fs.writeFileSync("./test.txt", "some fix"); | ||
await commit("Feat: test commit"); | ||
await executePreleaseVersionScript(); | ||
expect(getPackageVersion()).toBe("0.2.0-blizzard.0"); | ||
}); | ||
it("multiple prereleases with patch", async () => { | ||
await commit("Feat: test commit"); | ||
await executePreleaseVersionScript(); | ||
fs.writeFileSync("./test.txt", "some fix"); | ||
await commit("Fix: test commit"); | ||
await executePreleaseVersionScript(); | ||
expect(getPackageVersion()).toBe("0.1.1-blizzard.0"); | ||
}); | ||
it("promote prerelease", async () => { | ||
await commit("Feat: test commit"); | ||
setPackageVersion("0.1.0-blizzard.5"); | ||
await executeVersionScript("--prerelease-tag=blizzard"); | ||
expect(getPackageVersion()).toBe("0.1.0"); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("tag-based", () => { | ||
it("default", async () => { | ||
await commit("Fix: test tag"); | ||
await run("git tag v0.0.2-alpha.0"); | ||
await executeVersionScript("--tag-based"); | ||
expect(getPackageVersion()).toBe("0.0.2-alpha.0"); | ||
expect(await getLastCommitMessage()).toBe( | ||
"CI: bumped version of @sliit-foss/automatic-versioning from 0.0.0 to 0.0.2-alpha.0" | ||
); | ||
}); | ||
it("no commit", async () => { | ||
const commitMsg = "Fix: test tag without commit"; | ||
await commit(commitMsg); | ||
await run("git tag v1.2.3-beta.1"); | ||
await executeVersionScript("--tag-based --skip-commit"); | ||
expect(getPackageVersion()).toBe("1.2.3-beta.1"); | ||
expect(await getLastCommitMessage()).toBe(commitMsg); | ||
}); | ||
it("rc tag", async () => { | ||
await commit("Fix: test tag rc"); | ||
await run("git tag v2022.07.18.rc6"); | ||
await executeVersionScript("--tag-based"); | ||
expect(getPackageVersion()).toBe("2022.7.18-rc6"); | ||
}); | ||
it("no tag diff", async () => { | ||
setPackageVersion("2023.7.18-rc6"); | ||
await run("git tag v2023.07.18.rc6"); | ||
await executeVersionScript("--tag-based"); | ||
expect(getPackageVersion()).toBe("2023.7.18-rc6"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as fs from "fs"; | ||
import * as crypto from "crypto"; | ||
import run from "../src/utils/runner"; | ||
import { resetPackageJson } from "./util"; | ||
|
||
jest.setTimeout(60000); | ||
|
||
const initialDir = process.cwd(); | ||
|
||
beforeAll(async () => { | ||
if (fs.existsSync("./tmp")) fs.rmdirSync("./tmp", { recursive: true }); | ||
fs.mkdirSync("./tmp"); | ||
process.chdir("./tmp"); | ||
await run("git init"); | ||
await run("git config user.email '[email protected]'"); | ||
await run("git config user.name 'SLIIT FOSS'"); | ||
resetPackageJson(); | ||
process.env.AUTOMATIC_VERSIONING_IS_TEST = "true"; | ||
}); | ||
|
||
afterAll(() => { | ||
process.chdir(initialDir); | ||
fs.rmdirSync("./tmp", { recursive: true }); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
fs.writeFileSync("./test.txt", crypto.randomBytes(16).toString("hex")); | ||
}); | ||
|
||
afterEach(async () => { | ||
await run("git reset --hard"); | ||
resetPackageJson(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as fs from "fs"; | ||
import run from "../src/utils/runner"; | ||
|
||
export const commit = async (message, empty) => { | ||
await run(`git add .`); | ||
await run(`git commit -m "${message}" ${empty ? "--allow-empty" : ""}`); | ||
}; | ||
|
||
export const getLastCommitMessage = async () => (await run("git log -1 --pretty=%B"))?.trim(); | ||
|
||
export const getCurrentBranch = async () => (await run("git rev-parse --abbrev-ref HEAD"))?.trim(); | ||
|
||
export const getPackageVersion = () => JSON.parse(fs.readFileSync("./package.json").toString()).version; | ||
|
||
export const setPackageVersion = (version) => { | ||
const packageJson = JSON.parse(fs.readFileSync("./package.json").toString()); | ||
packageJson.version = version; | ||
fs.writeFileSync("./package.json", JSON.stringify(packageJson, null, 2)); | ||
}; | ||
|
||
export const resetPackageJson = () => { | ||
fs.writeFileSync("./package.json", JSON.stringify({ name: "test", version: "0.0.0" }, null, 2)); | ||
}; | ||
|
||
export const executeVersionScript = async (args) => { | ||
process.argv = ["", "", ...(args?.split(" ") ?? [])]; | ||
await require("../src/index").default(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters