Skip to content

Commit

Permalink
Add unit tests for isGitConfigured
Browse files Browse the repository at this point in the history
  • Loading branch information
jculvey committed Oct 10, 2023
1 parent 0036933 commit 013c53d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
41 changes: 41 additions & 0 deletions packages/create-cloudflare/src/__tests__/common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as command from "helpers/command";
import { describe, expect, test, vi } from "vitest";
import { isGitConfigured } from "../common";

function promisify<T>(value: T) {
return new Promise<T>((res) => res(value));
}

describe("isGitConfigured", () => {
test("fully configured", async () => {
const spy = vi.spyOn(command, "runCommand");
spy.mockImplementation((cmd) =>
promisify(cmd.includes("email") ? "[email protected]" : "test user")
);
expect(await isGitConfigured()).toBe(true);
});

test("no name", async () => {
const spy = vi.spyOn(command, "runCommand");
spy.mockImplementation((cmd) =>
promisify(cmd.includes("email") ? "[email protected]" : "")
);
expect(await isGitConfigured()).toBe(false);
});

test("no email", async () => {
const spy = vi.spyOn(command, "runCommand");
spy.mockImplementation((cmd) =>
promisify(cmd.includes("name") ? "test user" : "")
);
expect(await isGitConfigured()).toBe(false);
});

test("runCommand fails", async () => {
const spy = vi.spyOn(command, "runCommand");
spy.mockImplementation(() => {
throw new Error("git not found");
});
expect(await isGitConfigured()).toBe(false);
});
});
6 changes: 3 additions & 3 deletions packages/create-cloudflare/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,19 +371,19 @@ async function getGitVersion() {
/**
* Check whether git is available on the user's machine.
*/
async function isGitInstalled() {
export async function isGitInstalled() {
return (await getGitVersion()) !== null;
}

async function isGitConfigured() {
export async function isGitConfigured() {
try {
const userName = await runCommand("git config user.name", {
useSpinner: false,
silent: true,
});
if (!userName) return false;

const email = await runCommand("git config user.name", {
const email = await runCommand("git config user.email", {
useSpinner: false,
silent: true,
});
Expand Down

0 comments on commit 013c53d

Please sign in to comment.