Skip to content

Commit 1932061

Browse files
committed
Fix tests.
1 parent 9beacf6 commit 1932061

File tree

1 file changed

+54
-15
lines changed

1 file changed

+54
-15
lines changed

test/test.js

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,49 @@ let osData = {
99
hostname: os.hostname(),
1010
arch: os.arch(),
1111
release: os.release(),
12+
file: undefined,
1213
}
1314

14-
let osRelease = fs.readFileSync('/etc/os-release', 'utf8')
15-
let lines = osRelease.split('\n')
16-
17-
// use different logic to determine the KV pairs
18-
let osKVPairs = {}
19-
lines.forEach(line => {
20-
let m = line.match(/(.+)=("{0,1})(.*)\2/)
21-
if (m) {
22-
osKVPairs[m[1].toLowerCase()] = m[3]
15+
const paths = ['/etc/os-release', '/usr/lib/os-release', '/etc/alpine-release']
16+
17+
let osRelease
18+
for (let i = 0; i < paths.length; i++) {
19+
try {
20+
osRelease = fs.readFileSync(paths[i], 'utf8')
21+
if (osRelease) {
22+
osData.file = paths[i]
23+
break
24+
}
25+
} catch (e) {
26+
console.log('could not read', paths[i])
2327
}
24-
})
25-
let expected = Object.assign({}, osData, osKVPairs)
28+
}
29+
let expected
30+
31+
if (osData.file === '/etc/alpine-release') {
32+
osData.name = 'Alpine'
33+
osData.id = 'alpine'
34+
osData.version = osRelease
35+
osData.version_id = osRelease
36+
expected = osData
37+
} else {
38+
let lines = osRelease.split('\n')
39+
40+
// use different logic to determine the KV pairs
41+
let osKVPairs = {}
42+
lines.forEach(line => {
43+
let m = line.match(/(.+)=("{0,1})(.*)\2/)
44+
if (m) {
45+
osKVPairs[m[1].toLowerCase()] = m[3]
46+
}
47+
})
48+
expected = Object.assign({}, osData, osKVPairs)
49+
}
2650

2751
//
2852
// run the tests
2953
//
30-
describe('os-release-info', function () {
54+
describe('linux-os-info', function () {
3155
it('should work by returning a promise', function (done) {
3256
let p = osInfo()
3357
p.should.be.instanceOf(Promise)
@@ -42,17 +66,32 @@ describe('os-release-info', function () {
4266
})
4367

4468
it('should work with a callback', function (done) {
45-
osInfo(function (err, info) {
69+
osInfo({mode: function (err, info) {
4670
compare(info, expected)
4771
done()
48-
})
72+
}})
4973
})
5074

5175
it('should work synchronously', function () {
52-
let info = osInfo({synchronous: true})
76+
let info = osInfo({mode: 'sync'})
5377
compare(info, expected)
5478
})
5579

80+
it('should return os info when no release file is found', function () {
81+
let info = osInfo({mode: 'sync', list: []})
82+
let e = new Error('linux-os-info - no file found')
83+
let expected = Object.assign({}, osData, {file: e})
84+
compare(info, expected)
85+
86+
osInfo({mode: function (err, info) {
87+
compare(info, expected)
88+
}, list: []})
89+
90+
osInfo({list: []}).then(info => {
91+
compare(info, expected)
92+
})
93+
})
94+
5695
describe('OS-specific tests', function () {
5796
let info = osInfo({synchronous: true})
5897

0 commit comments

Comments
 (0)