"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); require("cli-testing-library/extend-expect"); const child_process_1 = require("child_process"); const utils_1 = require("./utils"); const util_1 = require("util"); const cli_testing_library_1 = require("cli-testing-library"); const path_2 = require("path"); const fs_1 = require("fs"); const fsExec = (0, util_1.promisify)(child_process_1.exec); const fsRemove = (0, util_1.promisify)(fs_1.rm); /** * git remote -v * * [no remotes] */ const prepareNoRemoteGitRepository = async () => { const tempDir = await (0, utils_1.prepareTempDir)(); await fsExec('git init test', { cwd: tempDir }); const gitDir = path_1.default.resolve(tempDir, 'test'); const cleanup = async () => { return fsRemove(tempDir, { recursive: true }); }; return { gitDir, cleanup }; }; /** * git remote -v * * origin /tmp/remote.git (fetch) * origin /tmp/remote.git (push) */ const prepareOneRemoteGitRepository = async () => { const tempDir = await (0, utils_1.prepareTempDir)(); await fsExec('git init --bare remote.git', { cwd: tempDir }); await fsExec('git clone remote.git test', { cwd: tempDir }); const gitDir = path_1.default.resolve(tempDir, 'test'); const cleanup = async () => { return fsRemove(tempDir, { recursive: true }); }; return { gitDir, cleanup }; }; /** * git remote -v * * origin /tmp/remote.git (fetch) * origin /tmp/remote.git (push) * other ../remote2.git (fetch) * other ../remote2.git (push) */ const prepareTwoRemotesGitRepository = async () => { const tempDir = await (0, utils_1.prepareTempDir)(); await fsExec('git init --bare remote.git', { cwd: tempDir }); await fsExec('git init --bare other.git', { cwd: tempDir }); await fsExec('git clone remote.git test', { cwd: tempDir }); const gitDir = path_1.default.resolve(tempDir, 'test'); await fsExec('git remote add other ../other.git', { cwd: gitDir }); const cleanup = async () => { return fsRemove(tempDir, { recursive: true }); }; return { gitDir, cleanup }; }; describe('cli flow to push git branch', () => { it('do nothing when OCO_GITPUSH is set to false', async () => { const { gitDir, cleanup } = await prepareNoRemoteGitRepository(); await (0, cli_testing_library_1.render)('echo', [`'console.log("Hello World");' > index.ts`], { cwd: gitDir }); await (0, cli_testing_library_1.render)('git', ['add index.ts'], { cwd: gitDir }); const { queryByText, findByText, userEvent } = await (0, cli_testing_library_1.render)(`OCO_AI_PROVIDER='test' OCO_GITPUSH='false' node`, [(0, path_2.resolve)('./out/cli.cjs')], { cwd: gitDir }); expect(await findByText('Confirm the commit message?')).toBeInTheConsole(); userEvent.keyboard('[Enter]'); expect(await queryByText('Choose a remote to push to')).not.toBeInTheConsole(); expect(await queryByText('Do you want to run `git push`?')).not.toBeInTheConsole(); expect(await queryByText('Successfully pushed all commits to origin')).not.toBeInTheConsole(); expect(await queryByText('Command failed with exit code 1')).not.toBeInTheConsole(); await cleanup(); }); it('push and cause error when there is no remote', async () => { const { gitDir, cleanup } = await prepareNoRemoteGitRepository(); await (0, cli_testing_library_1.render)('echo', [`'console.log("Hello World");' > index.ts`], { cwd: gitDir }); await (0, cli_testing_library_1.render)('git', ['add index.ts'], { cwd: gitDir }); const { queryByText, findByText, userEvent } = await (0, cli_testing_library_1.render)(`OCO_AI_PROVIDER='test' OCO_GITPUSH='true' node`, [(0, path_2.resolve)('./out/cli.cjs')], { cwd: gitDir }); expect(await findByText('Confirm the commit message?')).toBeInTheConsole(); userEvent.keyboard('[Enter]'); expect(await queryByText('Choose a remote to push to')).not.toBeInTheConsole(); expect(await queryByText('Do you want to run `git push`?')).not.toBeInTheConsole(); expect(await queryByText('Successfully pushed all commits to origin')).not.toBeInTheConsole(); expect(await findByText('Command failed with exit code 1')).toBeInTheConsole(); await cleanup(); }); it('push when one remote is set', async () => { const { gitDir, cleanup } = await prepareOneRemoteGitRepository(); await (0, cli_testing_library_1.render)('echo', [`'console.log("Hello World");' > index.ts`], { cwd: gitDir }); await (0, cli_testing_library_1.render)('git', ['add index.ts'], { cwd: gitDir }); const { findByText, userEvent } = await (0, cli_testing_library_1.render)(`OCO_AI_PROVIDER='test' OCO_GITPUSH='true' node`, [(0, path_2.resolve)('./out/cli.cjs')], { cwd: gitDir }); expect(await findByText('Confirm the commit message?')).toBeInTheConsole(); userEvent.keyboard('[Enter]'); expect(await findByText('Do you want to run `git push`?')).toBeInTheConsole(); userEvent.keyboard('[Enter]'); expect(await findByText('Successfully pushed all commits to origin')).toBeInTheConsole(); await cleanup(); }); it('push when two remotes are set', async () => { const { gitDir, cleanup } = await prepareTwoRemotesGitRepository(); await (0, cli_testing_library_1.render)('echo', [`'console.log("Hello World");' > index.ts`], { cwd: gitDir }); await (0, cli_testing_library_1.render)('git', ['add index.ts'], { cwd: gitDir }); const { findByText, userEvent } = await (0, cli_testing_library_1.render)(`OCO_AI_PROVIDER='test' OCO_GITPUSH='true' node`, [(0, path_2.resolve)('./out/cli.cjs')], { cwd: gitDir }); expect(await findByText('Confirm the commit message?')).toBeInTheConsole(); userEvent.keyboard('[Enter]'); expect(await findByText('Choose a remote to push to')).toBeInTheConsole(); userEvent.keyboard('[Enter]'); expect(await findByText('Successfully pushed all commits to origin')).toBeInTheConsole(); await cleanup(); }); });