Skip to content

Commit 2e5d4fe

Browse files
committed
Refactor linux implementation and tests
1 parent ea915a1 commit 2e5d4fe

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

packages/get-os-info/src/get-os-info.spec.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@ import { expect } from 'chai';
22
import os from 'os';
33
import { promises as fs } from 'fs';
44

5-
import { getOsInfo } from './get-os-info';
5+
import { getOsInfo, parseDarwinInfo, parseLinuxInfo } from './get-os-info';
66

77
describe('get-os-info', function () {
8-
let osInfo;
9-
beforeEach(async function () {
10-
osInfo = await getOsInfo();
11-
});
12-
13-
it('returns info from "os" module', function () {
14-
const { os_arch, os_type, os_version, os_release } = osInfo;
8+
it('returns info from "os" module', async function () {
9+
const { os_arch, os_type, os_version, os_release } = await getOsInfo();
1510
expect({ os_arch, os_type, os_version, os_release }).to.deep.equal({
1611
os_arch: os.arch(),
1712
os_type: os.type(),
@@ -21,13 +16,31 @@ describe('get-os-info', function () {
2116
});
2217

2318
describe('on linux', function () {
24-
beforeEach(function () {
19+
it('parses os-release file', function () {
20+
// Copied from https://manpages.ubuntu.com/manpages/focal/man5/os-release.5.html#example
21+
const fixture = `
22+
NAME=Fedora
23+
VERSION="17 (Beefy Miracle)"
24+
ID=fedora
25+
VERSION_ID=17
26+
PRETTY_NAME="Fedora 17 (Beefy Miracle)"
27+
ANSI_COLOR="0;34"
28+
CPE_NAME="cpe:/o:fedoraproject:fedora:17"
29+
HOME_URL="https://fedoraproject.org/"
30+
BUG_REPORT_URL="https://bugzilla.redhat.com/"
31+
`;
32+
33+
expect(parseLinuxInfo(fixture)).to.deep.equal({
34+
os_linux_dist: 'fedora',
35+
os_linux_release: '17',
36+
});
37+
});
38+
39+
it('returns info from /etc/releases', async function () {
2540
if (process.platform !== 'linux') {
2641
this.skip();
2742
}
28-
});
2943

30-
it('returns info from /etc/releases', async function () {
3144
const etcRelease = await fs.readFile('/etc/os-release', 'utf-8');
3245

3346
const releaseKv = etcRelease

packages/get-os-info/src/get-os-info.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
11
import os from 'os';
22
import { promises as fs } from 'fs';
33

4+
type LinuxInfo = {
5+
os_linux_dist: string;
6+
os_linux_release: string;
7+
};
8+
49
type OsInfo = {
510
os_type: string;
611
os_version: string;
712
os_arch: string;
813
os_release: string;
9-
os_linux_dist?: string;
10-
os_linux_release?: string;
11-
};
14+
} & Partial<LinuxInfo>;
1215

13-
async function getLinuxInfo(): Promise<{
14-
os_linux_dist: string;
15-
os_linux_release: string;
16-
}> {
16+
async function getLinuxInfo(): Promise<LinuxInfo> {
1717
try {
1818
const releaseFile = '/etc/os-release';
1919
const etcRelease = await fs.readFile(releaseFile, 'utf-8');
20-
21-
const osReleaseEntries = etcRelease
22-
.split('\n')
23-
.map((l) => l.trim())
24-
.filter(Boolean)
25-
.map((l) => l.split('='))
26-
.map(([k, v]) => [
27-
k,
28-
(v || '').replace(/^["']/, '').replace(/["']$/, ''),
29-
]);
30-
31-
const osReleaseKv = Object.fromEntries(osReleaseEntries);
32-
20+
return parseLinuxInfo(etcRelease);
21+
} catch (e) {
3322
return {
34-
os_linux_dist: osReleaseKv.ID || 'unknown',
35-
os_linux_release: osReleaseKv.VERSION_ID || 'unknown',
23+
os_linux_dist: 'unknown',
24+
os_linux_release: 'unknown',
3625
};
37-
} catch (e) {
38-
// couldn't read /etc/os-release
3926
}
27+
}
28+
29+
export function parseLinuxInfo(etcRelease: string): LinuxInfo {
30+
const osReleaseEntries = etcRelease
31+
.split('\n')
32+
.map((l) => l.trim())
33+
.filter(Boolean)
34+
.map((l) => l.split('='))
35+
.map(([k, v]) => [k, (v || '').replace(/^["']/, '').replace(/["']$/, '')]);
36+
37+
const osReleaseKv = Object.fromEntries(osReleaseEntries);
4038

4139
return {
42-
os_linux_dist: 'unknown',
43-
os_linux_release: 'unknown',
40+
os_linux_dist: osReleaseKv.ID || 'unknown',
41+
os_linux_release: osReleaseKv.VERSION_ID || 'unknown',
4442
};
4543
}
4644

0 commit comments

Comments
 (0)