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