Skip to content

Commit 9697f83

Browse files
mohitsumandgolovin
authored andcommitted
Add support to binary file for component creation (#347)
1 parent 02fb810 commit 9697f83

File tree

2 files changed

+109
-12
lines changed

2 files changed

+109
-12
lines changed

src/openshift/component.ts

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,31 @@ export class Component extends OpenShiftItem {
1515
static async create(application: OpenShiftObject): Promise<string> {
1616
// should use QuickPickItem with label and description
1717
const sourceTypes: vscode.QuickPickItem[] = [
18-
{
19-
label: 'Git Repository',
20-
description: 'Use an existing git repository as a source for the component'
21-
},
22-
{
23-
label: 'Workspace Directory',
24-
description: 'Use workspace directory as a source for the component'
25-
}];
18+
{
19+
label: 'Git Repository',
20+
description: 'Use an existing git repository as a source for the component'
21+
},
22+
{
23+
label: 'Binary File',
24+
description: 'Use binary file as a source for the component'
25+
},
26+
{
27+
label: 'Workspace Directory',
28+
description: 'Use workspace directory as a source for the component'
29+
}
30+
];
2631
const componentSource = await vscode.window.showQuickPick(sourceTypes, {
2732
placeHolder: "Select source type for component"
2833
});
2934
if (!componentSource) return null;
3035

3136
let command: Promise<string>;
3237
if (componentSource.label === 'Git Repository') {
33-
command = Component.createGit(application);
38+
command = Component.createFromGit(application);
39+
} else if (componentSource.label === 'Binary File') {
40+
command = Component.createFromBinary(application);
3441
} else {
35-
command = Component.createLocal(application);
42+
command = Component.createFromLocal(application);
3643
}
3744
return command.catch((err) => Promise.reject(`Failed to create component with error '${err}'`));
3845
}
@@ -131,7 +138,7 @@ export class Component extends OpenShiftItem {
131138
return null;
132139
}
133140

134-
private static async createLocal(application: OpenShiftObject): Promise<string> {
141+
private static async createFromLocal(application: OpenShiftObject): Promise<string> {
135142
const folder = await vscode.window.showWorkspaceFolderPick({
136143
placeHolder: 'Select the target workspace folder'
137144
});
@@ -167,7 +174,7 @@ export class Component extends OpenShiftItem {
167174
.then(() => `Component '${componentName}' successfully created`);
168175
}
169176

170-
private static async createGit(application: OpenShiftObject): Promise<string> {
177+
private static async createFromGit(application: OpenShiftObject): Promise<string> {
171178
const repoURI = await vscode.window.showInputBox({prompt: 'Git repository URI', validateInput:
172179
(value: string) => {
173180
if (validator.isEmpty(value.trim())) {
@@ -211,4 +218,38 @@ export class Component extends OpenShiftItem {
211218
.then(() => Component.explorer.refresh(application))
212219
.then(() => `Component '${componentName}' successfully created`);
213220
}
221+
222+
private static async createFromBinary(application: OpenShiftObject): Promise<string> {
223+
const binaryFile = await vscode.window.showOpenDialog({
224+
openLabel: 'Select the binary file'
225+
});
226+
227+
if (!binaryFile) return null;
228+
229+
const componentName = await vscode.window.showInputBox({prompt: "Component name", validateInput: (value: string) => {
230+
if (validator.isEmpty(value.trim())) {
231+
return 'Empty component name';
232+
}
233+
}});
234+
235+
if (!componentName) return null;
236+
237+
const componentTypeName = await vscode.window.showQuickPick(Component.odo.getComponentTypes(), {placeHolder: "Component type"});
238+
239+
if (!componentTypeName) return null;
240+
241+
const componentTypeVersion = await vscode.window.showQuickPick(Component.odo.getComponentTypeVersions(componentTypeName), {placeHolder: "Component type Version"});
242+
243+
if (!componentTypeVersion) return null;
244+
245+
const project = application.getParent();
246+
return Progress.execWithProgress({
247+
cancellable: false,
248+
location: vscode.ProgressLocation.Notification,
249+
title: `Creating new component '${componentName}'`
250+
}, [{command: `odo create ${componentTypeName}:${componentTypeVersion} ${componentName} --binary ${binaryFile} --app ${application.getName()} --project ${project.getName()}`, increment: 100}
251+
], Component.odo)
252+
.then(() => Component.explorer.refresh(application))
253+
.then(() => `Component '${componentName}' successfully created`);
254+
}
214255
}

test/openshift/component.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ suite('Openshift/Component', () => {
9292
expect(result).null;
9393
});
9494

95+
test('returns null when no component name selected', async () => {
96+
inputStub.resolves();
97+
const result = await Component.create(appItem);
98+
99+
expect(result).null;
100+
});
101+
95102
test('returns null when no component type selected', async () => {
96103
quickPickStub.onSecondCall().resolves();
97104
const result = await Component.create(appItem);
@@ -164,6 +171,55 @@ suite('Openshift/Component', () => {
164171
expect(commandStub).calledOnceWith('git.clone', uri);
165172
});
166173
});
174+
175+
suite('from binary file', () => {
176+
let fileStub: sinon.SinonStub;
177+
const filePath = 'test/sample.war';
178+
179+
setup(() => {
180+
quickPickStub.onFirstCall().resolves({ label: 'Binary File' });
181+
fileStub = sandbox.stub(vscode.window, 'showOpenDialog').resolves(filePath);
182+
inputStub.resolves(componentItem.getName());
183+
});
184+
185+
test('happy path works', async () => {
186+
const steps = [
187+
{command: `odo create ${componentType}:${version} ${componentItem.getName()} --binary ${filePath} --app ${appItem.getName()} --project ${projectItem.getName()}`, increment: 100}
188+
];
189+
const result = await Component.create(appItem);
190+
191+
expect(result).equals(`Component '${componentItem.getName()}' successfully created`);
192+
expect(progressStub).calledOnceWith(sinon.match.object, steps);
193+
});
194+
195+
test('returns null when no binary file selected', async () => {
196+
fileStub.resolves();
197+
const result = await Component.create(appItem);
198+
199+
expect(result).null;
200+
});
201+
202+
test('returns null when no component name selected', async () => {
203+
inputStub.resolves();
204+
const result = await Component.create(appItem);
205+
206+
expect(result).null;
207+
});
208+
209+
test('returns null when no component type selected', async () => {
210+
quickPickStub.onSecondCall().resolves();
211+
const result = await Component.create(appItem);
212+
213+
expect(result).null;
214+
});
215+
216+
test('returns null when no component type version selected', async () => {
217+
quickPickStub.onThirdCall().resolves();
218+
const result = await Component.create(appItem);
219+
220+
expect(result).null;
221+
});
222+
});
167223
});
168224

169225
suite('del', () => {

0 commit comments

Comments
 (0)