Skip to content

Commit

Permalink
test: Update git.test.ts spec
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Mar 19, 2024
1 parent 553d72f commit bba6043
Showing 1 changed file with 85 additions and 33 deletions.
118 changes: 85 additions & 33 deletions test/utils/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { afterEach, describe, expect, jest, mock, test } from 'bun:test';
import {
afterEach,
beforeEach,
describe,
expect,
jest,
mock,
spyOn,
test,
} from 'bun:test';
import { execSync } from 'node:child_process';
import fs from 'node:fs';
import cmd from '../../src/utils/cmd';
import git, { localRepoExists } from '../../src/utils/git';
import log from '../../src/utils/log';
import git, {
getRepoName,
getRepoPath,
localRepoExists,
} from '../../src/utils/git';

mock.module('node:fs', () => ({
default: {
Expand All @@ -12,52 +24,92 @@ mock.module('node:fs', () => ({
},
}));

mock.module('../../src/utils/log', () => ({
default: {
info: mock(),
warning: mock(),
success: mock(),
error: mock(),
},
mock.module('node:child_process', () => ({
execSync: mock(() => {
return;
}),
}));

mock.module('../../src/utils/cmd', () => ({
default: {
run: mock(),
runIt: mock(),
},
}));
beforeEach(() => {
spyOn(console, 'log').mockImplementation(() => {});
});

mock.module('../../src/utils/git', () => ({
localRepoExists: mock(),
}));
afterEach(() => {
jest.clearAllMocks();
});

describe('Utils: git', () => {
afterEach(() => {
jest.clearAllMocks();
});

describe('clone', () => {
test('clones the repo if it does not exist locally', () => {
const path = 'test-path';
const repo = 'test-repo';
const ref = 'main';

(fs.existsSync as jest.Mock).mockReturnValue(false);
(fs.lstatSync as jest.Mock).mockReturnValue({ isDirectory: () => false });
(localRepoExists as jest.Mock).mockReturnValue(false);
(fs.existsSync as jest.Mock).mockReturnValueOnce(true);
(fs.lstatSync as jest.Mock).mockReturnValueOnce({
isDirectory: () => true,
});
(fs.existsSync as jest.Mock).mockReturnValueOnce(false);
(fs.lstatSync as jest.Mock).mockReturnValueOnce({
isDirectory: () => false,
});

git.clone({ path, repo, ref });

expect(localRepoExists).toHaveBeenCalledWith({
path: 'test-path/test-repo',
});
expect(cmd.run).toHaveBeenCalledWith(
expect(fs.existsSync).toHaveBeenLastCalledWith(
'test-path/test-repo/.git',
);
expect(fs.lstatSync).toHaveBeenLastCalledWith(path);
expect(execSync).toHaveBeenCalledWith(
'git -C test-path clone test-repo.git -b main',
{
stdio: 'inherit',
encoding: 'utf-8',
},
);
expect(log.success).toHaveBeenCalledWith(
'Repo test-repo was cloned successfully!',
});

test('does not clone the repo if it exists locally', () => {
const path = 'test-path';
const repo = 'test-repo';
const ref = 'main';

(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.lstatSync as jest.Mock).mockReturnValue({ isDirectory: () => true });

git.clone({ path, repo, ref });

expect(fs.existsSync).toHaveBeenLastCalledWith(
'test-path/test-repo/.git',
);
expect(fs.lstatSync).toHaveBeenLastCalledWith('test-path/test-repo/.git');
expect(execSync).not.toHaveBeenCalled();
});
});

describe('gitUtils', () => {
test('get repo name from full repository name', () => {
const repo = 'org/repo';
expect(getRepoName({ repo })).toBe('repo');
});

test('get repo root path from full repository path and name', () => {
const repo = 'org/repo';
const path = '/root/path';
expect(getRepoPath({ repo, path })).toBe('/root/path/repo');
});

test('returns true if local repo exists', () => {
const path = '/root/path/repo';
(fs.existsSync as jest.Mock).mockReturnValueOnce(true);
(fs.lstatSync as jest.Mock).mockReturnValueOnce({
isDirectory: () => true,
});

const repoExists = localRepoExists({ path });

expect(fs.existsSync).toHaveBeenCalledWith(`${path}/.git`);
expect(fs.lstatSync).toHaveBeenCalledWith(`${path}/.git`);
});
});
});

0 comments on commit bba6043

Please sign in to comment.