Skip to content

Commit

Permalink
feat(system): use systeminformation for os version information
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine committed Nov 8, 2019
1 parent c7875c9 commit 1fc845d
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 189 deletions.
3 changes: 3 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class Command {
}

try {
debug('loading operating system information');
await ui.run(() => system.loadOsInfo(), 'Inspecting operating system', {clear: true});

if (this.runPreChecks) {
const preChecks = require('./utils/pre-checks');
await preChecks(ui, system);
Expand Down
18 changes: 10 additions & 8 deletions lib/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,7 @@ class System {
* @public
*/
get operatingSystem() {
if (!this._operatingSystem) {
const getOS = require('./utils/get-os');
this._operatingSystem = getOS(this.platform);

return this._operatingSystem;
}

return this._operatingSystem;
return this._osInfo || {};
}

/**
Expand Down Expand Up @@ -346,6 +339,15 @@ class System {
return available;
}

async loadOsInfo() {
if (this._osInfo) {
return;
}

const {osInfo} = require('systeminformation/lib/osinfo');
this._osInfo = await osInfo();
}

/**
* Writes an error message to a global log file.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ class UI {
const ghostVersion = version ? ` Ghost Version: ${version}\n` : '';

return 'Debug Information:\n' +
` OS: ${system.operatingSystem.os}, v${system.operatingSystem.version}\n` +
` OS: ${system.operatingSystem.distro}, v${system.operatingSystem.release}\n` +
` Node Version: ${process.version}\n` +
ghostVersion +
` Ghost-CLI Version: ${system.cliVersion}\n` +
Expand Down
41 changes: 0 additions & 41 deletions lib/utils/get-os.js

This file was deleted.

42 changes: 29 additions & 13 deletions test/unit/command-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,11 @@ describe('Unit: Command', function () {
});

it('loads system and ui dependencies, calls run method', async function () {
const uiStub = sinon.stub().returns({ui: true});
const run = sinon.stub().callsFake(fn => fn());
const uiStub = sinon.stub().returns({ui: true, run});
const setEnvironmentStub = sinon.stub();
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub});
const loadOsInfo = sinon.stub().resolves();
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub, loadOsInfo});

const Command = proxyquire(modulePath, {
'./ui': uiStub,
Expand All @@ -312,15 +314,19 @@ describe('Unit: Command', function () {
expect(setEnvironmentStub.calledOnce).to.be.true;
expect(setEnvironmentStub.calledWithExactly(true, true)).to.be.true;
expect(systemStub.calledOnce).to.be.true;
expect(systemStub.calledWithExactly({ui: true}, [{extensiona: true}])).to.be.true;
expect(systemStub.calledWithExactly({ui: true, run}, [{extensiona: true}])).to.be.true;
expect(run.calledOnce).to.be.true;
expect(loadOsInfo.calledOnce).to.be.true;
expect(runStub.calledOnce).to.be.true;
expect(runStub.calledWithExactly({verbose: true, prompt: true, development: true, auto: false})).to.be.true;
});

it('binds cleanup handler if cleanup method is defined', async function () {
const uiStub = sinon.stub().returns({ui: true});
const run = sinon.stub().callsFake(fn => fn());
const uiStub = sinon.stub().returns({ui: true, run});
const setEnvironmentStub = sinon.stub();
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub});
const loadOsInfo = sinon.stub().resolves();
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub, loadOsInfo});

const Command = proxyquire(modulePath, {
'./ui': uiStub,
Expand Down Expand Up @@ -351,7 +357,9 @@ describe('Unit: Command', function () {
expect(setEnvironmentStub.calledOnce).to.be.true;
expect(setEnvironmentStub.calledWithExactly(true, true)).to.be.true;
expect(systemStub.calledOnce).to.be.true;
expect(systemStub.calledWithExactly({ui: true}, [{extensiona: true}])).to.be.true;
expect(systemStub.calledWithExactly({ui: true, run}, [{extensiona: true}])).to.be.true;
expect(run.calledOnce).to.be.true;
expect(loadOsInfo.calledOnce).to.be.true;
expect(runStub.calledOnce).to.be.true;
expect(runStub.calledWithExactly({verbose: false, prompt: false, development: false, auto: true})).to.be.true;
expect(onStub.calledTwice).to.be.true;
Expand All @@ -360,9 +368,11 @@ describe('Unit: Command', function () {
});

it('runs updateCheck if checkVersion property is true on command class', async function () {
const uiStub = sinon.stub().returns({ui: true});
const run = sinon.stub().callsFake(fn => fn());
const uiStub = sinon.stub().returns({ui: true, run});
const setEnvironmentStub = sinon.stub();
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub});
const loadOsInfo = sinon.stub().resolves();
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub, loadOsInfo});
const preChecksStub = sinon.stub().resolves();

const Command = proxyquire(modulePath, {
Expand Down Expand Up @@ -393,18 +403,22 @@ describe('Unit: Command', function () {
expect(setEnvironmentStub.calledOnce).to.be.true;
expect(setEnvironmentStub.calledWithExactly(true, true)).to.be.true;
expect(systemStub.calledOnce).to.be.true;
expect(systemStub.calledWithExactly({ui: true}, [{extensiona: true}])).to.be.true;
expect(systemStub.calledWithExactly({ui: true, run}, [{extensiona: true}])).to.be.true;
expect(run.calledOnce).to.be.true;
expect(loadOsInfo.calledOnce).to.be.true;
expect(preChecksStub.calledOnce).to.be.true;
expect(preChecksStub.calledWithExactly({ui: true}, {setEnvironment: setEnvironmentStub})).to.be.true;
expect(preChecksStub.calledWithExactly({ui: true, run}, {setEnvironment: setEnvironmentStub, loadOsInfo})).to.be.true;
expect(runStub.calledOnce).to.be.true;
expect(runStub.calledWithExactly({verbose: false, prompt: false, development: false, auto: false})).to.be.true;
});

it('catches errors, passes them to ui error method, then exits', async function () {
const run = sinon.stub().callsFake(fn => fn());
const errorStub = sinon.stub();
const uiStub = sinon.stub().returns({error: errorStub});
const uiStub = sinon.stub().returns({error: errorStub, run});
const loadOsInfo = sinon.stub().resolves();
const setEnvironmentStub = sinon.stub();
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub});
const systemStub = sinon.stub().returns({setEnvironment: setEnvironmentStub, loadOsInfo});

const Command = proxyquire(modulePath, {
'./ui': uiStub,
Expand Down Expand Up @@ -437,7 +451,9 @@ describe('Unit: Command', function () {
expect(setEnvironmentStub.calledOnce).to.be.true;
expect(setEnvironmentStub.calledWithExactly(false, true)).to.be.true;
expect(systemStub.calledOnce).to.be.true;
expect(systemStub.calledWithExactly({error: errorStub}, [{extensiona: true}])).to.be.true;
expect(systemStub.calledWithExactly({error: errorStub, run}, [{extensiona: true}])).to.be.true;
expect(run.calledOnce).to.be.true;
expect(loadOsInfo.calledOnce).to.be.true;
expect(runStub.calledOnce).to.be.true;
expect(runStub.calledWithExactly({verbose: false, prompt: false, development: false, auto: false})).to.be.true;
expect(errorStub.calledOnce).to.be.true;
Expand Down
66 changes: 39 additions & 27 deletions test/unit/system-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,34 +112,20 @@ describe('Unit: System', function () {
});
});

describe('operatingSystem getter', function () {
it('caches and returns the correct OS', function () {
const getOsStub = sinon.stub().returns({
os: 'Ubuntu',
version: '16'
});
const System = proxyquire(modulePath, {
'./utils/get-os': getOsStub
});
it('operatingSystem getter', function () {
const System = require(modulePath);
const sys = new System({}, []);

const instance = new System({}, []);
const platformStub = sinon.stub(os, 'platform').returns('linux');
const operatingSystem = instance.operatingSystem;

expect(platformStub.calledOnce).to.be.true;
expect(getOsStub.calledOnce).to.be.true;
expect(operatingSystem).to.be.an('object');
expect(operatingSystem.os).to.equal('Ubuntu');
expect(operatingSystem.version).to.equal('16');

// do the second call to see that it gets cached
const newOperatingSystem = instance.operatingSystem;
expect(newOperatingSystem).to.be.an('object');
expect(newOperatingSystem.os).to.equal('Ubuntu');
expect(newOperatingSystem.version).to.equal('16');
expect(newOperatingSystem).to.deep.equal(operatingSystem);
expect(getOsStub.calledOnce).to.be.true;
});
expect(sys.operatingSystem).to.deep.equal({});

sys._osInfo = {
platform: 'Linux',
distro: 'Ubuntu',
release: '18.04 LTS',
kernel: 'kernel'
};

expect(sys.operatingSystem).to.deep.equal(sys._osInfo);
});

describe('setEnvironment', function () {
Expand Down Expand Up @@ -578,6 +564,32 @@ describe('Unit: System', function () {
expect(getInstanceStub.calledTwice).to.be.true;
});

it('loadOsInfo returns if info already loaded', async function () {
const osInfo = sinon.stub().rejects();
const System = proxyquire(modulePath, {
'systeminformation/lib/osinfo': {osInfo}
});

const sys = new System({}, []);
sys._osInfo = {distro: 'Mac OS X', release: '10.15'};

await sys.loadOsInfo();
expect(osInfo.called).to.be.false;
});

it('loadOsInfo loads operating system information', async function () {
const osInfo = sinon.stub().resolves({distro: 'Windows', release: '10'});
const System = proxyquire(modulePath, {
'systeminformation/lib/osinfo': {osInfo}
});

const sys = new System({}, []);

await sys.loadOsInfo();
expect(osInfo.calledOnce).to.be.true;
expect(sys._osInfo).to.deep.equal({distro: 'Windows', release: '10'});
});

it('writeErrorLog works', function () {
const ensureDirStub = sinon.stub();
const writeFileStub = sinon.stub();
Expand Down
8 changes: 4 additions & 4 deletions test/unit/ui/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,8 +945,8 @@ describe('Unit: UI', function () {
cliVersion: '0.9.1.8',
environment: 'Earth',
operatingSystem: {
os: 'Ubuntu',
version: '16'
distro: 'Ubuntu',
release: '16'
},
getInstance: () => ({version: null})
};
Expand All @@ -970,8 +970,8 @@ describe('Unit: UI', function () {
cliVersion: '0.9.1.8',
environment: 'Earth',
operatingSystem: {
os: 'Ubuntu',
version: '16'
distro: 'Ubuntu',
release: '16'
},
getInstance: () => ({version: '1.0.0'})
};
Expand Down
95 changes: 0 additions & 95 deletions test/unit/utils/get-os-spec.js

This file was deleted.

0 comments on commit 1fc845d

Please sign in to comment.