Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit d8e38e5

Browse files
[REFACTOR] azure git (#354)
* [REFACTOR] azure git * add more tests * fixed lint error Co-authored-by: Andre Briggs <andrebriggs@users.noreply.github.com>
1 parent dc19cbf commit d8e38e5

File tree

4 files changed

+232
-131
lines changed

4 files changed

+232
-131
lines changed

src/commands/service/create-revision.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("test makePullRequest function", () => {
2626
const createPullRequestFunc = jest.spyOn(azure, "createPullRequest");
2727

2828
// two times because there are two branches: master and stable
29-
createPullRequestFunc.mockReturnValue(Promise.resolve());
29+
createPullRequestFunc.mockReturnValue(Promise.resolve({}));
3030

3131
await makePullRequest(["master", "stable"], {
3232
description: "description",

src/commands/service/create-revision.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ export const makePullRequest = async (
167167
for (const ring of defaultRings) {
168168
const title = opts.title || `[SPK] ${opts.sourceBranch} => ${ring}`;
169169
await createPullRequest(title, opts.sourceBranch!, ring, {
170-
description: opts.description,
171-
orgName: opts.orgName,
172-
originPushUrl: opts.remoteUrl,
173-
personalAccessToken: opts.personalAccessToken
170+
description: opts.description!,
171+
orgName: opts.orgName!,
172+
originPushUrl: opts.remoteUrl!,
173+
personalAccessToken: opts.personalAccessToken!
174174
});
175175
}
176176
};

src/lib/git/azure.test.ts

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import { WebApi } from "azure-devops-node-api";
1111
import uuid from "uuid/v4";
1212
import { Config } from "../../config";
1313
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
14-
import { createPullRequest, GitAPI } from "./azure";
14+
import * as gitutils from "../gitutils";
15+
import {
16+
createPullRequest,
17+
generatePRUrl,
18+
getGitOrigin,
19+
GitAPI
20+
} from "./azure";
1521

1622
////////////////////////////////////////////////////////////////////////////////
1723
// Tests
@@ -24,52 +30,52 @@ afterAll(() => {
2430
disableVerboseLogging();
2531
});
2632

33+
describe("test getGitOrigin function", () => {
34+
it("positive test: originPushUrl provided", async () => {
35+
const res = await getGitOrigin("git-url");
36+
expect(res).toBe("git-url");
37+
});
38+
it("positive test: originPushUrl not provided", async () => {
39+
jest
40+
.spyOn(gitutils, "getOriginUrl")
41+
.mockReturnValueOnce(Promise.resolve("origin-url"));
42+
const res = await getGitOrigin("");
43+
expect(res).toBe("origin-url");
44+
});
45+
it("negative test: getOriginUrl throws error", async () => {
46+
jest
47+
.spyOn(gitutils, "getOriginUrl")
48+
.mockReturnValueOnce(Promise.reject(new Error("Fake")));
49+
await expect(getGitOrigin("")).rejects.toThrow();
50+
});
51+
});
52+
2753
describe("GitAPI", () => {
2854
test("should fail when PAT not set", async () => {
29-
(Config as jest.Mock).mockReturnValue({
55+
(Config as jest.Mock).mockReturnValueOnce({
3056
azure_devops: {}
3157
});
32-
33-
let invalidPatError: Error | undefined;
34-
try {
35-
await GitAPI();
36-
} catch (err) {
37-
invalidPatError = err;
38-
}
39-
expect(invalidPatError).toBeDefined();
58+
await expect(GitAPI()).rejects.toThrow();
4059
});
4160

4261
test("should fail when DevOps org is invalid", async () => {
43-
(Config as jest.Mock).mockReturnValue({
62+
(Config as jest.Mock).mockReturnValueOnce({
4463
azure_devops: {
4564
access_token: uuid()
4665
}
4766
});
48-
49-
let invalidOrgError: Error | undefined;
50-
try {
51-
await GitAPI();
52-
} catch (err) {
53-
invalidOrgError = err;
54-
}
55-
expect(invalidOrgError).toBeDefined();
67+
await expect(GitAPI()).rejects.toThrow();
5668
});
5769

5870
test("should pass if org url and PAT set", async () => {
59-
(Config as jest.Mock).mockReturnValue({
71+
(Config as jest.Mock).mockReturnValueOnce({
6072
azure_devops: {
6173
access_token: uuid(),
6274
org: uuid()
6375
}
6476
});
6577

66-
let error: Error | undefined;
67-
try {
68-
await GitAPI();
69-
} catch (err) {
70-
error = err;
71-
}
72-
expect(error).toBeUndefined();
78+
await GitAPI();
7379
});
7480
});
7581

@@ -160,4 +166,47 @@ describe("createPullRequest", () => {
160166
expect(true).toBe(false);
161167
}
162168
});
169+
it("negative test", async () => {
170+
gitApi.createPullRequest = async () => {
171+
throw new Error("fake");
172+
};
173+
174+
await expect(
175+
createPullRequest("random title", "sourceRef", "targetRef", {
176+
description: uuid(),
177+
originPushUrl: "my-git-url"
178+
})
179+
).rejects.toThrow();
180+
});
181+
it("negative test: TF401179 error", async () => {
182+
gitApi.createPullRequest = async () => {
183+
throw new Error("TF401179");
184+
};
185+
186+
await expect(
187+
createPullRequest("random title", "sourceRef", "targetRef", {
188+
description: uuid(),
189+
originPushUrl: "my-git-url"
190+
})
191+
).rejects.toThrow();
192+
});
193+
});
194+
195+
describe("test generatePRUrl function", async () => {
196+
test("positive test", async () => {
197+
const res = await generatePRUrl({
198+
pullRequestId: 1,
199+
repository: {
200+
id: "test"
201+
}
202+
});
203+
expect(res).toBe("http://foobar.com/pullrequest/1");
204+
});
205+
test("negative test", async () => {
206+
await expect(
207+
generatePRUrl({
208+
repository: {}
209+
})
210+
).rejects.toThrow();
211+
});
163212
});

0 commit comments

Comments
 (0)