Skip to content

Commit c760fe0

Browse files
committed
feat: use github core platform helper instead of node:os
1 parent 54517cb commit c760fe0

File tree

5 files changed

+45
-152
lines changed

5 files changed

+45
-152
lines changed

lib/main/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/install.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os from 'os';
21
import { basename, dirname, join as joinPaths } from 'path';
32
import { readFile } from 'fs/promises';
43
import * as core from '@actions/core';
@@ -57,8 +56,8 @@ export default async function install() {
5756
installUpdates,
5857
} = gatherInputs();
5958
// we only support windows for now. But allow crazy people to skip this check if they like...
60-
if (!skipOsCheck && os.platform() !== 'win32') {
61-
throw new Error(`setup-sqlserver only supports Windows runners, got: ${os.platform()}`);
59+
if (!skipOsCheck && core.platform.platform !== 'win32') {
60+
throw new Error(`setup-sqlserver only supports Windows runners, got: ${core.platform.platform}`);
6261
}
6362
const osVersion = await getOsVersion();
6463
if (!VERSIONS.has(version)) {

src/utils.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,17 @@ import * as glob from '@actions/glob';
1717
*/
1818
export async function getOsVersion() {
1919
try {
20-
const systeminfo = await exec.getExecOutput('systeminfo', [], {
21-
silent: true,
22-
});
20+
const systeminfo = await core.platform.getDetails();
2321
// output the systeminfo in debug mode
2422
if (core.isDebug()) {
2523
core.startGroup('systeminfo');
26-
core.debug(systeminfo.stdout);
24+
core.debug(Object.entries(systeminfo).map((tuple) => tuple.join(': ')).join('\n'));
2725
core.endGroup();
2826
}
29-
// try to parse out the os name
30-
const matches = systeminfo.stdout.match(/os name:\s+(.*)/i);
31-
if (matches) {
32-
// parse the "version" (year)
33-
const version = matches[1].match(/([0-9]+)/);
34-
if (version) {
35-
return parseInt(version[1], 10);
36-
}
27+
// parse the "version" (year)
28+
const version = systeminfo.name.match(/([0-9]+)/);
29+
if (version) {
30+
return parseInt(version[1], 10);
3731
}
3832
} catch (e) {
3933
// don't throw errors, so the action can be as permissive as possible

test/install.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os from 'node:os';
21
import fs from 'fs/promises';
32
import * as core from '@actions/core';
43
import * as tc from '@actions/tool-cache';
@@ -16,7 +15,6 @@ use(sinonChai);
1615
describe('install', () => {
1716
let reverts: (() => void)[] = [];
1817
let versionStub: SinonStubbedInstance<Map<string, versions.VersionConfig>>;
19-
let osStub: SinonStubbedInstance<typeof os>;
2018
let coreStub: SinonStubbedInstance<typeof core>;
2119
let utilsStub: SinonStubbedInstance<typeof utils>;
2220
let tcStub: SinonStubbedInstance<typeof tc>;
@@ -82,12 +80,13 @@ describe('install', () => {
8280
utilsStub.downloadBoxInstaller.resolves('C:/tmp/box/setup.exe');
8381
utilsStub.downloadUpdateInstaller.resolves('C:/tmp/exe/sqlupdate.exe');
8482
utilsStub.waitForDatabase.resolves(0);
85-
osStub = stub(os);
86-
osStub.platform.returns('win32');
8783
coreStub = stub(core);
8884
coreStub.group.callsFake((message, cb) => {
8985
return cb();
9086
});
87+
stub(coreStub, 'platform').value({
88+
platform: 'win32',
89+
});
9190
tcStub = stub(tc);
9291
tcStub.find.returns('');
9392
execStub = stub(exec);
@@ -99,7 +98,9 @@ describe('install', () => {
9998
reverts = [];
10099
});
101100
it('fails if bad os', async () => {
102-
osStub.platform.returns('linux');
101+
stub(coreStub, 'platform').value({
102+
platform: 'linux',
103+
});
103104
try {
104105
await install();
105106
} catch (e) {

test/utils.ts

Lines changed: 28 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -14,137 +14,41 @@ import * as utils from '../src/utils';
1414
import * as crypto from '../src/crypto';
1515
use(sinonChai);
1616

17-
const windows2022 = `
18-
Host Name: fv-az618-92
19-
OS Name: Microsoft Windows Server 2022 Datacenter
20-
OS Version: 10.0.20348 N/A Build 20348
21-
OS Manufacturer: Microsoft Corporation
22-
OS Configuration: Standalone Server
23-
OS Build Type: Multiprocessor Free
24-
Registered Owner: N/A
25-
Registered Organization: N/A
26-
Product ID: 00454-60000-00001-AA926
27-
Original Install Date: 8/8/2023, 9:08:26 AM
28-
System Boot Time: 8/9/2023, 3:13:18 PM
29-
System Manufacturer: Microsoft Corporation
30-
System Model: Virtual Machine
31-
System Type: x64-based PC
32-
Processor(s): 1 Processor(s) Installed.
33-
[01]: Intel64 Family 6 Model 79 Stepping 1 GenuineIntel ~2295 Mhz
34-
BIOS Version: American Megatrends Inc. 090008 , 12/7/2018
35-
Windows Directory: C:\\Windows
36-
System Directory: C:\\Windows\\system32
37-
Boot Device: \\Device\\HarddiskVolume1
38-
System Locale: en-us;English (United States)
39-
Input Locale: en-us;English (United States)
40-
Time Zone: (UTC) Coordinated Universal Time
41-
Total Physical Memory: 7,168 MB
42-
Available Physical Memory: 5,426 MB
43-
Virtual Memory: Max Size: 8,959 MB
44-
Virtual Memory: Available: 7,373 MB
45-
Virtual Memory: In Use: 1,586 MB
46-
Page File Location(s): D:\\pagefile.sys
47-
Domain: WORKGROUP
48-
Logon Server: \\\\fv-az618-92
49-
Hotfix(s): 5 Hotfix(s) Installed.
50-
[01]: KB5028852
51-
[02]: KB5028858
52-
[03]: KB5011048
53-
[04]: KB5028171
54-
[05]: KB5028317
55-
Network Card(s): 1 NIC(s) Installed.
56-
[01]: Microsoft Hyper-V Network Adapter
57-
Connection Name: Ethernet
58-
DHCP Enabled: Yes
59-
DHCP Server: 168.63.129.16
60-
IP address(es)
61-
[01]: 10.1.0.141
62-
[02]: fe80::deb6:64f:8300:ef77
63-
Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed.
64-
`;
65-
66-
const windows2019 = `
67-
Host Name: fv-az276-691
68-
OS Name: Microsoft Windows Server 2019 Datacenter
69-
OS Version: 10.0.17763 N/A Build 17763
70-
OS Manufacturer: Microsoft Corporation
71-
OS Configuration: Standalone Server
72-
OS Build Type: Multiprocessor Free
73-
Registered Owner: N/A
74-
Registered Organization: N/A
75-
Product ID: 00430-00000-00000-AA138
76-
Original Install Date: 8/3/2023, 10:11:03 AM
77-
System Boot Time: 8/9/2023, 3:19:20 PM
78-
System Manufacturer: Microsoft Corporation
79-
System Model: Virtual Machine
80-
System Type: x64-based PC
81-
Processor(s): 1 Processor(s) Installed.
82-
[01]: Intel64 Family 6 Model 63 Stepping 2 GenuineIntel ~2394 Mhz
83-
BIOS Version: American Megatrends Inc. 090008 , 12/7/2018
84-
Windows Directory: C:\\Windows
85-
System Directory: C:\\Windows\\system32
86-
Boot Device: \\Device\\HarddiskVolume1
87-
System Locale: en-us;English (United States)
88-
Input Locale: en-us;English (United States)
89-
Time Zone: (UTC) Coordinated Universal Time
90-
Total Physical Memory: 7,168 MB
91-
Available Physical Memory: 5,543 MB
92-
Virtual Memory: Max Size: 8,959 MB
93-
Virtual Memory: Available: 7,426 MB
94-
Virtual Memory: In Use: 1,533 MB
95-
Page File Location(s): D:\\pagefile.sys
96-
Domain: WORKGROUP
97-
Logon Server: \\\\fv-az276-691
98-
Hotfix(s): 6 Hotfix(s) Installed.
99-
[01]: KB5028855
100-
[02]: KB4486153
101-
[03]: KB4589208
102-
[04]: KB5004424
103-
[05]: KB5028168
104-
[06]: KB5028316
105-
Network Card(s): 2 NIC(s) Installed.
106-
[01]: Hyper-V Virtual Ethernet Adapter
107-
Connection Name: vEthernet (nat)
108-
DHCP Enabled: No
109-
IP address(es)
110-
[01]: 172.27.224.1
111-
[02]: fe80::39cd:87ac:c9ad:cd5e
112-
[02]: Microsoft Hyper-V Network Adapter
113-
Connection Name: Ethernet 2
114-
DHCP Enabled: Yes
115-
DHCP Server: 168.63.129.16
116-
IP address(es)
117-
[01]: 10.1.36.0
118-
[02]: fe80::ae04:627e:a23b:d046
119-
Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed.
120-
`;
12117
describe('utils', () => {
12218
let coreStub: SinonStubbedInstance<typeof core>;
19+
let platformStub: SinonStubbedInstance<typeof core.platform>;
12320
beforeEach('stub core', () => {
21+
platformStub = stub(core.platform);
12422
coreStub = stub(core);
12523
});
12624
afterEach('restore stubs', () => {
12725
restore();
12826
});
12927
describe('.getOsVersion()', () => {
130-
let getExecOutput: SinonStubbedMember<typeof exec.getExecOutput>;
13128
beforeEach('stub debs', () => {
132-
getExecOutput = stub(exec, 'getExecOutput');
133-
getExecOutput.withArgs('systeminfo').resolves({
134-
exitCode: 0,
135-
stdout: windows2019,
136-
stderr: '',
29+
platformStub.getDetails.resolves({
30+
name: 'Microsoft Windows Server 2019 Datacenter',
31+
platform: 'win32',
32+
arch: 'x64',
33+
version: '10.0.17763',
34+
isWindows: true,
35+
isMacOS: false,
36+
isLinux: false,
13737
});
13838
});
13939
it('correctly returns for windows-2019', async () => {
14040
const out = await utils.getOsVersion();
14141
expect(out).to.equal(2019);
14242
});
14343
it('correctly returns for windows-2022', async () => {
144-
getExecOutput.withArgs('systeminfo').resolves({
145-
exitCode: 0,
146-
stdout: windows2022,
147-
stderr: '',
44+
platformStub.getDetails.resolves({
45+
name: 'Microsoft Windows Server 2022 Datacenter',
46+
platform: 'win32',
47+
arch: 'x64',
48+
version: '10.0.20348',
49+
isWindows: true,
50+
isMacOS: false,
51+
isLinux: false,
14852
});
14953
const out = await utils.getOsVersion();
15054
expect(out).to.equal(2022);
@@ -154,30 +58,25 @@ describe('utils', () => {
15458
await utils.getOsVersion();
15559
expect(coreStub.isDebug).to.have.callCount(1);
15660
expect(coreStub.startGroup).to.have.been.calledOnceWith('systeminfo');
157-
expect(coreStub.debug).to.have.been.calledOnceWith(windows2019);
61+
expect(coreStub.debug).to.have.been.calledOnceWith("name: Microsoft Windows Server 2019 Datacenter\nplatform: win32\narch: x64\nversion: 10.0.17763\nisWindows: true\nisMacOS: false\nisLinux: false");
15862
expect(coreStub.endGroup).to.have.callCount(1);
15963
});
16064
it('fails gracefully when error is thrown', async () => {
16165
const err = new Error('synthetic error');
162-
getExecOutput.withArgs('systeminfo').rejects(err);
66+
platformStub.getDetails.rejects(err);
16367
const res = await utils.getOsVersion();
16468
expect(res).to.equal(null);
16569
expect(coreStub.warning).to.have.been.calledOnceWithExactly(err);
16670
});
16771
it('fails gracefully with bad output', async () => {
168-
getExecOutput.withArgs('systeminfo').resolves({
169-
exitCode: 0,
170-
stdout: 'os name: not a number',
171-
stderr: '',
172-
});
173-
const res = await utils.getOsVersion();
174-
expect(res).to.equal(null);
175-
});
176-
it('fails gracefully with no output', async () => {
177-
getExecOutput.withArgs('systeminfo').resolves({
178-
exitCode: 0,
179-
stdout: '',
180-
stderr: '',
72+
platformStub.getDetails.resolves({
73+
name: 'not a number',
74+
platform: 'win32',
75+
arch: 'x64',
76+
version: '10.0.20348',
77+
isWindows: true,
78+
isMacOS: false,
79+
isLinux: false,
18180
});
18281
const res = await utils.getOsVersion();
18382
expect(res).to.equal(null);

0 commit comments

Comments
 (0)