@@ -2,16 +2,11 @@ import { expect } from 'chai';
2
2
import os from 'os' ;
3
3
import { promises as fs } from 'fs' ;
4
4
5
- import { getOsInfo } from './get-os-info' ;
5
+ import { getOsInfo , parseDarwinInfo , parseLinuxInfo } from './get-os-info' ;
6
6
7
7
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 ( ) ;
15
10
expect ( { os_arch, os_type, os_version, os_release } ) . to . deep . equal ( {
16
11
os_arch : os . arch ( ) ,
17
12
os_type : os . type ( ) ,
@@ -21,13 +16,31 @@ describe('get-os-info', function () {
21
16
} ) ;
22
17
23
18
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 ( ) {
25
40
if ( process . platform !== 'linux' ) {
26
41
this . skip ( ) ;
27
42
}
28
- } ) ;
29
43
30
- it ( 'returns info from /etc/releases' , async function ( ) {
31
44
const etcRelease = await fs . readFile ( '/etc/os-release' , 'utf-8' ) ;
32
45
33
46
const releaseKv = etcRelease
@@ -47,11 +60,66 @@ describe('get-os-info', function () {
47
60
expect ( distroId ) . to . match ( / ^ ( r h e l | u b u n t u | d e b i a n ) $ / ) ;
48
61
expect ( distroVer ) . to . match ( / ^ \d + / ) ;
49
62
50
- const { os_linux_dist, os_linux_release } = osInfo ;
63
+ const { os_linux_dist, os_linux_release } = await getOsInfo ( ) ;
51
64
expect ( { os_linux_dist, os_linux_release } ) . to . deep . equal ( {
52
65
os_linux_dist : distroId ,
53
66
os_linux_release : distroVer ,
54
67
} ) ;
55
68
} ) ;
56
69
} ) ;
70
+
71
+ describe ( 'on darwin' , function ( ) {
72
+ it ( 'parses the SystemVersion.plist file' , function ( ) {
73
+ const fixture = `
74
+ <?xml version="1.0" encoding="UTF-8"?>
75
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
76
+ <plist version="1.0">
77
+ <dict>
78
+ <key>BuildID</key>
79
+ <string>2B3829A8-E319-11EF-8892-025514DE0AB1</string>
80
+ <key>ProductBuildVersion</key>
81
+ <string>24D70</string>
82
+ <key>ProductCopyright</key>
83
+ <string>1983-2025 Apple Inc.</string>
84
+ <key>ProductName</key>
85
+ <string>macOS</string>
86
+ <key>ProductUserVisibleVersion</key>
87
+ <string>15.3.1</string>
88
+ <key>ProductVersion</key>
89
+ <string>15.3.1</string>
90
+ <key>iOSSupportVersion</key>
91
+ <string>18.3</string>
92
+ </dict>
93
+ </plist>
94
+ ` ;
95
+
96
+ expect ( parseDarwinInfo ( fixture ) ) . to . deep . equal ( {
97
+ os_darwin_product_name : 'macOS' ,
98
+ os_darwin_product_version : '15.3.1' ,
99
+ os_darwin_product_build_version : '24D70' ,
100
+ } ) ;
101
+ } ) ;
102
+
103
+ it ( 'returns info from /System/Library/CoreServices/SystemVersion.plist' , async function ( ) {
104
+ if ( process . platform !== 'darwin' ) {
105
+ this . skip ( ) ;
106
+ }
107
+
108
+ const systemVersionPlist = await fs . readFile (
109
+ '/System/Library/CoreServices/SystemVersion.plist' ,
110
+ 'utf-8'
111
+ ) ;
112
+
113
+ const {
114
+ os_darwin_product_name,
115
+ os_darwin_product_version,
116
+ os_darwin_product_build_version,
117
+ } = await getOsInfo ( ) ;
118
+
119
+ // Instead of reimplementing the parser, we simply check that the values are present in the original file
120
+ expect ( systemVersionPlist ) . contains ( os_darwin_product_name ) ;
121
+ expect ( systemVersionPlist ) . contains ( os_darwin_product_version ) ;
122
+ expect ( systemVersionPlist ) . contains ( os_darwin_product_build_version ) ;
123
+ } ) ;
124
+ } ) ;
57
125
} ) ;
0 commit comments