-
-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(system): add os and version to System class (#602)
no issue - added a new getter to the System class, that returns the operating system as well as the version - works for linux, mac, and windows. Everything else falls back to the native `os.platform()` fn. - prints the os and the version in the debug information - added tests - updated issue template
- Loading branch information
Showing
8 changed files
with
171 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const os = require('os'); | ||
const execa = require('execa'); | ||
|
||
module.exports = function getOS(platform) { | ||
const osInfo = { | ||
os: os.platform(), | ||
version: os.release() | ||
}; | ||
|
||
if (platform.linux) { | ||
try { | ||
osInfo.os = execa.shellSync('lsb_release -i -s').stdout; | ||
osInfo.version = execa.shellSync('lsb_release -r -s').stdout; | ||
} catch (e) { | ||
return osInfo; | ||
} | ||
} else if (platform.macos) { | ||
// Darwin is Mac OS, use `sw_vers` | ||
try { | ||
osInfo.os = execa.shellSync('sw_vers -productName').stdout; | ||
osInfo.version = execa.shellSync('sw_vers -productVersion').stdout; | ||
} catch (e) { | ||
return osInfo; | ||
} | ||
} else if (platform.windows) { | ||
// for windows run `ver` | ||
// should output something like this: Microsoft Windows XP [Version 5.1.2600] | ||
try { | ||
const winOutput = execa.shellSync('ver').stdout.split(/\[/i); | ||
|
||
osInfo.os = winOutput[0].trim(); | ||
osInfo.version = /[0-9]+\.[0-9]+\.[0-9]+/.exec(winOutput[1])[0]; | ||
} catch (e) { | ||
return osInfo; | ||
} | ||
} | ||
|
||
return osInfo; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
const expect = require('chai').expect; | ||
const sinon = require('sinon'); | ||
const execa = require('execa'); | ||
const os = require('os'); | ||
const getOS = require('../../../lib/utils/get-os'); | ||
|
||
describe('Unit: Utils > getOS', function () { | ||
const sandbox = sinon.sandbox.create(); | ||
let platformStub, versionStub, execaStub; | ||
|
||
beforeEach(function () { | ||
versionStub = sandbox.stub(os, 'release'); | ||
execaStub = sandbox.stub(execa, 'shellSync'); | ||
platformStub = sandbox.stub(os, 'platform'); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('and returns correct Linux OS', function () { | ||
platformStub.returns('linux'); | ||
execaStub.withArgs('lsb_release -i -s').returns({stdout: 'Ubuntu'}); | ||
execaStub.withArgs('lsb_release -r -s').returns({stdout: '16'}); | ||
|
||
const osResult = getOS({linux: true}); | ||
expect(osResult.os).to.equal('Ubuntu'); | ||
expect(osResult.version).to.equal('16'); | ||
expect(execaStub.calledTwice).to.be.true; | ||
}); | ||
|
||
it('and returns correct mac OS', function () { | ||
platformStub.returns('darwin'); | ||
execaStub.withArgs('sw_vers -productName').returns({stdout: 'Mac OS X'}); | ||
execaStub.withArgs('sw_vers -productVersion').returns({stdout: '10.13.3'}); | ||
|
||
const osResult = getOS({macos: true}); | ||
expect(osResult.os).to.equal('Mac OS X'); | ||
expect(osResult.version).to.equal('10.13.3'); | ||
expect(execaStub.calledTwice).to.be.true; | ||
}); | ||
|
||
it('and returns correct Windows OS', function () { | ||
platformStub.returns('win32'); | ||
execaStub.withArgs('ver').returns({stdout: 'Microsoft Windows XP [Version 5.1.2600]'}); | ||
|
||
const osResult = getOS({windows: true}); | ||
expect(osResult.os).to.equal('Microsoft Windows XP'); | ||
expect(osResult.version).to.equal('5.1.2600'); | ||
expect(execaStub.calledOnce).to.be.true; | ||
}); | ||
|
||
it('and returns default os.platform if OS is not Mac, Linux, or Windows', function () { | ||
platformStub.returns('freebsd'); | ||
versionStub.returns('1.0.0') | ||
const osResult = getOS({ | ||
linux: false, | ||
macos: false, | ||
windows: false | ||
}); | ||
expect(osResult.os).to.equal('freebsd'); | ||
expect(osResult.version).to.equal('1.0.0'); | ||
expect(execaStub.calledOnce).to.be.false; | ||
}); | ||
}); |