Skip to content

Commit e0dc48f

Browse files
authored
Use the same tool location when running in terminal or through odo.execute (#409)
Fix #305.
1 parent 9b24a9f commit e0dc48f

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/odo.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,13 @@ export class OdoImpl implements Odo {
286286
return [... await this.getComponents(application), ... await this.getServices(application)];
287287
}
288288

289-
public executeInTerminal(command: string, cwd: string = process.cwd(), name: string = 'OpenShift') {
290-
const terminal: Terminal = WindowUtil.createTerminal(name, cwd);
289+
public async executeInTerminal(command: string, cwd: string = process.cwd(), name: string = 'OpenShift') {
290+
const cmd = command.split(' ')[0];
291+
let toolLocation = await ToolsConfig.detectOrDownload(cmd);
292+
if (toolLocation) {
293+
toolLocation = path.dirname(toolLocation);
294+
}
295+
const terminal: Terminal = WindowUtil.createTerminal(name, cwd, toolLocation);
291296
terminal.sendText(command, true);
292297
terminal.show();
293298
}

src/util/windowUtils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import * as path from 'path';
1010
import { Platform } from './platform';
1111

1212
export class WindowUtil {
13-
private static readonly toolsLocation: string = path.resolve(Platform.getUserHomePath(), '.vs-openshift');
1413

15-
static createTerminal(name: string, cwd: string, env: NodeJS.ProcessEnv = process.env): Terminal {
14+
static createTerminal(name: string, cwd: string, toolLocation?: string, env: NodeJS.ProcessEnv = process.env): Terminal {
1615
const finalEnv: NodeJS.ProcessEnv = {};
1716
Object.assign(finalEnv, env);
1817
const key = process.platform === 'win32' ? 'Path' : 'PATH';
19-
finalEnv[key] = `${this.toolsLocation}${path.delimiter}${env[key]}`;
2018

19+
if (toolLocation && env[key] && !env[key].includes(toolLocation)) {
20+
finalEnv[key] = `${toolLocation}${path.delimiter}${env[key]}`;
21+
}
2122
const options: TerminalOptions = {
2223
cwd: cwd,
2324
name: name,

test/odo.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import * as sinon from 'sinon';
1010
import * as chai from 'chai';
1111
import * as sinonChai from 'sinon-chai';
1212
import { ToolsConfig } from '../src/tools';
13+
import { WindowUtil } from '../src/util/windowUtils';
1314
import { window } from 'vscode';
15+
import { Terminal } from 'vscode';
1416
import jsYaml = require('js-yaml');
1517
import { TestItem } from './openshift/testOSItem';
1618
import { ExecException } from 'child_process';
1719
import * as fs from 'fs';
20+
import * as path from 'path';
1821

1922
const expect = chai.expect;
2023
chai.use(sinonChai);
@@ -86,6 +89,24 @@ suite("odo", () => {
8689

8790
expect(result).deep.equals({ error: err, stdout: '', stderr: '' });
8891
});
92+
93+
test('executeInTerminal send command to terminal and shows it', async () => {
94+
const termFake: Terminal = {
95+
name: "name",
96+
processId: Promise.resolve(1),
97+
sendText: sinon.stub(),
98+
show: sinon.stub(),
99+
hide: sinon.stub(),
100+
dispose: sinon.stub()
101+
};
102+
toolsStub.restore();
103+
toolsStub = sandbox.stub(ToolsConfig, 'detectOrDownload').resolves(path.join('segment1', 'segment2'));
104+
let ctStub = sandbox.stub(WindowUtil, 'createTerminal').returns(termFake);
105+
await odoCli.executeInTerminal('cmd');
106+
expect(termFake.sendText).calledOnce;
107+
expect(termFake.show).calledOnce;
108+
expect(ctStub).calledWith('OpenShift', process.cwd(), 'segment1');
109+
});
89110
});
90111

91112
suite('item listings', () => {

test/util/window.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as path from 'path';
1212
import { window, TerminalOptions } from 'vscode';
1313
import { WindowUtil } from '../../src/util/windowUtils';
1414
import { Platform } from '../../src/util/platform';
15+
import { ToolsConfig } from '../../src/tools';
1516

1617
const expect = chai.expect;
1718
chai.use(sinonChai);
@@ -31,23 +32,23 @@ suite('Window Utility', () => {
3132

3233
test('createTerminal creates a terminal object', () => {
3334
WindowUtil.createTerminal('name', process.cwd());
34-
3535
expect(termStub).calledOnce;
3636
});
3737

3838
test('createTerminal adds tools location and shell path to the environment', () => {
39+
const toolLocationDir = path.dirname(path.join("dir", "where", "tool", "is", "located", "tool"));
3940
const env: NodeJS.ProcessEnv = {};
4041
const key = process.platform === 'win32' ? 'Path' : 'PATH';
4142
Object.assign(env, process.env);
42-
env[key] = `${path.join(Platform.getUserHomePath(), '.vs-openshift')}${path.delimiter}${process.env[key]}`;
43+
env[key] = `${toolLocationDir}${path.delimiter}${process.env[key]}`;
4344

4445
const options: TerminalOptions = {
4546
cwd: process.cwd(),
4647
name: 'terminal',
4748
shellPath: process.platform === 'win32' ? undefined : '/bin/bash',
4849
env: env
4950
};
50-
WindowUtil.createTerminal('terminal', process.cwd());
51+
WindowUtil.createTerminal('terminal', process.cwd(), toolLocationDir);
5152

5253
expect(termStub).calledOnceWith(options);
5354
});

0 commit comments

Comments
 (0)