Skip to content

Commit

Permalink
Improve filesystem-based detection of glibc (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstaley authored Mar 19, 2024
1 parent 2def922 commit 2642d96
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/detect-libc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const GLIBC = 'glibc';
* A Regexp constant to get the GLIBC Version.
* @type {string}
*/
const RE_GLIBC_VERSION = /GLIBC\s(\d+\.\d+)/;
const RE_GLIBC_VERSION = /LIBC[a-z0-9 \-).]*?(\d+\.\d+)/i;

/**
* A String constant containing the value `musl`.
Expand Down Expand Up @@ -86,7 +86,7 @@ const getFamilyFromLddContent = (content) => {
if (content.includes('musl')) {
return MUSL;
}
if (content.includes('GLIBC')) {
if (content.includes('GNU C Library')) {
return GLIBC;
}
return null;
Expand Down
78 changes: 76 additions & 2 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test('linux - glibc family detected via ldd', async (t) => {
isLinux: () => true
},
'./filesystem': {
readFile: () => Promise.resolve('bunch-of-text GLIBC')
readFile: () => Promise.resolve('# This file is part of the GNU C Library.')
}
});

Expand All @@ -74,7 +74,7 @@ test('linux - glibc familySync detected via ldd', async (t) => {
isLinux: () => true
},
'./filesystem': {
readFileSync: () => 'bunch-of-text GLIBC'
readFileSync: () => '# The GNU C Library is free software; you can redistribute it and/or'
}
});

Expand Down Expand Up @@ -488,6 +488,43 @@ test('linux - glibc version detected via filesystem', async (t) => {
t.is(await libc.version(), '1.23');
});

test('linux - glibc version detected via filesystem (libc)', async (t) => {
t.plan(1);

const out = '--vers | --versi | --versio | --version)\necho \'ldd (GNU libc) 2.39\'';
const libc = proxyquire('../', {
'./process': {
isLinux: () => true
},
'./filesystem': {
readFile: () => Promise.resolve(out)
}
});

t.is(await libc.version(), '2.39');
});

test('linux - libc version not detected via filesystem (void linux musl)', async (t) => {
t.plan(1);

const out = 'startlibc_startGNU AS 2.35.1';
const libc = proxyquire('../', {
'./process': {
isLinux: () => true,
getReport: () => ({})
},
'./filesystem': {
readFile: () => Promise.resolve(out)
},
child_process: {
exec: (_c, cb) => cb(null, out),
execSync: () => out
}
});

t.is(await libc.version(), null);
});

test('linux - glibc version detected via filesystemSync', async (t) => {
t.plan(1);

Expand All @@ -504,6 +541,43 @@ test('linux - glibc version detected via filesystemSync', async (t) => {
t.is(libc.versionSync(), '1.23');
});

test('linux - glibc version detected via filesystemSync (libc)', async (t) => {
t.plan(1);

const out = '--vers | --versi | --versio | --version)\necho \'ldd (GNU libc) 2.39\'';
const libc = proxyquire('../', {
'./process': {
isLinux: () => true
},
'./filesystem': {
readFileSync: () => out
}
});

t.is(libc.versionSync(), '2.39');
});

test('linux - libc version not detected via filesystemSync (void linux musl)', (t) => {
t.plan(1);

const out = 'startlibc_startGNU AS 2.35.1';
const libc = proxyquire('../', {
'./process': {
isLinux: () => true,
getReport: () => ({})
},
'./filesystem': {
readFile: () => Promise.resolve(out)
},
child_process: {
exec: (_c, cb) => cb(null, out),
execSync: () => out
}
});

t.is(libc.versionSync(), null);
});

test('linux - glibc version detected via child process', async (t) => {
t.plan(1);

Expand Down

0 comments on commit 2642d96

Please sign in to comment.