Skip to content

Commit 3e863d1

Browse files
Chhavi-Mandowaradhruvparekh12
authored andcommitted
test: Add tests for file-upload redeploy without flags scenarios
1 parent b6b2dbe commit 3e863d1

File tree

6 files changed

+292
-22
lines changed

6 files changed

+292
-22
lines changed

src/adapters/file-upload.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { print } from '../util';
1414
import BaseClass from './base-class';
1515
import { getFileList } from '../util/fs';
1616
import { createSignedUploadUrlMutation, importProjectMutation } from '../graphql';
17-
import { SignedUploadUrlData } from '../types/launch';
17+
import { SignedUploadUrlData, FileUploadMethod } from '../types/launch';
18+
import config from '../config';
1819

1920
export default class FileUpload extends BaseClass {
2021
/**
@@ -40,6 +41,19 @@ export default class FileUpload extends BaseClass {
4041
await this.initApolloClient();
4142

4243
let redeployLatest = this.config['redeploy-latest'];
44+
let redeployLastUpload = this.config['redeploy-last-upload'];
45+
46+
if (!redeployLatest && !redeployLastUpload) {
47+
await this.confirmRedeployment();
48+
const latestRedeploymentConfirmed = await this.confirmLatestRedeployment();
49+
redeployLatest = latestRedeploymentConfirmed;
50+
redeployLastUpload = !latestRedeploymentConfirmed;
51+
}
52+
53+
if (redeployLastUpload && redeployLatest) {
54+
this.log('redeploy-last-upload and redeploy-latest flags are not supported together.', 'error');
55+
this.exit(1);
56+
}
4357

4458
let uploadUid;
4559
if (redeployLatest) {
@@ -52,6 +66,38 @@ export default class FileUpload extends BaseClass {
5266
await this.createNewDeployment(true, uploadUid);
5367
}
5468

69+
private async confirmRedeployment(): Promise<void> {
70+
const redeploy = await cliux.inquire({
71+
type: 'confirm',
72+
name: 'deployLatestCommit',
73+
message: 'Do you want to redeploy this existing Launch project?',
74+
});
75+
if (!redeploy) {
76+
this.log('Project redeployment aborted.', 'info');
77+
this.exit(1);
78+
}
79+
}
80+
81+
private async confirmLatestRedeployment(): Promise<boolean | void> {
82+
const choices = [
83+
...map(config.supportedFileUploadMethods, (fileUploadMethod) => ({
84+
value: fileUploadMethod,
85+
name: `Redeploy with ${fileUploadMethod}`,
86+
}))
87+
];
88+
89+
const selectedFileUploadMethod: FileUploadMethod = await cliux.inquire({
90+
choices: choices,
91+
type: 'search-list',
92+
name: 'fileUploadMethod',
93+
message: 'Choose a redeploy method to proceed',
94+
});
95+
if (selectedFileUploadMethod === FileUploadMethod.LastFileUpload) {
96+
return false;
97+
}
98+
return true;
99+
}
100+
55101
private async handleNewProject(): Promise<void> {
56102
const uploadUid = await this.prepareAndUploadNewProjectFile();
57103
await this.createNewProject(uploadUid);

src/adapters/github.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,35 @@ export default class GitHub extends BaseClass {
3535

3636
private async handleExistingProject(): Promise<void> {
3737
await this.initApolloClient();
38+
3839
const redeployLastUpload = this.config['redeploy-last-upload'];
40+
const redeployLatest = this.config['redeploy-latest'];
3941

4042
if (redeployLastUpload) {
4143
this.log('redeploy-last-upload flag is not supported for Github Project.', 'error');
4244
this.exit(1);
4345
return;
4446
}
4547

48+
if(!redeployLatest && !redeployLastUpload){
49+
await this.confirmLatestRedeployment();
50+
}
51+
4652
await this.createNewDeployment();
4753
}
4854

55+
private async confirmLatestRedeployment(): Promise<void> {
56+
const deployLatestCommit = (await ux.inquire({
57+
type: 'confirm',
58+
name: 'deployLatestCommit',
59+
message: 'Redeploy latest commit?',
60+
}));
61+
if (!deployLatestCommit) {
62+
this.log('Cannot create a new project because its an existing project.', 'info');
63+
this.exit(1);
64+
}
65+
}
66+
4967
private async handleNewProject(): Promise<void> {
5068
// NOTE Step 1: Check is Github connected
5169
if (await this.checkGitHubConnected()) {
@@ -59,6 +77,22 @@ export default class GitHub extends BaseClass {
5977
}
6078
await this.createNewProject();
6179
}
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+
// }
6296

6397
/**
6498
* @method createNewProject - Create new launch project
@@ -241,7 +275,7 @@ export default class GitHub extends BaseClass {
241275
this.log('GitHub connection not found!', 'warn');
242276
await this.connectToAdapterOnUi();
243277
}
244-
278+
console.log('thor5--------------git connected');
245279
return this.config.userConnection;
246280
}
247281

@@ -272,7 +306,7 @@ export default class GitHub extends BaseClass {
272306
this.log('Repository not found in the list!', 'error');
273307
this.exit(1);
274308
}
275-
309+
console.log('thor6--------------git remote available');
276310
return true;
277311
}
278312

src/config/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ const config = {
4040
'Import variables from the local env file',
4141
],
4242
variableType: '',
43-
supportedFrameworksForServerCommands: ['ANGULAR', 'OTHER', 'REMIX']
43+
supportedFrameworksForServerCommands: ['ANGULAR', 'OTHER', 'REMIX'],
44+
supportedFileUploadMethods: ['last file upload', 'new file']
45+
4446
};
4547

4648
export default config;

src/types/launch.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import { LoggerType } from './utils';
77

88
type Providers = 'GitHub' | 'FileUpload';
99

10+
enum FileUploadMethod {
11+
LastFileUpload = 'last file upload',
12+
NewFile = 'new file',
13+
}
14+
1015
type LogFn = (message: string | any, logType?: LoggerType | PrintOptions | undefined) => void;
1116

1217
type ExitFn = (code?: number | undefined) => void;
@@ -38,6 +43,7 @@ type ConfigType = {
3843
deployment?: string;
3944
environment?: string;
4045
provider?: Providers;
46+
fileUploadMethod?: FileUploadMethod;
4147
authorization?: string;
4248
logsApiBaseUrl: string;
4349
projectBasePath: string;
@@ -86,4 +92,5 @@ export {
8692
GraphqlHeaders,
8793
GraphqlApiClientInput,
8894
SignedUploadUrlData,
95+
FileUploadMethod,
8996
};

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

Lines changed: 138 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { cliux } from '@contentstack/cli-utilities';
55
import fs from 'fs';
66
import { FileUpload, BaseClass } from '../../../src/adapters';
77
import { BaseCommand } from '../../../src/base-command';
8-
import e from 'express';
98
import { isNull } from 'util';
109
import { log } from 'console';
10+
import { FileUploadMethod } from '../../../src/types/launch';
1111

1212
describe('File Upload', () => {
1313
let inquireStub, exitStub, prepareApiClientsStub, prepareConfigStub, getConfigStub;
@@ -77,7 +77,23 @@ describe('File Upload', () => {
7777
});
7878

7979
describe('Redeploy existing project', () => {
80-
it('should run file upload flow for existing project where flag passed is redeploy-latest', async () => {
80+
let sandbox;
81+
let processExitStub;
82+
83+
beforeEach(() => {
84+
sandbox = createSandbox();
85+
86+
processExitStub = sandbox.stub(process, 'exit').callsFake((code) => {
87+
throw new Error(code);
88+
});
89+
90+
});
91+
92+
afterEach(() => {
93+
sandbox.restore();
94+
});
95+
96+
it('should run file upload flow successfully for existing project where flag passed is redeploy-latest', async () => {
8197
let adapterConstructorOptions = {
8298
config: {
8399
isExistingProject: true,
@@ -98,7 +114,7 @@ describe('File Upload', () => {
98114
expect(showSuggestionStub.calledOnce).to.be.true;
99115
});
100116

101-
it('should run file upload flow for existing project where flag passed is redeploy-last-upload', async () => {
117+
it('should run file upload flow successfully for existing project where flag passed is redeploy-last-upload', async () => {
102118
let adapterConstructorOptions = {
103119
config: {
104120
isExistingProject: true,
@@ -118,8 +134,126 @@ describe('File Upload', () => {
118134
expect(showDeploymentUrlStub.calledOnce).to.be.true;
119135
expect(showSuggestionStub.calledOnce).to.be.true;
120136
});
121-
});
122137

138+
it('should exit with an error message when both --redeploy-last-upload and --redeploy-latest flags are passed', async () => {
139+
let adapterConstructorOptions = {
140+
config: {
141+
isExistingProject: true,
142+
currentConfig: { uid: '123244', organizationUid: 'bltxxxxxxxx' },
143+
'redeploy-last-upload': true,
144+
'redeploy-latest': true,
145+
},
146+
};
147+
let exitStatusCode;
148+
149+
try {
150+
await new FileUpload(adapterConstructorOptions).run();
151+
} catch (err) {
152+
exitStatusCode = err.message;
153+
}
154+
155+
expect(processExitStub.calledOnceWithExactly(1)).to.be.true;
156+
expect(exitStatusCode).to.equal('1');
157+
expect(initApolloClientStub.calledOnce).to.be.true;
158+
expect(createSignedUploadUrlStub.calledOnce).to.be.false;
159+
expect(archiveStub.calledOnce).to.be.false;
160+
expect(uploadFileStub.calledOnce).to.be.false;
161+
expect(createNewDeploymentStub.calledOnce).to.be.false;
162+
expect(prepareLaunchConfigStub.calledOnce).to.be.false;
163+
expect(showLogsStub.calledOnce).to.be.false;
164+
expect(showDeploymentUrlStub.calledOnce).to.be.false;
165+
expect(showSuggestionStub.calledOnce).to.be.false;
166+
});
167+
168+
it('should show prompt and successfully redeploy with "new file" if the option to redeploy with new file is selected, when --redeploy-latest and --redeploy-last-upload flags are not passed', async () => {
169+
let adapterConstructorOptions = {
170+
config: {
171+
isExistingProject: true,
172+
currentConfig: { uid: '123244', organizationUid: 'bltxxxxxxxx' },
173+
},
174+
};
175+
inquireStub.withArgs({
176+
type: 'confirm',
177+
name: 'deployLatestCommit',
178+
message: 'Do you want to redeploy this existing Launch project?',
179+
}).resolves(true);
180+
inquireStub.resolves(FileUploadMethod.NewFile);
181+
182+
await new FileUpload(adapterConstructorOptions).run();
183+
184+
expect(initApolloClientStub.calledOnce).to.be.true;
185+
expect(createSignedUploadUrlStub.calledOnce).to.be.true;
186+
expect(archiveStub.calledOnce).to.be.true;
187+
expect(uploadFileStub.calledOnce).to.be.true;
188+
expect(createNewDeploymentStub.calledOnce).to.be.true;
189+
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
190+
expect(showLogsStub.calledOnce).to.be.true;
191+
expect(showDeploymentUrlStub.calledOnce).to.be.true;
192+
expect(showSuggestionStub.calledOnce).to.be.true;
193+
});
194+
195+
it('should show prompt and successfully redeploy with "last file upload" if the option to redeploy with last file upload is selected, when --redeploy-latest and --redeploy-last-upload flags are not passed', async () => {
196+
let adapterConstructorOptions = {
197+
config: {
198+
isExistingProject: true,
199+
currentConfig: { uid: '123244', organizationUid: 'bltxxxxxxxx' },
200+
},
201+
};
202+
inquireStub.withArgs({
203+
type: 'confirm',
204+
name: 'deployLatestCommit',
205+
message: 'Do you want to redeploy this existing Launch project?',
206+
}).resolves(true);
207+
inquireStub.resolves(FileUploadMethod.LastFileUpload);
208+
209+
await new FileUpload(adapterConstructorOptions).run();
210+
211+
expect(initApolloClientStub.calledOnce).to.be.true;
212+
expect(createSignedUploadUrlStub.calledOnce).to.be.false;
213+
expect(archiveStub.calledOnce).to.be.false;
214+
expect(uploadFileStub.calledOnce).to.be.false;
215+
expect(createNewDeploymentStub.calledOnce).to.be.true;
216+
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
217+
expect(showLogsStub.calledOnce).to.be.true;
218+
expect(showDeploymentUrlStub.calledOnce).to.be.true;
219+
expect(showSuggestionStub.calledOnce).to.be.true;
220+
});
221+
222+
it('should exit if "No" is selected for prompt to redeploy, when --redeploy-latest and --redeploy-last-upload flags are not passed', async() => {
223+
let adapterConstructorOptions = {
224+
config: {
225+
isExistingProject: true,
226+
currentConfig: { uid: '123244', organizationUid: 'bltxxxxxxxx' },
227+
},
228+
};
229+
inquireStub.withArgs({
230+
type: 'confirm',
231+
name: 'deployLatestCommit',
232+
message: 'Do you want to redeploy this existing Launch project?',
233+
}).resolves(false);
234+
let exitStatusCode;
235+
236+
try {
237+
await new FileUpload(adapterConstructorOptions).run();
238+
} catch (err) {
239+
exitStatusCode = err.message;
240+
}
241+
242+
expect(processExitStub.calledOnceWithExactly(1)).to.be.true;
243+
expect(exitStatusCode).to.equal('1');
244+
expect(initApolloClientStub.calledOnce).to.be.true;
245+
expect(createSignedUploadUrlStub.calledOnce).to.be.false;
246+
expect(archiveStub.calledOnce).to.be.false;
247+
expect(uploadFileStub.calledOnce).to.be.false;
248+
expect(createNewDeploymentStub.calledOnce).to.be.false;
249+
expect(prepareLaunchConfigStub.calledOnce).to.be.false;
250+
expect(showLogsStub.calledOnce).to.be.false;
251+
expect(showDeploymentUrlStub.calledOnce).to.be.false;
252+
expect(showSuggestionStub.calledOnce).to.be.false;
253+
});
254+
255+
});
256+
123257
describe('Deploy new project', () => {
124258
let adapterConstructorOptions = {
125259
config: {

0 commit comments

Comments
 (0)