Skip to content

Commit fb33312

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

File tree

5 files changed

+66
-19
lines changed

5 files changed

+66
-19
lines changed

packages/machine-id/README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const id = getMachineID({ raw: true });
2828
console.log('Original Machine ID:', id);
2929
```
3030

31-
3231
## Supported Platforms
3332

3433
- **macOS**: Uses the `IOPlatformUUID` from the `IOKit` framework (Supported on macOS 12.0 and later).
@@ -44,19 +43,19 @@ Here's a table of performance comparisons between the two libraries, based on th
4443
| Test | node-machine-id | @mongodb-js/machine-id | Improvement |
4544
| ----------- | --------------- | ---------------------- | ----------- |
4645
| **Mac** |
47-
| Raw | 10.71ms | 0.0072ms | 1494x |
48-
| Hashed | 12.42ms | 0.0176ms | 707x |
46+
| Raw | 10.71ms | 0.0072ms | 1494x |
47+
| Hashed | 12.42ms | 0.0176ms | 707x |
4948
| **Linux** |
50-
| Raw | 3.26ms | 0.0059ms | 557x |
51-
| Hashed | 3.25ms | 0.0088ms | 368x |
49+
| Raw | 3.26ms | 0.0059ms | 557x |
50+
| Hashed | 3.25ms | 0.0088ms | 368x |
5251
| **Windows** |
53-
| Raw | 45.36ms* | 0.0122ms | 3704x |
54-
| Hashed | 28.66ms* | 0.0272ms | 1053x |
55-
56-
\* - Windows tests may be inaccurate due to potential caching.
52+
| Raw | 45.36ms\* | 0.0122ms | 3704x |
53+
| Hashed | 28.66ms\* | 0.0272ms | 1053x |
5754

55+
\* - Windows tests may be inaccurate due to potential caching.
5856

5957
### Migrating from `node-machine-id`
58+
6059
If you were previously using `node-machine-id`, you can use the following mapping to get a result with the following hashing transformation. This is not guaranteed always to 1:1 match the output of `node-machine-id` for all cases. For example on Linux, it falls back to `/etc/machine-id` if `/var/lib/dbus/machine-id` is not available.
6160

6261
```ts
@@ -66,11 +65,14 @@ import { getMachineId } from '@mongodb-js/machine-id';
6665
function machineIdSync(original: boolean): string | undefined {
6766
const rawMachineId = getMachineId({ raw: true }).toLowerCase();
6867

69-
return original ? rawMachineId : createHash('sha256').update(rawMachineId).digest('hex');
68+
return original
69+
? rawMachineId
70+
: createHash('sha256').update(rawMachineId).digest('hex');
7071
}
7172
```
7273

7374
## Credits
75+
7476
Influenced by the work from [denisbrodbeck/machineid](https://github.com/denisbrodbeck/machineid) and [automation-stack/node-machine-id](https://github.com/automation-stack/node-machine-id).
7577

7678
## License

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)