Skip to content

Commit a1cb699

Browse files
sam-githubMylesBorins
authored andcommitted
test: getgroups() may contain duplicate GIDs
Some systems may have multiple group names with the same group ID, in which case getgroups() returns duplicate values, where `id -G` will filter the duplicates. Unique and sort the arrays so they can be compared. Backport-PR-URL: #12468 PR-URL: #10389 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent eb47897 commit a1cb699

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed
+12-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
4-
const exec = require('child_process').exec;
3+
4+
// Check `id -G` and `process.getgroups()` return same groups.
55

66
if (common.isOSX) {
77
common.skip('Output of `id -G` is unreliable on Darwin.');
88
return;
99
}
10+
const assert = require('assert');
11+
const exec = require('child_process').exec;
1012

1113
if (typeof process.getgroups === 'function') {
12-
const groups = process.getgroups();
14+
const groups = unique(process.getgroups());
1315
assert(Array.isArray(groups));
1416
assert(groups.length > 0);
1517
exec('id -G', function(err, stdout) {
16-
if (err) throw err;
17-
const real_groups = stdout.match(/\d+/g).map(Number);
18-
assert.strictEqual(groups.length, real_groups.length);
18+
assert.ifError(err);
19+
const real_groups = unique(stdout.match(/\d+/g).map(Number));
20+
assert.deepStrictEqual(groups, real_groups);
1921
check(groups, real_groups);
2022
check(real_groups, groups);
2123
});
@@ -24,3 +26,7 @@ if (typeof process.getgroups === 'function') {
2426
function check(a, b) {
2527
for (let i = 0; i < a.length; ++i) assert.notStrictEqual(b.indexOf(a[i]), -1);
2628
}
29+
30+
function unique(groups) {
31+
return [...new Set(groups)].sort();
32+
}

0 commit comments

Comments
 (0)