Skip to content

Commit 66541de

Browse files
committed
fix(getos): check if given os is also not UNKNOWN and try different formats
re #735
1 parent a1195f1 commit 66541de

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,23 @@ HOME_URL="https://amazonlinux.com/"`;
110110
});
111111
});
112112
});
113+
114+
describe('isValidOs', () => {
115+
it('should return FALSE if undefined / null', () => {
116+
expect(getos.isValidOs(undefined)).toStrictEqual(false);
117+
expect(getos.isValidOs(null as any)).toStrictEqual(false);
118+
});
119+
120+
it('should return FALSE if distro is UNKNOWN', () => {
121+
expect(getos.isValidOs({ dist: getos.UNKNOWN, os: 'linux', release: '0' })).toStrictEqual(
122+
false
123+
);
124+
});
125+
126+
it('should return TRUE if distro is not UNKNOWN', () => {
127+
expect(getos.isValidOs({ dist: 'ubuntu', os: 'linux', release: '20.04' })).toStrictEqual(
128+
true
129+
);
130+
});
131+
});
113132
});

packages/mongodb-memory-server-core/src/util/getos/index.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const OSRegex = {
2020
id_like: /^id_like\s*=\s*"?([\w\s]*)"?$/im,
2121
};
2222

23+
/** Helper Static so that a consistent UNKNOWN value is used */
24+
export const UNKNOWN = 'unknown';
25+
2326
export interface OtherOS {
2427
os: 'aix' | 'android' | 'darwin' | 'freebsd' | 'openbsd' | 'sunos' | 'win32' | 'cygwin' | string;
2528
}
@@ -74,53 +77,67 @@ async function getLinuxInformation(): Promise<LinuxOS> {
7477

7578
const upstreamLSB = await tryReleaseFile('/etc/upstream-release/lsb-release', parseLSB);
7679

77-
if (!isNullOrUndefined(upstreamLSB)) {
80+
if (isValidOs(upstreamLSB)) {
7881
log('getLinuxInformation: Using UpstreamLSB');
7982

8083
return upstreamLSB;
8184
}
8285

8386
const etcOsRelease = await tryReleaseFile('/etc/os-release', parseOS);
8487

85-
if (!isNullOrUndefined(etcOsRelease)) {
88+
if (isValidOs(etcOsRelease)) {
8689
log('getLinuxInformation: Using etcOsRelease');
8790

8891
return etcOsRelease;
8992
}
9093

9194
const usrOsRelease = await tryReleaseFile('/usr/lib/os-release', parseOS);
9295

93-
if (!isNullOrUndefined(usrOsRelease)) {
96+
if (isValidOs(usrOsRelease)) {
9497
log('getLinuxInformation: Using usrOsRelease');
9598

9699
return usrOsRelease;
97100
}
98101

99102
const etcLSBRelease = await tryReleaseFile('/etc/lsb-release', parseLSB);
100103

101-
if (!isNullOrUndefined(etcLSBRelease)) {
104+
if (isValidOs(etcLSBRelease)) {
102105
log('getLinuxInformation: Using etcLSBRelease');
103106

104107
return etcLSBRelease;
105108
}
106109

107-
console.warn('Could not find any Release File, using fallback binary');
110+
console.warn('Could not find any valid Release File, using fallback information');
108111

109112
// if none has worked, return unknown
110113
return {
111114
os: 'linux',
112-
dist: 'unknown',
115+
dist: UNKNOWN,
113116
release: '',
114117
};
115118
}
116119

120+
/**
121+
* Helper function to check if the input os is valid
122+
* @param os The OS information to check
123+
* @returns `true` if not undefined AND not UNKNOWN
124+
*/
125+
export function isValidOs(os: LinuxOS | undefined): os is LinuxOS {
126+
// helper for debugging
127+
if (os && os.dist === UNKNOWN) {
128+
log('isValidOS: found defined os, but was unknown:', os);
129+
}
130+
131+
return !isNullOrUndefined(os) && os.dist !== UNKNOWN;
132+
}
133+
117134
/**
118135
* Parse LSB-like output (either command or file)
119136
*/
120137
export function parseLSB(input: string): LinuxOS {
121138
return {
122139
os: 'linux',
123-
dist: input.match(LSBRegex.name)?.[1].toLocaleLowerCase() ?? 'unknown',
140+
dist: input.match(LSBRegex.name)?.[1].toLocaleLowerCase() ?? UNKNOWN,
124141
codename: input.match(LSBRegex.codename)?.[1].toLocaleLowerCase(),
125142
release: input.match(LSBRegex.release)?.[1].toLocaleLowerCase() ?? '',
126143
};
@@ -132,7 +149,7 @@ export function parseLSB(input: string): LinuxOS {
132149
export function parseOS(input: string): LinuxOS {
133150
return {
134151
os: 'linux',
135-
dist: input.match(OSRegex.name)?.[1].toLocaleLowerCase() ?? 'unknown',
152+
dist: input.match(OSRegex.name)?.[1].toLocaleLowerCase() ?? UNKNOWN,
136153
codename: input.match(OSRegex.codename)?.[1].toLocaleLowerCase(),
137154
release: input.match(OSRegex.release)?.[1].toLocaleLowerCase() ?? '',
138155
id_like: input.match(OSRegex.id_like)?.[1].toLocaleLowerCase().split(' '),

0 commit comments

Comments
 (0)