Skip to content

Commit b28b104

Browse files
committed
fix(MongoBinaryDownloadUrl::getUbuntuVersionString): guard against no mapping
guard against distros having ID_LIKE but no upstream file and no special mapping fixes #616
1 parent 6b60cf4 commit b28b104

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

packages/mongodb-memory-server-core/src/util/MongoBinaryDownloadUrl.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,7 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts {
340340
* @param os LinuxOS Object
341341
*/
342342
getUbuntuVersionString(os: LinuxOS): string {
343-
const ubuntuOS: LinuxOS = {
344-
...os,
345-
dist: 'ubuntu',
346-
};
343+
let ubuntuOS: LinuxOS | undefined = undefined;
347344

348345
// "id_like" processing (version conversion) [this is an block to be collapsible]
349346
{
@@ -355,8 +352,12 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts {
355352
20: '20.04',
356353
};
357354

358-
ubuntuOS.release =
359-
mintToUbuntuRelease[parseInt(os.release.split('.')[0])] || mintToUbuntuRelease[20];
355+
ubuntuOS = {
356+
os: 'linux',
357+
dist: 'ubuntu',
358+
release:
359+
mintToUbuntuRelease[parseInt(os.release.split('.')[0])] || mintToUbuntuRelease[20],
360+
};
360361
}
361362

362363
if (/^elementary\s?os\s*$/i.test(os.dist)) {
@@ -371,7 +372,29 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts {
371372
const [elementaryMajor, elementaryMinor] = os.release.split('.').map((el) => parseInt(el));
372373
const realMajor = elementaryMajor || elementaryMinor;
373374

374-
ubuntuOS.release = elementaryToUbuntuRelease[realMajor] || elementaryToUbuntuRelease[6];
375+
ubuntuOS = {
376+
os: 'linux',
377+
dist: 'ubuntu',
378+
release: elementaryToUbuntuRelease[realMajor] || elementaryToUbuntuRelease[6],
379+
};
380+
}
381+
}
382+
383+
if (isNullOrUndefined(ubuntuOS)) {
384+
// Warn against distros that have a ID_LIKE set to "ubuntu", but no other upstream information and are not specially mapped (see above)
385+
if (!/^ubuntu(?:| linux)\s*$/i.test(os.dist)) {
386+
console.warn(
387+
`Unmapped distro "${os.dist}" with ID_LIKE "ubuntu", defaulting to highest ubuntu version!\n` +
388+
'This means that your distro does not have a internal mapping in MMS or does not have a upstream release file (like "/etc/upstream-release/lsb-release"), but has set a ID_LIKE'
389+
);
390+
391+
ubuntuOS = {
392+
os: 'linux',
393+
dist: 'ubuntu',
394+
release: '20.04', // TODO: try to keep this up-to-date to the latest LTS
395+
};
396+
} else {
397+
ubuntuOS = os;
375398
}
376399
}
377400

packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownloadUrl.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,23 @@ describe('MongoBinaryDownloadUrl', () => {
734734
})
735735
).toBe('ubuntu2004');
736736
});
737+
738+
it('should return default version with warning when using ID_LIKE but not being ubuntu', () => {
739+
// Test for https://github.com/nodkz/mongodb-memory-server/issues/616
740+
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => void 0);
741+
742+
downloadUrl.version = '5.0.3';
743+
expect(
744+
downloadUrl.getUbuntuVersionString({
745+
os: 'linux',
746+
dist: 'zorin',
747+
release: '16',
748+
id_like: ['ubuntu'],
749+
})
750+
).toBe('ubuntu2004');
751+
752+
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
753+
});
737754
});
738755

739756
describe('getDebianVersionString()', () => {

0 commit comments

Comments
 (0)