Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KetanReddy committed Aug 20, 2024
1 parent 1b9d551 commit 8e8e7c7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
67 changes: 54 additions & 13 deletions plugins/version-file/__tests__/version-file.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Auto, { SEMVER } from '@auto-it/core';
import mockFs from "mock-fs";
import fs from "fs";
import BazelPlugin from '../src';
import VersionFilePlugin from '../src';
import { makeHooks } from '@auto-it/core/dist/utils/make-hooks';
import { dummyLog } from "@auto-it/core/dist/utils/logger";

Expand All @@ -27,7 +27,7 @@ describe('Version File Read Operations', () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new BazelPlugin({});
const plugin = new VersionFilePlugin({});
const hooks = makeHooks();

plugin.apply({
Expand All @@ -44,7 +44,7 @@ describe('Version File Read Operations', () => {
mockFs({
"VERSIONFILE": `1.0.0`,
});
const plugin = new BazelPlugin({versionFile: "VERSIONFILE"});
const plugin = new VersionFilePlugin({versionFile: "VERSIONFILE"});
const hooks = makeHooks();

plugin.apply({
Expand All @@ -63,14 +63,19 @@ describe("Version File Write Operations", () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new BazelPlugin({});
const plugin = new VersionFilePlugin({});
const hooks = makeHooks();

const prefixRelease: (a: string) => string = (version: string) => {
return `v${version}`;
};

plugin.apply({
hooks,
remote: "origin",
baseBranch: "main",
logger: dummyLog(),
prefixRelease
} as Auto);

await hooks.version.promise({bump: SEMVER.major})
Expand All @@ -84,14 +89,19 @@ describe("Version File Write Operations", () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new BazelPlugin({});
const plugin = new VersionFilePlugin({});
const hooks = makeHooks();

const prefixRelease: (a: string) => string = (version: string) => {
return `v${version}`;
};

plugin.apply({
hooks,
remote: "origin",
baseBranch: "main",
logger: dummyLog(),
prefixRelease
} as Auto);

await hooks.version.promise({bump: SEMVER.minor})
Expand All @@ -105,14 +115,19 @@ describe("Version File Write Operations", () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new BazelPlugin({});
const plugin = new VersionFilePlugin({});
const hooks = makeHooks();

const prefixRelease: (a: string) => string = (version: string) => {
return `v${version}`;
};

plugin.apply({
hooks,
remote: "origin",
baseBranch: "main",
logger: dummyLog(),
prefixRelease
} as Auto);

await hooks.version.promise({bump: SEMVER.patch})
Expand All @@ -121,14 +136,40 @@ describe("Version File Write Operations", () => {
expect(execPromise).toHaveBeenNthCalledWith(1, "git", ["commit", "-am", "\"Bump version to: v1.0.1 [skip ci]\""]);
expect(execPromise).toHaveBeenNthCalledWith(2, "git", ["tag", "v1.0.1"]);
});

test("It should version the file properly for without a prefix", async () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new VersionFilePlugin({});
const hooks = makeHooks();

const prefixRelease: (a: string) => string = (version: string) => {
return `${version}`;
};

plugin.apply({
hooks,
remote: "origin",
baseBranch: "main",
logger: dummyLog(),
prefixRelease
} as Auto);

await hooks.version.promise({bump: SEMVER.patch})
expect(fs.readFileSync("VERSION", "utf-8")).toStrictEqual("1.0.1");
// check that the proper git operations were performed
expect(execPromise).toHaveBeenNthCalledWith(1, "git", ["commit", "-am", "\"Bump version to: 1.0.1 [skip ci]\""]);
expect(execPromise).toHaveBeenNthCalledWith(2, "git", ["tag", "1.0.1"]);
});
})

describe("Test Release Types", () => {
test("Full release with no release script", async () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new BazelPlugin({});
const plugin = new VersionFilePlugin({});
const hooks = makeHooks();

plugin.apply({
Expand All @@ -148,7 +189,7 @@ describe("Test Release Types", () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new BazelPlugin({publishScript:"./tools/release.sh"});
const plugin = new VersionFilePlugin({publishScript:"./tools/release.sh"});
const hooks = makeHooks();

plugin.apply({
Expand All @@ -171,7 +212,7 @@ describe("Test Release Types", () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new BazelPlugin({});
const plugin = new VersionFilePlugin({});
const hooks = makeHooks();

plugin.apply(({
Expand Down Expand Up @@ -199,7 +240,7 @@ describe("Test Release Types", () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new BazelPlugin({publishScript:"./tools/release.sh"});
const plugin = new VersionFilePlugin({publishScript:"./tools/release.sh"});
const hooks = makeHooks();

plugin.apply(({
Expand Down Expand Up @@ -235,7 +276,7 @@ describe("Test Release Types", () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new BazelPlugin({});
const plugin = new VersionFilePlugin({});
const hooks = makeHooks();

plugin.apply(({
Expand Down Expand Up @@ -272,7 +313,7 @@ describe("Test Release Types", () => {
mockFs({
"VERSION": `1.0.0`,
});
const plugin = new BazelPlugin({publishScript:"./tools/release.sh"});
const plugin = new VersionFilePlugin({publishScript:"./tools/release.sh"});
const hooks = makeHooks();

plugin.apply(({
Expand Down Expand Up @@ -311,7 +352,7 @@ describe("Test Release Types", () => {
mockFs({
VERSION: `1.0.0`,
});
const plugin = new BazelPlugin({
const plugin = new VersionFilePlugin({
publishScript: "./tools/release.sh",
publishScriptReleaseTypeArgs: {
publish: ["args", "for", "publish"],
Expand Down
5 changes: 3 additions & 2 deletions plugins/version-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ export default class VersionFilePlugin implements IPlugin {
if (newVersion){
// Seal versions via commit and tag
await writeNewVersion(auto, newVersion, this.versionFile)
await execPromise("git", ["commit", "-am", `"Bump version to: v${newVersion} [skip ci]"`]);
const prefixedVersion = auto.prefixRelease(newVersion)
await execPromise("git", ["commit", "-am", `"Bump version to: ${prefixedVersion} [skip ci]"`]);
await execPromise("git", [
"tag",
auto.prefixRelease(newVersion)
prefixedVersion
]);
auto.logger.verbose.info("Successfully versioned repo");
} else {
Expand Down

0 comments on commit 8e8e7c7

Please sign in to comment.