diff --git a/test/utils/git.test.ts b/test/utils/git.test.ts index bc24933..0cfa4f1 100644 --- a/test/utils/git.test.ts +++ b/test/utils/git.test.ts @@ -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: { @@ -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(); + }); }); });