Skip to content

Commit d99b4bd

Browse files
committed
test: Add tests for github redeploy without flags scenarios
1 parent 3e863d1 commit d99b4bd

File tree

3 files changed

+107
-34
lines changed

3 files changed

+107
-34
lines changed

src/adapters/github.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export default class GitHub extends BaseClass {
4242
if (redeployLastUpload) {
4343
this.log('redeploy-last-upload flag is not supported for Github Project.', 'error');
4444
this.exit(1);
45-
return;
4645
}
4746

4847
if(!redeployLatest && !redeployLastUpload){
@@ -53,11 +52,11 @@ export default class GitHub extends BaseClass {
5352
}
5453

5554
private async confirmLatestRedeployment(): Promise<void> {
56-
const deployLatestCommit = (await ux.inquire({
55+
const deployLatestCommit = await ux.inquire({
5756
type: 'confirm',
5857
name: 'deployLatestCommit',
5958
message: 'Redeploy latest commit?',
60-
}));
59+
});
6160
if (!deployLatestCommit) {
6261
this.log('Cannot create a new project because its an existing project.', 'info');
6362
this.exit(1);
@@ -77,22 +76,6 @@ export default class GitHub extends BaseClass {
7776
}
7877
await this.createNewProject();
7978
}
80-
// private async handleNewProject(): Promise<void> {
81-
// // NOTE Step 1: Check is Github connected
82-
// // NOTE Step 2: check is the git remote available in the user's repo list
83-
// // NOTE Step 3: check is the user has proper git access
84-
// const isGithubConnected = await this.checkGitHubConnected();
85-
// console.log('thor1--------------git connected', isGithubConnected);
86-
// const isGitRemoteAvailableAndValid = await this.checkGitRemoteAvailableAndValid();
87-
// console.log('thor2--------------git remote available', isGitRemoteAvailableAndValid);
88-
// const isUserGitHubAccess = await this.checkUserGitHubAccess();
89-
// console.log('thor6--------------git user access', isUserGitHubAccess);
90-
91-
// if(isGithubConnected && isGitRemoteAvailableAndValid && isUserGitHubAccess){
92-
// await this.prepareForNewProjectCreation();
93-
// }
94-
// await this.createNewProject();
95-
// }
9679

9780
/**
9881
* @method createNewProject - Create new launch project
@@ -275,7 +258,7 @@ export default class GitHub extends BaseClass {
275258
this.log('GitHub connection not found!', 'warn');
276259
await this.connectToAdapterOnUi();
277260
}
278-
console.log('thor5--------------git connected');
261+
279262
return this.config.userConnection;
280263
}
281264

@@ -306,7 +289,7 @@ console.log('thor5--------------git connected');
306289
this.log('Repository not found in the list!', 'error');
307290
this.exit(1);
308291
}
309-
console.log('thor6--------------git remote available');
292+
310293
return true;
311294
}
312295

test/unit/adapters/file-upload.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ts-nocheck
22
import { expect } from 'chai';
3-
import { stub, createSandbox , sinon} from 'sinon';
3+
import { stub, createSandbox , sinon } from 'sinon';
44
import { cliux } from '@contentstack/cli-utilities';
55
import fs from 'fs';
66
import { FileUpload, BaseClass } from '../../../src/adapters';
@@ -47,11 +47,14 @@ describe('File Upload', () => {
4747
showLogsStub,
4848
showDeploymentUrlStub,
4949
showSuggestionStub;
50+
const signedUploadUrlData = { uploadUrl: 'http://example.com/upload', uploadUid: '123456789' };
51+
const zipName = 'test.zip';
52+
const zipPath = '/path/to/zip';
5053

5154
beforeEach(() => {
5255
initApolloClientStub = stub(BaseClass.prototype, 'initApolloClient').resolves();
53-
createSignedUploadUrlStub = stub(FileUpload.prototype, 'createSignedUploadUrl').resolves({ uploadUrl: 'http://example.com/upload', uploadUid: '123456789' });
54-
archiveStub = stub(FileUpload.prototype, 'archive').resolves({ zipName: 'test.zip', zipPath: '/path/to/zip' });
56+
createSignedUploadUrlStub = stub(FileUpload.prototype, 'createSignedUploadUrl').resolves(signedUploadUrlData);
57+
archiveStub = stub(FileUpload.prototype, 'archive').resolves({ zipName, zipPath });
5558
uploadFileStub = stub(FileUpload.prototype, 'uploadFile').resolves();
5659
createNewDeploymentStub = stub(FileUpload.prototype, 'createNewDeployment').resolves();
5760
prepareAndUploadNewProjectFile = stub(FileUpload.prototype, 'prepareAndUploadNewProjectFile').resolves();
@@ -107,7 +110,9 @@ describe('File Upload', () => {
107110
expect(createSignedUploadUrlStub.calledOnce).to.be.true;
108111
expect(archiveStub.calledOnce).to.be.true;
109112
expect(uploadFileStub.calledOnce).to.be.true;
113+
expect(uploadFileStub.args[0]).to.deep.equal([zipName, zipPath, signedUploadUrlData]);
110114
expect(createNewDeploymentStub.calledOnce).to.be.true;
115+
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, signedUploadUrlData.uploadUid]);
111116
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
112117
expect(showLogsStub.calledOnce).to.be.true;
113118
expect(showDeploymentUrlStub.calledOnce).to.be.true;
@@ -129,6 +134,7 @@ describe('File Upload', () => {
129134
expect(archiveStub.calledOnce).to.be.false;
130135
expect(uploadFileStub.calledOnce).to.be.false;
131136
expect(createNewDeploymentStub.calledOnce).to.be.true;
137+
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, undefined]);
132138
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
133139
expect(showLogsStub.calledOnce).to.be.true;
134140
expect(showDeploymentUrlStub.calledOnce).to.be.true;
@@ -185,7 +191,9 @@ describe('File Upload', () => {
185191
expect(createSignedUploadUrlStub.calledOnce).to.be.true;
186192
expect(archiveStub.calledOnce).to.be.true;
187193
expect(uploadFileStub.calledOnce).to.be.true;
194+
expect(uploadFileStub.args[0]).to.deep.equal([zipName, zipPath, signedUploadUrlData]);
188195
expect(createNewDeploymentStub.calledOnce).to.be.true;
196+
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, signedUploadUrlData.uploadUid]);
189197
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
190198
expect(showLogsStub.calledOnce).to.be.true;
191199
expect(showDeploymentUrlStub.calledOnce).to.be.true;
@@ -213,6 +221,7 @@ describe('File Upload', () => {
213221
expect(archiveStub.calledOnce).to.be.false;
214222
expect(uploadFileStub.calledOnce).to.be.false;
215223
expect(createNewDeploymentStub.calledOnce).to.be.true;
224+
expect(createNewDeploymentStub.args[0]).to.deep.equal([true, undefined]);
216225
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
217226
expect(showLogsStub.calledOnce).to.be.true;
218227
expect(showDeploymentUrlStub.calledOnce).to.be.true;

test/unit/adapters/github.test.ts

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { cliux } from '@contentstack/cli-utilities';
55
import { githubAdapterMockData } from '../mock/index';
66
import { GitHub, BaseClass } from '../../../src/adapters';
77
import { BaseCommand } from '../../../src/base-command';
8-
import { exit } from 'process';
98
import fs from 'fs';
109

1110
describe('GitHub', () => {
@@ -79,34 +78,116 @@ describe('GitHub', () => {
7978
});
8079

8180
describe('Redeploy existing project', () => {
82-
it('should abort github flow for existing project and flag redeploy-last-upload is passed', async () => {
81+
let sandbox;
82+
let processExitStub;
83+
84+
beforeEach(() => {
85+
sandbox = createSandbox();
86+
87+
processExitStub = sandbox.stub(process, 'exit').callsFake((code) => {
88+
throw new Error(code);
89+
});
90+
91+
});
92+
93+
afterEach(() => {
94+
sandbox.restore();
95+
});
96+
97+
it('should successfully run github flow for existing project when flag redeploy-latest is passed ', async () => {
98+
let adapterConstructorOptions = {
99+
config: {
100+
isExistingProject: true,
101+
'redeploy-latest': true,
102+
},
103+
};
104+
105+
await new GitHub(adapterConstructorOptions).run();
106+
107+
expect(initApolloClientStub.calledOnce).to.be.true;
108+
expect(createNewDeploymentStub.calledOnce).to.be.true;
109+
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
110+
expect(showLogsStub.calledOnce).to.be.true;
111+
expect(showDeploymentUrlStub.calledOnce).to.be.true;
112+
expect(showSuggestionStub.calledOnce).to.be.true;
113+
});
114+
115+
it('should abort github flow for existing project when flag redeploy-last-upload is passed', async () => {
83116
const adapterConstructorOptions = {
84117
config: {
85118
isExistingProject: true,
86119
'redeploy-last-upload': true,
87120
},
88121
};
89-
const exitStub = stub(process, 'exit');
90-
const githubInstance = new GitHub(adapterConstructorOptions);
122+
let exitStatusCode;
91123

92-
await githubInstance.handleExistingProject();
124+
try {
125+
await new GitHub(adapterConstructorOptions).run();
126+
} catch (err) {
127+
exitStatusCode = err.message;
128+
}
93129

94-
expect(exitStub.calledOnceWithExactly(1)).to.be.true;
130+
expect(processExitStub.calledOnceWithExactly(1)).to.be.true;
131+
expect(exitStatusCode).to.equal('1');
132+
expect(initApolloClientStub.calledOnce).to.be.true;
133+
expect(createNewDeploymentStub.calledOnce).to.be.false;
134+
expect(prepareLaunchConfigStub.calledOnce).to.be.false;
135+
expect(showLogsStub.calledOnce).to.be.false;
136+
expect(showDeploymentUrlStub.calledOnce).to.be.false;
137+
expect(showSuggestionStub.calledOnce).to.be.false;
95138
});
96139

97-
it('should run github flow for existing project and flag redeploy-latest is passed ', async () => {
98-
let adapterConstructorOptions = {
140+
it('should show prompt and successfully redeploy with "latest commit" if the option to redeploy is selected, when --redeploy-latest flag is not passed', async() => {
141+
const adapterConstructorOptions = {
99142
config: {
100-
isExistingProject: true,
101-
'redeploy-latest': true,
143+
isExistingProject: true
102144
},
103145
};
146+
inquireStub.withArgs({
147+
type: 'confirm',
148+
name: 'deployLatestCommit',
149+
message: 'Redeploy latest commit?',
150+
}).resolves(true);
104151

105152
await new GitHub(adapterConstructorOptions).run();
106153

107154
expect(initApolloClientStub.calledOnce).to.be.true;
108155
expect(createNewDeploymentStub.calledOnce).to.be.true;
156+
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
157+
expect(showLogsStub.calledOnce).to.be.true;
158+
expect(showDeploymentUrlStub.calledOnce).to.be.true;
159+
expect(showSuggestionStub.calledOnce).to.be.true;
109160
});
161+
162+
it('should exit if "No" is selected for prompt to redeploy, when --redeploy-latest flag is not passed', async() => {
163+
const adapterConstructorOptions = {
164+
config: {
165+
isExistingProject: true
166+
},
167+
};
168+
inquireStub.withArgs({
169+
type: 'confirm',
170+
name: 'deployLatestCommit',
171+
message: 'Redeploy latest commit?',
172+
}).resolves(false);
173+
let exitStatusCode;
174+
175+
try {
176+
await new GitHub(adapterConstructorOptions).run();
177+
} catch (err) {
178+
exitStatusCode = err.message;
179+
}
180+
181+
expect(processExitStub.calledOnceWithExactly(1)).to.be.true;
182+
expect(exitStatusCode).to.equal('1');
183+
expect(initApolloClientStub.calledOnce).to.be.true;
184+
expect(createNewDeploymentStub.calledOnce).to.be.false;
185+
expect(prepareLaunchConfigStub.calledOnce).to.be.false;
186+
expect(showLogsStub.calledOnce).to.be.false;
187+
expect(showDeploymentUrlStub.calledOnce).to.be.false;
188+
expect(showSuggestionStub.calledOnce).to.be.false;
189+
});
190+
110191
});
111192

112193
describe('Deploy new project', () => {

0 commit comments

Comments
 (0)