Skip to content

Commit b8e5a65

Browse files
committed
feat: add testing coverage and corner case tests
1 parent 723f5c3 commit b8e5a65

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

packages/machine-id/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"bootstrap": "npm run compile",
1010
"pretest": "npm run compile",
1111
"test": "mocha",
12-
"test-ci": "npm run test",
12+
"test-cov": "nyc -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test",
13+
"test-ci": "npm run test-cov",
1314
"lint": "eslint . && prettier --check .",
1415
"check": "npm run lint && npm run test",
1516
"prepublishOnly": "npm run compile",
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
#!/usr/bin/env node
22
import { getMachineId } from '..';
33

4-
// Check if --raw flag is provided
5-
const rawFlag = process.argv.includes('--raw');
6-
7-
// Get the machine ID, passing the raw option if requested
8-
const id =
9-
getMachineId({ raw: rawFlag }) || 'Machine ID not available on this platform';
4+
const id = getMachineId({ raw: process.argv.includes('--raw') }) || '';
105

116
// eslint-disable-next-line no-console
127
console.log(id);

packages/machine-id/src/index.spec.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
/* eslint-disable no-console */
22
import { getMachineId } from '.';
33
import { machineIdSync as otherMachineId } from 'node-machine-id';
4-
import { expect } from 'chai';
4+
import chai, { expect } from 'chai';
55
import { createHash } from 'crypto';
6+
import sinonChai from 'sinon-chai';
7+
import sinon from 'sinon';
8+
import bindings from 'bindings';
9+
10+
chai.use(sinonChai);
611

712
describe('machine-id', function () {
813
this.timeout(5_000);
@@ -53,6 +58,43 @@ describe('machine-id', function () {
5358
const hashRegex = /^[0-9a-f]{64}$/i;
5459

5560
expect(hashRegex.test(id));
61+
62+
expect(id).equals(
63+
createHash('sha256')
64+
.update(getMachineId({ raw: true }) || '')
65+
.digest('hex'),
66+
);
67+
});
68+
});
69+
70+
describe('edge cases', function () {
71+
afterEach(function () {
72+
sinon.restore();
73+
});
74+
75+
describe('returns undefined', function () {
76+
it('if something goes wrong with the binding function', function () {
77+
sinon
78+
.stub(bindings('machine_id'), 'getMachineId')
79+
.throws(new Error('Binding error'));
80+
81+
expect(getMachineId({ raw: true })).to.be.undefined;
82+
expect(getMachineId()).to.be.undefined;
83+
});
84+
85+
it('if the binding function returns an empty string', function () {
86+
sinon.stub(bindings('machine_id'), 'getMachineId').returns('');
87+
88+
expect(getMachineId({ raw: true })).to.be.undefined;
89+
expect(getMachineId()).to.be.undefined;
90+
});
91+
92+
it('if the binding function returns undefined', function () {
93+
sinon.stub(bindings('machine_id'), 'getMachineId').returns(undefined);
94+
95+
expect(getMachineId({ raw: true })).to.be.undefined;
96+
expect(getMachineId()).to.be.undefined;
97+
});
5698
});
5799
});
58100
});

packages/machine-id/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ export type GetMachineIdOptions = {
1515
export function getMachineId({ raw = false }: GetMachineIdOptions = {}):
1616
| string
1717
| undefined {
18-
const machineId: string | undefined = binding.getMachineId();
18+
let machineId: string | undefined;
19+
20+
try {
21+
machineId = binding.getMachineId();
22+
} catch {
23+
// If the binding fails, we can assume the machine ID is not available.
24+
return undefined;
25+
}
1926

2027
if (!machineId) {
2128
return undefined;

0 commit comments

Comments
 (0)