Skip to content

Commit

Permalink
Reveal detection method and polyfill spawnSync (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreprocess authored and lovell committed Oct 28, 2017
1 parent 2507554 commit 01fdf51
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
16 changes: 15 additions & 1 deletion lib/detect-libc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ var spawnOptions = {
env: process.env
};

if (!spawnSync) {
spawnSync = function () {
return { status: 126, stdout: '', stderr: '' };
};
}

function contains (needle) {
return function (haystack) {
return haystack.indexOf(needle) !== -1;
Expand All @@ -24,32 +30,39 @@ function versionFromMuslLdd (out) {

var family = '';
var version = '';
var method = '';

if (spawnSync && platform === 'linux') {
if (platform === 'linux') {
// Try getconf
var glibc = spawnSync('getconf', ['GNU_LIBC_VERSION'], spawnOptions);
if (glibc.status === 0) {
family = GLIBC;
version = glibc.stdout.trim().split(' ')[1];
method = 'getconf';
} else {
// Try ldd
var ldd = spawnSync('ldd', ['--version'], spawnOptions);
if (ldd.status === 0 && ldd.stdout.indexOf(MUSL) !== -1) {
family = MUSL;
version = versionFromMuslLdd(ldd.stdout);
method = 'ldd';
} else if (ldd.status === 1 && ldd.stderr.indexOf(MUSL) !== -1) {
family = MUSL;
version = versionFromMuslLdd(ldd.stderr);
method = 'ldd';
} else {
// Try filesystem (family only)
try {
var lib = readdirSync('/lib');
if (lib.some(contains('-linux-gnu'))) {
family = GLIBC;
method = 'filesystem';
} else if (lib.some(contains('libc.musl-'))) {
family = MUSL;
method = 'filesystem';
} else if (lib.some(contains('ld-musl-'))) {
family = MUSL;
method = 'filesystem';
}
} catch (e) {}
}
Expand All @@ -63,5 +76,6 @@ module.exports = {
MUSL: MUSL,
family: family,
version: version,
method: method,
isNonGlibcLinux: isNonGlibcLinux
};
27 changes: 18 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const proxyquire = require('proxyquire')
.noPreserveCache();

ava('linux glibc is detected', function (t) {
t.plan(5);
t.plan(6);

const libc = proxyquire('./', {
os: {
Expand All @@ -28,11 +28,12 @@ ava('linux glibc is detected', function (t) {
t.is('musl', libc.MUSL);
t.is(libc.GLIBC, libc.family);
t.is('1.23', libc.version);
t.is('getconf', libc.method);
t.false(libc.isNonGlibcLinux);
});

ava('linux musl is detected via ldd exit 0', function (t) {
t.plan(5);
t.plan(6);

const libc = proxyquire('./', {
os: {
Expand All @@ -57,11 +58,12 @@ ava('linux musl is detected via ldd exit 0', function (t) {
t.is('musl', libc.MUSL);
t.is(libc.MUSL, libc.family);
t.is('1.2.3', libc.version);
t.is('ldd', libc.method);
t.true(libc.isNonGlibcLinux);
});

ava('linux musl is detected via ldd exit 1', function (t) {
t.plan(5);
t.plan(6);

const libc = proxyquire('./', {
os: {
Expand All @@ -86,11 +88,12 @@ ava('linux musl is detected via ldd exit 1', function (t) {
t.is('musl', libc.MUSL);
t.is(libc.MUSL, libc.family);
t.is('1.2.3', libc.version);
t.is('ldd', libc.method);
t.true(libc.isNonGlibcLinux);
});

ava('darwin is ignored', function (t) {
t.plan(5);
t.plan(6);

const libc = proxyquire('./', {
os: {
Expand All @@ -104,11 +107,12 @@ ava('darwin is ignored', function (t) {
t.is('musl', libc.MUSL);
t.is('', libc.family);
t.is('', libc.version);
t.is('', libc.method);
t.false(libc.isNonGlibcLinux);
});

ava('win32 is ignored', function (t) {
t.plan(5);
t.plan(6);

const libc = proxyquire('./', {
os: {
Expand All @@ -122,11 +126,12 @@ ava('win32 is ignored', function (t) {
t.is('musl', libc.MUSL);
t.is('', libc.family);
t.is('', libc.version);
t.is('', libc.method);
t.false(libc.isNonGlibcLinux);
});

ava('Linux detect glibc from filesystem', function (t) {
t.plan(5);
t.plan(6);

const libc = proxyquire('./', {
os: {
Expand All @@ -150,11 +155,12 @@ ava('Linux detect glibc from filesystem', function (t) {
t.is('musl', libc.MUSL);
t.is(libc.GLIBC, libc.family);
t.is('', libc.version);
t.is('filesystem', libc.method);
t.false(libc.isNonGlibcLinux);
});

ava('Linux detect musl from filesystem', function (t) {
t.plan(5);
t.plan(6);

const libc = proxyquire('./', {
os: {
Expand All @@ -178,11 +184,12 @@ ava('Linux detect musl from filesystem', function (t) {
t.is('musl', libc.MUSL);
t.is(libc.MUSL, libc.family);
t.is('', libc.version);
t.is('filesystem', libc.method);
t.true(libc.isNonGlibcLinux);
});

ava('NodeOS detect musl from filesystem', function (t) {
t.plan(5);
t.plan(6);

const libc = proxyquire('./', {
os: {
Expand All @@ -206,11 +213,12 @@ ava('NodeOS detect musl from filesystem', function (t) {
t.is('musl', libc.MUSL);
t.is(libc.MUSL, libc.family);
t.is('', libc.version);
t.is('filesystem', libc.method);
t.true(libc.isNonGlibcLinux);
});

ava('Linux fail to detect from filesystem', function (t) {
t.plan(5);
t.plan(6);

const libc = proxyquire('./', {
os: {
Expand All @@ -234,5 +242,6 @@ ava('Linux fail to detect from filesystem', function (t) {
t.is('musl', libc.MUSL);
t.is('', libc.family);
t.is('', libc.version);
t.is('', libc.method);
t.false(libc.isNonGlibcLinux);
});

0 comments on commit 01fdf51

Please sign in to comment.