Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to binary file for component creation #347

Merged
merged 6 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 53 additions & 12 deletions src/openshift/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,31 @@ export class Component extends OpenShiftItem {
static async create(application: OpenShiftObject): Promise<string> {
// should use QuickPickItem with label and description
const sourceTypes: vscode.QuickPickItem[] = [
{
label: 'Git Repository',
description: 'Use an existing git repository as a source for the component'
},
{
label: 'Workspace Directory',
description: 'Use workspace directory as a source for the component'
}];
{
label: 'Git Repository',
description: 'Use an existing git repository as a source for the component'
},
{
label: 'Workspace Directory',
description: 'Use workspace directory as a source for the component'
},
{
label: 'Binary File',
description: 'Use binary file as a source for the component'
}
];
const componentSource = await vscode.window.showQuickPick(sourceTypes, {
placeHolder: "Select source type for component"
});
if (!componentSource) return null;

let command: Promise<string>;
if (componentSource.label === 'Git Repository') {
command = Component.createGit(application);
command = Component.createFromGit(application);
} else if (componentSource.label === 'Workspace Directory') {
command = Component.createFromLocal(application);
} else {
command = Component.createLocal(application);
command = Component.createFromBinary(application);
}
return command.catch((err) => Promise.reject(`Failed to create component with error '${err}'`));
}
Expand Down Expand Up @@ -126,7 +133,7 @@ export class Component extends OpenShiftItem {
return null;
}

private static async createLocal(application: OpenShiftObject): Promise<string> {
private static async createFromLocal(application: OpenShiftObject): Promise<string> {
const folder = await vscode.window.showWorkspaceFolderPick({
placeHolder: 'Select the target workspace folder'
});
Expand Down Expand Up @@ -162,7 +169,7 @@ export class Component extends OpenShiftItem {
.then(() => `Component '${componentName}' successfully created`);
}

private static async createGit(application: OpenShiftObject): Promise<string> {
private static async createFromGit(application: OpenShiftObject): Promise<string> {
const repoURI = await vscode.window.showInputBox({prompt: 'Git repository URI', validateInput:
(value: string) => {
if (validator.isEmpty(value.trim())) {
Expand Down Expand Up @@ -206,4 +213,38 @@ export class Component extends OpenShiftItem {
.then(() => Component.explorer.refresh(application))
.then(() => `Component '${componentName}' successfully created`);
}

private static async createFromBinary(application: OpenShiftObject): Promise<string> {
const binaryFile = await vscode.window.showOpenDialog({
openLabel: 'Select the binary file'
});

if (!binaryFile) return null;

const componentName = await vscode.window.showInputBox({prompt: "Component name", validateInput: (value: string) => {
if (validator.isEmpty(value.trim())) {
return 'Empty component name';
}
}});

if (!componentName) return null;

const componentTypeName = await vscode.window.showQuickPick(Component.odo.getComponentTypes(), {placeHolder: "Component type"});

if (!componentTypeName) return null;

const componentTypeVersion = await vscode.window.showQuickPick(Component.odo.getComponentTypeVersions(componentTypeName), {placeHolder: "Component type Version"});

if (!componentTypeVersion) return null;

const project = application.getParent();
return Progress.execWithProgress({
cancellable: false,
location: vscode.ProgressLocation.Notification,
title: `Creating new component '${componentName}'`
}, [{command: `odo create ${componentTypeName}:${componentTypeVersion} ${componentName} --binary ${binaryFile} --app ${application.getName()} --project ${project.getName()}`, increment: 100}
], Component.odo)
.then(() => Component.explorer.refresh(application))
.then(() => `Component '${componentName}' successfully created`);
}
}
56 changes: 56 additions & 0 deletions test/openshift/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ suite('Openshift/Component', () => {
expect(result).null;
});

test('returns null when no component name selected', async () => {
inputStub.resolves();
const result = await Component.create(appItem);

expect(result).null;
});

test('returns null when no component type selected', async () => {
quickPickStub.onSecondCall().resolves();
const result = await Component.create(appItem);
Expand Down Expand Up @@ -161,6 +168,55 @@ suite('Openshift/Component', () => {
expect(commandStub).calledOnceWith('git.clone', uri);
});
});

suite('from binary file', () => {
let fileStub: sinon.SinonStub;
const filePath = { fsPath: 'path/path' };

setup(() => {
quickPickStub.onFirstCall().resolves({ label: 'Binary File' });
fileStub = sandbox.stub(vscode.window, 'showOpenDialog').resolves([filePath]);
inputStub.resolves(componentItem.getName());
});

test('happy path works', async () => {
const steps = [
{command: `odo create ${componentType}:${version} ${componentItem.getName()} --binary ${filePath.fsPath} --app ${appItem.getName()} --project ${projectItem.getName()}`, increment: 100}
];
const result = await Component.create(appItem);

expect(result).equals(`Component '${componentItem.getName()}' successfully created`);
expect(progressStub).calledOnceWith(sinon.match.object, steps);
});

test('returns null when no binary file selected', async () => {
fileStub.resolves();
const result = await Component.create(appItem);

expect(result).null;
});

test('returns null when no component name selected', async () => {
inputStub.resolves();
const result = await Component.create(appItem);

expect(result).null;
});

test('returns null when no component type selected', async () => {
quickPickStub.onSecondCall().resolves();
const result = await Component.create(appItem);

expect(result).null;
});

test('returns null when no component type version selected', async () => {
quickPickStub.onThirdCall().resolves();
const result = await Component.create(appItem);

expect(result).null;
});
});
});

suite('del', () => {
Expand Down