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 <jasnell@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent eb47897 commit a1cb699

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
'use strict';
22
constcommon=require('../common');
3-
constassert=require('assert');
4-
constexec=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+
constassert=require('assert');
11+
constexec=require('child_process').exec;
1012

1113
if(typeofprocess.getgroups==='function'){
12-
constgroups=process.getgroups();
14+
constgroups=unique(process.getgroups());
1315
assert(Array.isArray(groups));
1416
assert(groups.length>0);
1517
exec('id -G',function(err,stdout){
16-
if(err)throwerr;
17-
constreal_groups=stdout.match(/\d+/g).map(Number);
18-
assert.strictEqual(groups.length,real_groups.length);
18+
assert.ifError(err);
19+
constreal_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
functioncheck(a,b){
2527
for(leti=0;i<a.length;++i)assert.notStrictEqual(b.indexOf(a[i]),-1);
2628
}
29+
30+
functionunique(groups){
31+
return[...newSet(groups)].sort();
32+
}

0 commit comments

Comments
 (0)