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

Commit ac3ab36

Browse files
committed
Fixing tests
1 parent f6b9097 commit ac3ab36

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

src/commands/hld/reconcile.test.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
IReconcileDependencies,
1717
reconcileHld,
1818
testAndGetAbsPath,
19-
tryGetGitOrigin,
2019
validateInputs
2120
} from "./reconcile";
2221
import * as reconcile from "./reconcile";
@@ -104,31 +103,6 @@ describe("checkForFabrikate", () => {
104103
});
105104
});
106105

107-
describe("tryGetGitOrigin", () => {
108-
it("attempts to retrieve azdo git origin", async () => {
109-
const originUrl = "http://github.com/repo/url";
110-
111-
(getAzdoOriginUrl as jest.Mock).mockReturnValue(originUrl);
112-
113-
const originUrlResponse = await tryGetGitOrigin();
114-
expect(originUrlResponse).toEqual(originUrl);
115-
expect(getAzdoOriginUrl).toHaveBeenCalledTimes(1);
116-
});
117-
118-
it("attempts to retrieve git origin from using git cli", async () => {
119-
const originUrl = "http://github.com/repo/url";
120-
(getAzdoOriginUrl as jest.Mock).mockRejectedValue("some reason");
121-
122-
(getOriginUrl as jest.Mock).mockReturnValue(originUrl);
123-
124-
const originUrlResponse = await tryGetGitOrigin();
125-
126-
expect(originUrlResponse).toEqual(originUrl);
127-
expect(getAzdoOriginUrl).toHaveBeenCalledTimes(1);
128-
expect(getOriginUrl).toHaveBeenCalledTimes(1);
129-
});
130-
});
131-
132106
describe("testAndGetAbsPath", () => {
133107
it("fails to test and get an absolute path for a file", () => {
134108
const test = jest.fn();

src/commands/hld/reconcile.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Bedrock } from "../../config";
99
import { assertIsStringWithContent } from "../../lib/assertions";
1010
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
1111
import { generateAccessYaml } from "../../lib/fileutils";
12-
import { getAzdoOriginUrl, getOriginUrl } from "../../lib/gitutils";
12+
import { tryGetGitOrigin } from "../../lib/gitutils";
1313
import { TraefikIngressRoute } from "../../lib/traefik/ingress-route";
1414
import {
1515
ITraefikMiddleware,
@@ -210,17 +210,6 @@ export const commandDecorator = (command: commander.Command) => {
210210
);
211211
};
212212

213-
export const tryGetGitOrigin = async (
214-
absRepoPath?: string
215-
): Promise<string> => {
216-
return getAzdoOriginUrl().catch(_ => {
217-
logger.warn(
218-
"Could not get Git Origin for Azure DevOps - are you running 'spk' _not_ in a pipeline?"
219-
);
220-
return getOriginUrl(absRepoPath);
221-
});
222-
};
223-
224213
export const reconcileHld = async (
225214
dependencies: IReconcileDependencies,
226215
bedrockYaml: IBedrockFile,

src/lib/gitutils.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
getRepositoryName,
1111
getRepositoryUrl,
1212
pushBranch,
13-
safeGitUrlForLogging
13+
safeGitUrlForLogging,
14+
tryGetGitOrigin
1415
} from "../lib/gitutils";
1516
import { disableVerboseLogging, enableVerboseLogging } from "../logger";
1617
import { exec } from "./shell";
@@ -211,6 +212,35 @@ describe("pushBranch", () => {
211212
});
212213
});
213214

215+
describe("tryGetGitOrigin", () => {
216+
it("attempts to retrieve azdo git origin", async () => {
217+
const originUrl = "http://github.com/repo/url";
218+
219+
when(exec as jest.Mock)
220+
.calledWith("echo", ["$(Build.Repository.Uri)"])
221+
.mockReturnValue(originUrl);
222+
223+
const originUrlResponse = await tryGetGitOrigin();
224+
expect(originUrlResponse).toEqual(originUrl);
225+
});
226+
227+
it("attempts to retrieve git origin from using git cli", async () => {
228+
const originUrl = "http://github.com/repo/url";
229+
// Echoing variable from AzDo fails… trying Git
230+
when(exec as jest.Mock)
231+
.calledWith("echo", ["$(Build.Repository.Uri)"])
232+
.mockRejectedValue("some reason");
233+
234+
// Retrieving url from Git succeeds
235+
when(exec as jest.Mock)
236+
.calledWith("git", ["config", "--get", "remote.origin.url"])
237+
.mockReturnValue(originUrl);
238+
239+
const originUrlResponse = await tryGetGitOrigin();
240+
expect(originUrlResponse).toEqual(originUrl);
241+
});
242+
});
243+
214244
describe("getOriginUrl", () => {
215245
it("should call exec with the proper git arguments", async () => {
216246
const originUrl = "foo";

src/lib/gitutils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ export const pushBranch = async (branchName: string): Promise<void> => {
110110
}
111111
};
112112

113+
/**
114+
* Tries to fetch the Git origin from an AzDo set environment variable, else falls back to fetching it from Git directly.
115+
* @param absRepoPath The Absolute Path to the Repository to fetch the origin url.
116+
*/
117+
export const tryGetGitOrigin = async (
118+
absRepoPath?: string
119+
): Promise<string> => {
120+
return getAzdoOriginUrl().catch(_ => {
121+
logger.warn(
122+
"Could not get Git Origin for Azure DevOps - are you running 'spk' _not_ in a pipeline?"
123+
);
124+
return getOriginUrl(absRepoPath);
125+
});
126+
};
127+
113128
/**
114129
* Gets the origin url.
115130
* @param absRepositoryPath The Absolute Path to the Repository to fetch the origin

0 commit comments

Comments
 (0)