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 537ddac
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions test/utils/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { afterEach, describe, expect, jest, mock, test } from 'bun:test';
import { beforeEach, afterEach, describe, expect, jest, mock, test, spyOn } from 'bun:test';
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 from '../../src/utils/git';
import { execSync } from 'node:child_process';

mock.module('node:fs', () => ({
default: {
Expand All @@ -12,52 +11,57 @@ 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(
'git -C test-path clone test-repo.git -b main',
);
expect(log.success).toHaveBeenCalledWith(
'Repo test-repo was cloned successfully!',
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',
},
);
});

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();
});
});
});

0 comments on commit 537ddac

Please sign in to comment.