Skip to content

Commit 4d6921e

Browse files
committed
Refactoring for Util and Installer API
Fix adds options parameter to Util.executeCommand with macOS default to process.env and proces.env.PATH altered to have /usr/local/bin. Fix refactors installer.wrteFile to use Util.writeFile. Fix increases unit test code coverage for util.js to 100%.
1 parent 664afc6 commit 4d6921e

3 files changed

Lines changed: 99 additions & 9 deletions

File tree

browser/model/helpers/util.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
'use strict';
22

33
let child_process = require('child_process');
4-
let fs = require('fs');
4+
let fs = require('fs-extra');
55

66
import Platform from '../../services/platform';
77

88
class Util {
99

10-
static executeCommand(command, outputCode=1) {
10+
static executeCommand(command, outputCode=1,options) {
1111
return new Promise((resolve, reject) => {
12-
let options = {
13-
env : Object.assign({},Platform.ENV)
14-
};
1512
if (Platform.OS == 'darwin') {
16-
options.env.PATH = options.env.PATH + ':/usr/local/bin';
13+
if(options === undefined) {
14+
options = {};
15+
}
16+
if(options['env'] === undefined) {
17+
options.env = Object.assign({},Platform.ENV);
18+
}
19+
if(options.env['PATH']) {
20+
options.env.PATH = options.env.PATH + ':/usr/local/bin';
21+
} else {
22+
options.env.PATH = '/usr/local/bin';
23+
}
1724
}
1825
child_process.exec(command, options, defaultCallback(resolve, reject, outputCode));
1926
});
@@ -25,7 +32,7 @@ class Util {
2532
});
2633
}
2734

28-
static writeFile(key, file, data) {
35+
static writeFile(file, data) {
2936
return new Promise((resolve, reject) => {
3037
fs.writeFile(file, data, (err) => {
3138
if (err) {

browser/pages/start/controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class StartController {
3434

3535
launchDevstudio_darwin() {
3636
let devStudioAppPath = path.join(this.installerDataSvc.jbdsDir(), 'Devstudio.app');
37-
Util.executeCommand(`open ${devStudioAppPath}`,1).then((result)=>{
37+
Util.executeCommand(`open ${devStudioAppPath}`,1).then(()=>{
3838
Logger.info('devstudio started sucessfully');
3939
this.exit();
4040
}).catch((error)=>{

test/unit/model/helpers/util-test.js

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import chai, { expect } from 'chai';
44
import sinon from 'sinon';
55
import { default as sinonChai } from 'sinon-chai';
66
import Util from 'browser/model/helpers/util';
7+
import Platform from 'browser/services/platform';
78
import child_process from 'child_process';
8-
import fs from 'fs';
9+
import fs from 'fs-extra';
910
chai.use(sinonChai);
1011

1112
describe('Util', function() {
@@ -39,6 +40,15 @@ describe('Util', function() {
3940
});
4041
});
4142

43+
it('should resolve to standard output when called with only one param', function() {
44+
sandbox.stub(child_process, 'exec').yields(null, 'stdout', 'stderr');
45+
46+
return Util.executeCommand('command')
47+
.then((result) => {
48+
expect(result).to.equal('stdout');
49+
});
50+
});
51+
4252
it('should resolve to error output with 2 as second param', function() {
4353
sandbox.stub(child_process, 'exec').yields(null, 'stdout', 'stderr');
4454

@@ -60,6 +70,58 @@ describe('Util', function() {
6070
expect(error).to.equal(err);
6171
});
6272
});
73+
74+
describe('on macos',function(){
75+
describe('when options parameter is undefined', function(){
76+
it('should set options.env.PATH to "/usr/local/bin" if process.env.PATH is not present or empty', function(){
77+
sandbox.stub(Platform,'getOS').returns('darwin');
78+
sandbox.stub(Platform,'getEnv').returns({PATH:''});
79+
let mock = sandbox.mock(child_process);
80+
let execExpect = mock.expects('exec');
81+
execExpect.withArgs('command', {env: {PATH:'/usr/local/bin'}});
82+
execExpect.yields(null,'stdout','stderr');
83+
return Util.executeCommand('command', 1).then(()=>{
84+
mock.verify();
85+
});
86+
});
87+
88+
it('should add ":/usr/local/bin" to the options.env.PATH if process.env.PATH is not empty', function(){
89+
sandbox.stub(Platform,'getOS').returns('darwin');
90+
sandbox.stub(Platform,'getEnv').returns({PATH:'/bin'});
91+
let mock = sandbox.mock(child_process);
92+
let execExpect = mock.expects('exec');
93+
execExpect.withArgs('command', {env: {PATH:'/bin:/usr/local/bin'}});
94+
execExpect.yields(null,'stdout','stderr');
95+
return Util.executeCommand('command', 1).then(()=>{
96+
mock.verify();
97+
});
98+
});
99+
});
100+
101+
describe('when options parameter is provided', function(){
102+
it('should set options.env.PATH to "/usr/local/bin" if options.env.PATH is not present or empty', function(){
103+
sandbox.stub(Platform,'getOS').returns('darwin');
104+
let mock = sandbox.mock(child_process);
105+
let execExpect = mock.expects('exec');
106+
execExpect.withArgs('command', {env: {PATH:'/usr/local/bin'}});
107+
execExpect.yields(null,'stdout','stderr');
108+
return Util.executeCommand('command', 1, {env: {PATH:''}}).then(()=>{
109+
mock.verify();
110+
});
111+
});
112+
113+
it('should add ":/usr/local/bin" to the options.env.PATH if options.env.PATH is not empty', function(){
114+
sandbox.stub(Platform,'getOS').returns('darwin');
115+
let mock = sandbox.mock(child_process);
116+
let execExpect = mock.expects('exec');
117+
execExpect.withArgs('command', {env: {PATH:'/bin:/usr/local/bin'}});
118+
execExpect.yields(null,'stdout','stderr');
119+
return Util.executeCommand('command', 1, {env: {PATH:'/bin'}}).then(()=>{
120+
mock.verify();
121+
});
122+
});
123+
});
124+
});
63125
});
64126

65127
describe('executeFile', function() {
@@ -82,6 +144,15 @@ describe('Util', function() {
82144
});
83145
});
84146

147+
it('should resolve to standard output when called with only one param', function() {
148+
sandbox.stub(child_process, 'execFile').yields(null, 'stdout', 'stderr');
149+
150+
return Util.executeFile('file', 'arguments')
151+
.then((result) => {
152+
expect(result).to.equal('stdout');
153+
});
154+
});
155+
85156
it('should resolve to error output with 2 as second param', function() {
86157
sandbox.stub(child_process, 'execFile').yields(null, 'stdout', 'stderr');
87158

@@ -210,4 +281,16 @@ describe('Util', function() {
210281
});
211282
});
212283
});
284+
285+
describe('writeFile', function(){
286+
it('calls fs#writeFile with correct arguments', function(){
287+
sandbox.stub(fs, 'writeFile').yields();
288+
289+
return Util.writeFile('file','data')
290+
.then(function() {
291+
expect(fs.writeFile).to.have.been.calledOnce;
292+
expect(fs.writeFile).to.have.been.calledWith('file','data');
293+
});
294+
});
295+
});
213296
});

0 commit comments

Comments
 (0)