Skip to content

Commit fa1cf46

Browse files
authored
Fix: a bug that causes an error when pushing without setting git remote (#396)
1 parent 1d19ddd commit fa1cf46

File tree

6 files changed

+218
-6
lines changed

6 files changed

+218
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ All available languages are currently listed in the [i18n](https://github.com/di
224224

225225
### Push to git (gonna be deprecated)
226226

227-
A prompt to ushing to git is on by default but if you would like to turn it off just use:
227+
A prompt for pushing to git is on by default but if you would like to turn it off just use:
228228

229229
```sh
230230
oco config set OCO_GITPUSH=false

src/commands/commit.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,16 @@ ${chalk.grey('——————————————————')}`
107107

108108
const remotes = await getGitRemotes();
109109

110+
// user isn't pushing, return early
111+
if (config.OCO_GITPUSH === false) return;
112+
110113
if (!remotes.length) {
111114
const { stdout } = await execa('git', ['push']);
112115
if (stdout) outro(stdout);
113116
process.exit(0);
114117
}
115118

116-
if (remotes.length === 1 && config.OCO_GITPUSH !== true) {
119+
if (remotes.length === 1) {
117120
const isPushConfirmedByUser = await confirm({
118121
message: 'Do you want to run `git push`?'
119122
});

test/e2e/gitPush.test.ts

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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+
});

test/e2e/oneFile.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ it('cli flow to generate commit message for 1 new file (staged)', async () => {
1717
expect(await findByText('Confirm the commit message?')).toBeInTheConsole();
1818
userEvent.keyboard('[Enter]');
1919

20-
expect(await findByText('Choose a remote to push to')).toBeInTheConsole();
20+
expect(await findByText('Do you want to run `git push`?')).toBeInTheConsole();
2121
userEvent.keyboard('[Enter]');
2222

2323
expect(await findByText('Successfully pushed all commits to origin')).toBeInTheConsole();
@@ -46,7 +46,7 @@ it('cli flow to generate commit message for 1 changed file (not staged)', async
4646

4747
expect(await findByText('Successfully committed')).toBeInTheConsole();
4848

49-
expect(await findByText('Choose a remote to push to')).toBeInTheConsole();
49+
expect(await findByText('Do you want to run `git push`?')).toBeInTheConsole();
5050
userEvent.keyboard('[Enter]');
5151

5252
expect(await findByText('Successfully pushed all commits to origin')).toBeInTheConsole();

test/e2e/prompt-module/commitlint.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ describe('cli flow to generate commit message using @commitlint prompt-module',
209209
oco.userEvent.keyboard('[Enter]');
210210

211211
expect(
212-
await oco.findByText('Choose a remote to push to')
212+
await oco.findByText('Do you want to run `git push`?')
213213
).toBeInTheConsole();
214214
oco.userEvent.keyboard('[Enter]');
215215

test/e2e/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const prepareEnvironment = async (): Promise<{
1515
gitDir: string;
1616
cleanup: () => Promise<void>;
1717
}> => {
18-
const tempDir = await fsMakeTempDir(path.join(tmpdir(), 'opencommit-test-'));
18+
const tempDir = await prepareTempDir();
1919
// Create a remote git repository int the temp directory. This is necessary to execute the `git push` command
2020
await fsExec('git init --bare remote.git', { cwd: tempDir });
2121
await fsExec('git clone remote.git test', { cwd: tempDir });
@@ -30,4 +30,8 @@ export const prepareEnvironment = async (): Promise<{
3030
}
3131
}
3232

33+
export const prepareTempDir = async(): Promise<string> => {
34+
return await fsMakeTempDir(path.join(tmpdir(), 'opencommit-test-'));
35+
}
36+
3337
export const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

0 commit comments

Comments
 (0)