|
| 1 | +import path from 'path'; |
| 2 | +import 'cli-testing-library/extend-expect'; |
| 3 | +import { exec } from 'child_process'; |
| 4 | +import { prepareTempDir } from './utils'; |
| 5 | +import { promisify } from 'util'; |
| 6 | +import { render } from 'cli-testing-library'; |
| 7 | +import { resolve } from 'path'; |
| 8 | +import { rm } from 'fs'; |
| 9 | +const fsExec = promisify(exec); |
| 10 | +const fsRemove = promisify(rm); |
| 11 | + |
| 12 | +/** |
| 13 | + * git remote -v |
| 14 | + * |
| 15 | + * [no remotes] |
| 16 | + */ |
| 17 | +const prepareNoRemoteGitRepository = async (): Promise<{ |
| 18 | + gitDir: string; |
| 19 | + cleanup: () => Promise<void>; |
| 20 | +}> => { |
| 21 | + const tempDir = await prepareTempDir(); |
| 22 | + await fsExec('git init test', { cwd: tempDir }); |
| 23 | + const gitDir = path.resolve(tempDir, 'test'); |
| 24 | + |
| 25 | + const cleanup = async () => { |
| 26 | + return fsRemove(tempDir, { recursive: true }); |
| 27 | + }; |
| 28 | + return { |
| 29 | + gitDir, |
| 30 | + cleanup |
| 31 | + }; |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * git remote -v |
| 36 | + * |
| 37 | + * origin /tmp/remote.git (fetch) |
| 38 | + * origin /tmp/remote.git (push) |
| 39 | + */ |
| 40 | +const prepareOneRemoteGitRepository = async (): Promise<{ |
| 41 | + gitDir: string; |
| 42 | + cleanup: () => Promise<void>; |
| 43 | +}> => { |
| 44 | + const tempDir = await prepareTempDir(); |
| 45 | + await fsExec('git init --bare remote.git', { cwd: tempDir }); |
| 46 | + await fsExec('git clone remote.git test', { cwd: tempDir }); |
| 47 | + const gitDir = path.resolve(tempDir, 'test'); |
| 48 | + |
| 49 | + const cleanup = async () => { |
| 50 | + return fsRemove(tempDir, { recursive: true }); |
| 51 | + }; |
| 52 | + return { |
| 53 | + gitDir, |
| 54 | + cleanup |
| 55 | + }; |
| 56 | +}; |
| 57 | + |
| 58 | +/** |
| 59 | + * git remote -v |
| 60 | + * |
| 61 | + * origin /tmp/remote.git (fetch) |
| 62 | + * origin /tmp/remote.git (push) |
| 63 | + * other ../remote2.git (fetch) |
| 64 | + * other ../remote2.git (push) |
| 65 | + */ |
| 66 | +const prepareTwoRemotesGitRepository = async (): Promise<{ |
| 67 | + gitDir: string; |
| 68 | + cleanup: () => Promise<void>; |
| 69 | +}> => { |
| 70 | + const tempDir = await prepareTempDir(); |
| 71 | + await fsExec('git init --bare remote.git', { cwd: tempDir }); |
| 72 | + await fsExec('git init --bare other.git', { cwd: tempDir }); |
| 73 | + await fsExec('git clone remote.git test', { cwd: tempDir }); |
| 74 | + const gitDir = path.resolve(tempDir, 'test'); |
| 75 | + await fsExec('git remote add other ../other.git', { cwd: gitDir }); |
| 76 | + |
| 77 | + const cleanup = async () => { |
| 78 | + return fsRemove(tempDir, { recursive: true }); |
| 79 | + }; |
| 80 | + return { |
| 81 | + gitDir, |
| 82 | + cleanup |
| 83 | + }; |
| 84 | +}; |
| 85 | + |
| 86 | +describe('cli flow to push git branch', () => { |
| 87 | + it('do nothing when OCO_GITPUSH is set to false', async () => { |
| 88 | + const { gitDir, cleanup } = await prepareNoRemoteGitRepository(); |
| 89 | + |
| 90 | + await render('echo', [`'console.log("Hello World");' > index.ts`], { |
| 91 | + cwd: gitDir |
| 92 | + }); |
| 93 | + await render('git', ['add index.ts'], { cwd: gitDir }); |
| 94 | + |
| 95 | + const { queryByText, findByText, userEvent } = await render( |
| 96 | + `OCO_AI_PROVIDER='test' OCO_GITPUSH='false' node`, |
| 97 | + [resolve('./out/cli.cjs')], |
| 98 | + { cwd: gitDir } |
| 99 | + ); |
| 100 | + expect(await findByText('Confirm the commit message?')).toBeInTheConsole(); |
| 101 | + userEvent.keyboard('[Enter]'); |
| 102 | + |
| 103 | + expect( |
| 104 | + await queryByText('Choose a remote to push to') |
| 105 | + ).not.toBeInTheConsole(); |
| 106 | + expect( |
| 107 | + await queryByText('Do you want to run `git push`?') |
| 108 | + ).not.toBeInTheConsole(); |
| 109 | + expect( |
| 110 | + await queryByText('Successfully pushed all commits to origin') |
| 111 | + ).not.toBeInTheConsole(); |
| 112 | + expect( |
| 113 | + await queryByText('Command failed with exit code 1') |
| 114 | + ).not.toBeInTheConsole(); |
| 115 | + |
| 116 | + await cleanup(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('push and cause error when there is no remote', async () => { |
| 120 | + const { gitDir, cleanup } = await prepareNoRemoteGitRepository(); |
| 121 | + |
| 122 | + await render('echo', [`'console.log("Hello World");' > index.ts`], { |
| 123 | + cwd: gitDir |
| 124 | + }); |
| 125 | + await render('git', ['add index.ts'], { cwd: gitDir }); |
| 126 | + |
| 127 | + const { queryByText, findByText, userEvent } = await render( |
| 128 | + `OCO_AI_PROVIDER='test' node`, |
| 129 | + [resolve('./out/cli.cjs')], |
| 130 | + { cwd: gitDir } |
| 131 | + ); |
| 132 | + expect(await findByText('Confirm the commit message?')).toBeInTheConsole(); |
| 133 | + userEvent.keyboard('[Enter]'); |
| 134 | + |
| 135 | + expect( |
| 136 | + await queryByText('Choose a remote to push to') |
| 137 | + ).not.toBeInTheConsole(); |
| 138 | + expect( |
| 139 | + await queryByText('Do you want to run `git push`?') |
| 140 | + ).not.toBeInTheConsole(); |
| 141 | + expect( |
| 142 | + await queryByText('Successfully pushed all commits to origin') |
| 143 | + ).not.toBeInTheConsole(); |
| 144 | + |
| 145 | + expect( |
| 146 | + await findByText('Command failed with exit code 1') |
| 147 | + ).toBeInTheConsole(); |
| 148 | + |
| 149 | + await cleanup(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('push when one remote is set', async () => { |
| 153 | + const { gitDir, cleanup } = await prepareOneRemoteGitRepository(); |
| 154 | + |
| 155 | + await render('echo', [`'console.log("Hello World");' > index.ts`], { |
| 156 | + cwd: gitDir |
| 157 | + }); |
| 158 | + await render('git', ['add index.ts'], { cwd: gitDir }); |
| 159 | + |
| 160 | + const { findByText, userEvent } = await render( |
| 161 | + `OCO_AI_PROVIDER='test' node`, |
| 162 | + [resolve('./out/cli.cjs')], |
| 163 | + { cwd: gitDir } |
| 164 | + ); |
| 165 | + expect(await findByText('Confirm the commit message?')).toBeInTheConsole(); |
| 166 | + userEvent.keyboard('[Enter]'); |
| 167 | + |
| 168 | + expect( |
| 169 | + await findByText('Do you want to run `git push`?') |
| 170 | + ).toBeInTheConsole(); |
| 171 | + userEvent.keyboard('[Enter]'); |
| 172 | + |
| 173 | + expect( |
| 174 | + await findByText('Successfully pushed all commits to origin') |
| 175 | + ).toBeInTheConsole(); |
| 176 | + |
| 177 | + await cleanup(); |
| 178 | + }); |
| 179 | + |
| 180 | + it('push when two remotes are set', async () => { |
| 181 | + const { gitDir, cleanup } = await prepareTwoRemotesGitRepository(); |
| 182 | + |
| 183 | + await render('echo', [`'console.log("Hello World");' > index.ts`], { |
| 184 | + cwd: gitDir |
| 185 | + }); |
| 186 | + await render('git', ['add index.ts'], { cwd: gitDir }); |
| 187 | + |
| 188 | + const { findByText, userEvent } = await render( |
| 189 | + `OCO_AI_PROVIDER='test' node`, |
| 190 | + [resolve('./out/cli.cjs')], |
| 191 | + { cwd: gitDir } |
| 192 | + ); |
| 193 | + expect(await findByText('Confirm the commit message?')).toBeInTheConsole(); |
| 194 | + userEvent.keyboard('[Enter]'); |
| 195 | + |
| 196 | + expect(await findByText('Choose a remote to push to')).toBeInTheConsole(); |
| 197 | + userEvent.keyboard('[Enter]'); |
| 198 | + |
| 199 | + expect( |
| 200 | + await findByText('Successfully pushed all commits to origin') |
| 201 | + ).toBeInTheConsole(); |
| 202 | + |
| 203 | + await cleanup(); |
| 204 | + }); |
| 205 | +}); |
0 commit comments