Skip to content

Commit 1fc845d

Browse files
committed
feat(system): use systeminformation for os version information
1 parent c7875c9 commit 1fc845d

File tree

8 files changed

+86
-189
lines changed

8 files changed

+86
-189
lines changed

lib/command.js

+3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ class Command {
169169
}
170170

171171
try {
172+
debug('loading operating system information');
173+
await ui.run(() => system.loadOsInfo(), 'Inspecting operating system', {clear: true});
174+
172175
if (this.runPreChecks) {
173176
const preChecks = require('./utils/pre-checks');
174177
await preChecks(ui, system);

lib/system.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,7 @@ class System {
8181
* @public
8282
*/
8383
get operatingSystem() {
84-
if (!this._operatingSystem) {
85-
const getOS = require('./utils/get-os');
86-
this._operatingSystem = getOS(this.platform);
87-
88-
return this._operatingSystem;
89-
}
90-
91-
return this._operatingSystem;
84+
return this._osInfo || {};
9285
}
9386

9487
/**
@@ -346,6 +339,15 @@ class System {
346339
return available;
347340
}
348341

342+
async loadOsInfo() {
343+
if (this._osInfo) {
344+
return;
345+
}
346+
347+
const {osInfo} = require('systeminformation/lib/osinfo');
348+
this._osInfo = await osInfo();
349+
}
350+
349351
/**
350352
* Writes an error message to a global log file.
351353
*

lib/ui/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class UI {
490490
const ghostVersion = version ? ` Ghost Version: ${version}\n` : '';
491491

492492
return 'Debug Information:\n' +
493-
` OS: ${system.operatingSystem.os}, v${system.operatingSystem.version}\n` +
493+
` OS: ${system.operatingSystem.distro}, v${system.operatingSystem.release}\n` +
494494
` Node Version: ${process.version}\n` +
495495
ghostVersion +
496496
` Ghost-CLI Version: ${system.cliVersion}\n` +

lib/utils/get-os.js

-41
This file was deleted.

test/unit/command-spec.js

+29-13
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,11 @@ describe('Unit: Command', function () {
283283
});
284284

285285
it('loads system and ui dependencies, calls run method', async function () {
286-
const uiStub = sinon.stub().returns({ui: true});
286+
const run = sinon.stub().callsFake(fn => fn());
287+
const uiStub = sinon.stub().returns({ui: true, run});
287288
const setEnvironmentStub = sinon.stub();
288-
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub});
289+
const loadOsInfo = sinon.stub().resolves();
290+
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub, loadOsInfo});
289291

290292
const Command = proxyquire(modulePath, {
291293
'./ui': uiStub,
@@ -312,15 +314,19 @@ describe('Unit: Command', function () {
312314
expect(setEnvironmentStub.calledOnce).to.be.true;
313315
expect(setEnvironmentStub.calledWithExactly(true, true)).to.be.true;
314316
expect(systemStub.calledOnce).to.be.true;
315-
expect(systemStub.calledWithExactly({ui: true}, [{extensiona: true}])).to.be.true;
317+
expect(systemStub.calledWithExactly({ui: true, run}, [{extensiona: true}])).to.be.true;
318+
expect(run.calledOnce).to.be.true;
319+
expect(loadOsInfo.calledOnce).to.be.true;
316320
expect(runStub.calledOnce).to.be.true;
317321
expect(runStub.calledWithExactly({verbose: true, prompt: true, development: true, auto: false})).to.be.true;
318322
});
319323

320324
it('binds cleanup handler if cleanup method is defined', async function () {
321-
const uiStub = sinon.stub().returns({ui: true});
325+
const run = sinon.stub().callsFake(fn => fn());
326+
const uiStub = sinon.stub().returns({ui: true, run});
322327
const setEnvironmentStub = sinon.stub();
323-
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub});
328+
const loadOsInfo = sinon.stub().resolves();
329+
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub, loadOsInfo});
324330

325331
const Command = proxyquire(modulePath, {
326332
'./ui': uiStub,
@@ -351,7 +357,9 @@ describe('Unit: Command', function () {
351357
expect(setEnvironmentStub.calledOnce).to.be.true;
352358
expect(setEnvironmentStub.calledWithExactly(true, true)).to.be.true;
353359
expect(systemStub.calledOnce).to.be.true;
354-
expect(systemStub.calledWithExactly({ui: true}, [{extensiona: true}])).to.be.true;
360+
expect(systemStub.calledWithExactly({ui: true, run}, [{extensiona: true}])).to.be.true;
361+
expect(run.calledOnce).to.be.true;
362+
expect(loadOsInfo.calledOnce).to.be.true;
355363
expect(runStub.calledOnce).to.be.true;
356364
expect(runStub.calledWithExactly({verbose: false, prompt: false, development: false, auto: true})).to.be.true;
357365
expect(onStub.calledTwice).to.be.true;
@@ -360,9 +368,11 @@ describe('Unit: Command', function () {
360368
});
361369

362370
it('runs updateCheck if checkVersion property is true on command class', async function () {
363-
const uiStub = sinon.stub().returns({ui: true});
371+
const run = sinon.stub().callsFake(fn => fn());
372+
const uiStub = sinon.stub().returns({ui: true, run});
364373
const setEnvironmentStub = sinon.stub();
365-
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub});
374+
const loadOsInfo = sinon.stub().resolves();
375+
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub, loadOsInfo});
366376
const preChecksStub = sinon.stub().resolves();
367377

368378
const Command = proxyquire(modulePath, {
@@ -393,18 +403,22 @@ describe('Unit: Command', function () {
393403
expect(setEnvironmentStub.calledOnce).to.be.true;
394404
expect(setEnvironmentStub.calledWithExactly(true, true)).to.be.true;
395405
expect(systemStub.calledOnce).to.be.true;
396-
expect(systemStub.calledWithExactly({ui: true}, [{extensiona: true}])).to.be.true;
406+
expect(systemStub.calledWithExactly({ui: true, run}, [{extensiona: true}])).to.be.true;
407+
expect(run.calledOnce).to.be.true;
408+
expect(loadOsInfo.calledOnce).to.be.true;
397409
expect(preChecksStub.calledOnce).to.be.true;
398-
expect(preChecksStub.calledWithExactly({ui: true}, {setEnvironment: setEnvironmentStub})).to.be.true;
410+
expect(preChecksStub.calledWithExactly({ui: true, run}, {setEnvironment: setEnvironmentStub, loadOsInfo})).to.be.true;
399411
expect(runStub.calledOnce).to.be.true;
400412
expect(runStub.calledWithExactly({verbose: false, prompt: false, development: false, auto: false})).to.be.true;
401413
});
402414

403415
it('catches errors, passes them to ui error method, then exits', async function () {
416+
const run = sinon.stub().callsFake(fn => fn());
404417
const errorStub = sinon.stub();
405-
const uiStub = sinon.stub().returns({error: errorStub});
418+
const uiStub = sinon.stub().returns({error: errorStub, run});
419+
const loadOsInfo = sinon.stub().resolves();
406420
const setEnvironmentStub = sinon.stub();
407-
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub});
421+
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub, loadOsInfo});
408422

409423
const Command = proxyquire(modulePath, {
410424
'./ui': uiStub,
@@ -437,7 +451,9 @@ describe('Unit: Command', function () {
437451
expect(setEnvironmentStub.calledOnce).to.be.true;
438452
expect(setEnvironmentStub.calledWithExactly(false, true)).to.be.true;
439453
expect(systemStub.calledOnce).to.be.true;
440-
expect(systemStub.calledWithExactly({error: errorStub}, [{extensiona: true}])).to.be.true;
454+
expect(systemStub.calledWithExactly({error: errorStub, run}, [{extensiona: true}])).to.be.true;
455+
expect(run.calledOnce).to.be.true;
456+
expect(loadOsInfo.calledOnce).to.be.true;
441457
expect(runStub.calledOnce).to.be.true;
442458
expect(runStub.calledWithExactly({verbose: false, prompt: false, development: false, auto: false})).to.be.true;
443459
expect(errorStub.calledOnce).to.be.true;

test/unit/system-spec.js

+39-27
Original file line numberDiff line numberDiff line change
@@ -112,34 +112,20 @@ describe('Unit: System', function () {
112112
});
113113
});
114114

115-
describe('operatingSystem getter', function () {
116-
it('caches and returns the correct OS', function () {
117-
const getOsStub = sinon.stub().returns({
118-
os: 'Ubuntu',
119-
version: '16'
120-
});
121-
const System = proxyquire(modulePath, {
122-
'./utils/get-os': getOsStub
123-
});
115+
it('operatingSystem getter', function () {
116+
const System = require(modulePath);
117+
const sys = new System({}, []);
124118

125-
const instance = new System({}, []);
126-
const platformStub = sinon.stub(os, 'platform').returns('linux');
127-
const operatingSystem = instance.operatingSystem;
128-
129-
expect(platformStub.calledOnce).to.be.true;
130-
expect(getOsStub.calledOnce).to.be.true;
131-
expect(operatingSystem).to.be.an('object');
132-
expect(operatingSystem.os).to.equal('Ubuntu');
133-
expect(operatingSystem.version).to.equal('16');
134-
135-
// do the second call to see that it gets cached
136-
const newOperatingSystem = instance.operatingSystem;
137-
expect(newOperatingSystem).to.be.an('object');
138-
expect(newOperatingSystem.os).to.equal('Ubuntu');
139-
expect(newOperatingSystem.version).to.equal('16');
140-
expect(newOperatingSystem).to.deep.equal(operatingSystem);
141-
expect(getOsStub.calledOnce).to.be.true;
142-
});
119+
expect(sys.operatingSystem).to.deep.equal({});
120+
121+
sys._osInfo = {
122+
platform: 'Linux',
123+
distro: 'Ubuntu',
124+
release: '18.04 LTS',
125+
kernel: 'kernel'
126+
};
127+
128+
expect(sys.operatingSystem).to.deep.equal(sys._osInfo);
143129
});
144130

145131
describe('setEnvironment', function () {
@@ -578,6 +564,32 @@ describe('Unit: System', function () {
578564
expect(getInstanceStub.calledTwice).to.be.true;
579565
});
580566

567+
it('loadOsInfo returns if info already loaded', async function () {
568+
const osInfo = sinon.stub().rejects();
569+
const System = proxyquire(modulePath, {
570+
'systeminformation/lib/osinfo': {osInfo}
571+
});
572+
573+
const sys = new System({}, []);
574+
sys._osInfo = {distro: 'Mac OS X', release: '10.15'};
575+
576+
await sys.loadOsInfo();
577+
expect(osInfo.called).to.be.false;
578+
});
579+
580+
it('loadOsInfo loads operating system information', async function () {
581+
const osInfo = sinon.stub().resolves({distro: 'Windows', release: '10'});
582+
const System = proxyquire(modulePath, {
583+
'systeminformation/lib/osinfo': {osInfo}
584+
});
585+
586+
const sys = new System({}, []);
587+
588+
await sys.loadOsInfo();
589+
expect(osInfo.calledOnce).to.be.true;
590+
expect(sys._osInfo).to.deep.equal({distro: 'Windows', release: '10'});
591+
});
592+
581593
it('writeErrorLog works', function () {
582594
const ensureDirStub = sinon.stub();
583595
const writeFileStub = sinon.stub();

test/unit/ui/index-spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -945,8 +945,8 @@ describe('Unit: UI', function () {
945945
cliVersion: '0.9.1.8',
946946
environment: 'Earth',
947947
operatingSystem: {
948-
os: 'Ubuntu',
949-
version: '16'
948+
distro: 'Ubuntu',
949+
release: '16'
950950
},
951951
getInstance: () => ({version: null})
952952
};
@@ -970,8 +970,8 @@ describe('Unit: UI', function () {
970970
cliVersion: '0.9.1.8',
971971
environment: 'Earth',
972972
operatingSystem: {
973-
os: 'Ubuntu',
974-
version: '16'
973+
distro: 'Ubuntu',
974+
release: '16'
975975
},
976976
getInstance: () => ({version: '1.0.0'})
977977
};

test/unit/utils/get-os-spec.js

-95
This file was deleted.

0 commit comments

Comments
 (0)