Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tag canaries for cocoapods plugin #1702

Merged
merged 10 commits into from
Jan 11, 2021
2 changes: 1 addition & 1 deletion packages/core/src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ const loadEnv = () => {
};

/** Get the pr number from user input or the CI env. */
function getPrNumberFromEnv(pr?: number) {
export function getPrNumberFromEnv(pr?: number) {
const envPr = "pr" in env && Number(env.pr);
const prNumber = pr || envPr;

Expand Down
225 changes: 214 additions & 11 deletions plugins/cocoapods/__tests__/cocoapods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import CocoapodsPlugin, {
getParsedPodspecContents,
getVersion,
updatePodspecVersion,
updateSourceLocation,
getSourceInfo,
} from "../src";

const specWithVersion = (version: string) => `
const specWithVersion = (
version: string,
source = "{ :git => 'https://github.com/intuit/auto.git', :tag => s.version.to_s }"
) => `
Pod:: Spec.new do | s |
s.name = 'Test'
s.version = '${version}'
Expand All @@ -29,7 +34,7 @@ const specWithVersion = (version: string) => `
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { : type => 'MIT', : file => 'LICENSE' }
s.author = { 'hborawski' => 'harris_borawski@intuit.com' }
s.source = { : git => 'https://github.com/intuit/auto.git', : tag => s.version.to_s }
s.source = ${source}

s.ios.deployment_target = '11.0'

Expand All @@ -46,11 +51,12 @@ const mockPodspec = (contents: string) => {
return jest.spyOn(utilities, "getPodspecContents").mockReturnValue(contents);
};

let exec = jest.fn().mockResolvedValueOnce("");
let exec = jest.fn();
// @ts-ignore
jest.mock("../../../packages/core/dist/utils/exec-promise", () => (...args) =>
exec(...args)
);
const logger = dummyLog();

describe("Cocoapods Plugin", () => {
let hooks: Auto.IAutoHooks;
Expand All @@ -67,7 +73,25 @@ describe("Cocoapods Plugin", () => {
exec.mockClear();
const plugin = new CocoapodsPlugin(options);
hooks = makeHooks();
plugin.apply({ hooks, logger: dummyLog(), prefixRelease } as Auto.Auto);
plugin.apply(({
hooks,
logger: logger,
prefixRelease,
git: {
getLatestRelease: async () => "0.0.1",
getPullRequest: async () => ({
data: {
head: {
repo: {
clone_url: "https://github.com/intuit-fork/auto.git",
},
},
},
}),
},
remote: "https://github.com/intuit/auto.git",
getCurrentVersion: async () => "0.0.1",
} as unknown) as Auto.Auto);
});

describe("getParsedPodspecContents", () => {
Expand All @@ -93,6 +117,25 @@ describe("Cocoapods Plugin", () => {

expect(getVersion("./Test.podspec")).toBe("0.0.1");
});
test("should return canary version", () => {
mockPodspec(specWithVersion("0.0.1-canary.1.0.0"));

expect(getVersion("./Test.podspec")).toBe("0.0.1-canary.1.0.0");
});
});
describe("getSourceInfo", () => {
test("should throw error if source line cant be found", () => {
mockPodspec(specWithVersion("0.0.1", "no source"));

expect(() => getSourceInfo("./Test.podspec")).toThrow();
});
test("should retrieve source info", () => {
mockPodspec(specWithVersion("0.0.1"));

expect(getSourceInfo("./Test.podspec")).toBe(
"{ :git => 'https://github.com/intuit/auto.git', :tag => s.version.to_s }"
);
});
});
describe("updatePodspecVersion", () => {
test("should throw error if there is an error writing file", async () => {
Expand All @@ -104,11 +147,9 @@ describe("Cocoapods Plugin", () => {
throw new Error("Filesystem Error");
});

await expect(
updatePodspecVersion("./Test.podspec", "0.0.2")
).rejects.toThrowError(
"Error updating version in podspec: ./Test.podspec"
);
expect(
updatePodspecVersion.bind(null, "./Test.podspec", "0.0.2")
).toThrowError("Error updating version in podspec: ./Test.podspec");
});
test("should successfully write new version", async () => {
mockPodspec(specWithVersion("0.0.1"));
Expand All @@ -119,6 +160,47 @@ describe("Cocoapods Plugin", () => {
expect(mock).lastCalledWith(expect.any(String), specWithVersion("0.0.2"));
});
});
describe("updateSourceLocation", () => {
test("should throw error if there is an error writing file", async () => {
mockPodspec(specWithVersion("0.0.1"));

exec.mockReturnValue("commithash");

jest
.spyOn(utilities, "writePodspecContents")
.mockImplementationOnce(() => {
throw new Error("Filesystem Error");
});

await expect(
updateSourceLocation(
"./Test.podspec",
"https://github.com/somefork/auto.git"
)
).rejects.toThrowError(
"Error updating source location in podspec: ./Test.podspec"
);
});
test("should successfully write new source location", async () => {
mockPodspec(specWithVersion("0.0.1"));

const mock = jest.spyOn(utilities, "writePodspecContents");

exec.mockReturnValue(Promise.resolve("commithash"));

await updateSourceLocation(
"./Test.podspec",
"https://github.com/somefork/auto.git"
);
expect(mock).lastCalledWith(
expect.any(String),
specWithVersion(
"0.0.1",
"{ :git => 'https://github.com/somefork/auto.git', :commit => 'commithash' }"
)
);
});
});
describe("modifyConfig hook", () => {
test("should set noVersionPrefix to true", () => {
const config = {};
Expand All @@ -143,6 +225,32 @@ describe("Cocoapods Plugin", () => {
});
});
describe("version hook", () => {
test("should do nothing on dryRun", async () => {
mockPodspec(specWithVersion("0.0.1"));

const mockLog = jest.spyOn(logger.log, "info");

await hooks.version.promise({ bump: Auto.SEMVER.patch, dryRun: true });

expect(exec).toHaveBeenCalledTimes(0);
expect(mockLog).toHaveBeenCalledTimes(1);
});
test("should not use logger on quiet dryRun", async () => {
mockPodspec(specWithVersion("0.0.1"));

const mockLog = jest.spyOn(logger.log, "info");
const mockConsole = jest.spyOn(console, "log");

await hooks.version.promise({
bump: Auto.SEMVER.patch,
dryRun: true,
quiet: true,
});

expect(exec).toHaveBeenCalledTimes(0);
expect(mockLog).toHaveBeenCalledTimes(0);
expect(mockConsole).toHaveBeenCalledTimes(1);
});
test("should version release - patch version", async () => {
mockPodspec(specWithVersion("0.0.1"));

Expand Down Expand Up @@ -175,8 +283,10 @@ describe("Cocoapods Plugin", () => {

const mock = jest
.spyOn(utilities, "writePodspecContents")
.mockImplementationOnce(() => {
throw new Error("Filesystem Error");
.mockImplementation((path, contents) => {
if (contents.includes("1.0.0")) {
throw new Error("Filesystem Error");
}
});

await expect(
Expand Down Expand Up @@ -235,6 +345,99 @@ describe("Cocoapods Plugin", () => {
});
});

describe("canary hook", () => {
test("should do nothing on dryRun", async () => {
mockPodspec(specWithVersion("0.0.1"));
jest.spyOn(Auto, "getPrNumberFromEnv").mockReturnValue(1);

const mockLog = jest.spyOn(logger.log, "info");

await hooks.canary.promise({
bump: Auto.SEMVER.patch,
canaryIdentifier: "canary.1.0",
dryRun: true,
});

expect(exec).toHaveBeenCalledTimes(0);
expect(mockLog).toHaveBeenCalledTimes(1);
});
test("should not use logger on quiet dryRun", async () => {
mockPodspec(specWithVersion("0.0.1"));
jest.spyOn(Auto, "getPrNumberFromEnv").mockReturnValue(1);

const mockLog = jest.spyOn(logger.log, "info");
const mockConsole = jest.spyOn(console, "log");

await hooks.canary.promise({
bump: Auto.SEMVER.patch,
canaryIdentifier: "canary.1.0",
dryRun: true,
quiet: true,
});

expect(exec).toHaveBeenCalledTimes(0);
expect(mockLog).toHaveBeenCalledTimes(0);
expect(mockConsole).toHaveBeenCalledTimes(1);
});
test("should tag with canary version", async () => {
jest.spyOn(Auto, "getPrNumberFromEnv").mockReturnValue(1);
let podSpec = specWithVersion("0.0.1");
jest
.spyOn(utilities, "getPodspecContents")
.mockImplementation(() => podSpec);
const mock = jest
.spyOn(utilities, "writePodspecContents")
.mockImplementation((path, contents) => {
podSpec = contents;
});

const newVersion = await hooks.canary.promise({
bump: "minor" as Auto.SEMVER,
canaryIdentifier: "canary.1.1.1",
});

expect(newVersion).toBe("0.1.0-canary.1.1.1");
expect(exec).toBeCalledTimes(3);
expect(exec).toHaveBeenCalledWith("git", ["checkout", "./Test.podspec"]);

expect(mock).toHaveBeenLastCalledWith(
expect.any(String),
specWithVersion(
"0.1.0-canary.1.1.1",
"{ :git => 'https://github.com/intuit-fork/auto.git', :commit => 'undefined' }"
)
);
});
test("should tag with canary version with no PR number", async () => {
let podSpec = specWithVersion("0.0.1");
jest
.spyOn(utilities, "getPodspecContents")
.mockImplementation(() => podSpec);
const mock = jest
.spyOn(utilities, "writePodspecContents")
.mockImplementation((path, contents) => {
podSpec = contents;
});

const newVersion = await hooks.canary.promise({
bump: "minor" as Auto.SEMVER,
canaryIdentifier: "canary.1.1.1",
});

expect(newVersion).toBe("0.1.0-canary.1.1.1");
expect(exec).toBeCalledTimes(3);
expect(exec).toHaveBeenCalledWith("git", ["checkout", "./Test.podspec"]);

expect(mock).toHaveBeenLastCalledWith(
expect.any(String),
specWithVersion(
"0.1.0-canary.1.1.1",
"{ :git => 'https://github.com/intuit/auto.git', :commit => 'undefined' }"
)
);
});
});

describe("publish hook", () => {
test("should push to trunk if no specsRepo in options", async () => {
mockPodspec(specWithVersion("0.0.1"));
Expand Down
Loading