Skip to content

Commit

Permalink
feat(getos): support multiple "id_like"
Browse files Browse the repository at this point in the history
re #525
  • Loading branch information
hasezoey committed Aug 7, 2021
1 parent af33d00 commit ce42fad
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
Expand Up @@ -201,7 +201,9 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts {

// warn for the fallback
console.warn(
`Unknown/unsupported linux "${os.dist}(${os.id_like})". Falling back to legacy MongoDB build!`
`Unknown/unsupported linux "${os.dist}(${os.id_like?.join(
', '
)})". Falling back to legacy MongoDB build!`
);

return this.getLegacyVersionString();
Expand Down Expand Up @@ -452,5 +454,8 @@ export default MongoBinaryDownloadUrl;
* Helper function to reduce code / regex duplication
*/
function regexHelper(regex: RegExp, os: LinuxOS): boolean {
return regex.test(os.dist) || (!isNullOrUndefined(os.id_like) ? regex.test(os.id_like) : false);
return (
regex.test(os.dist) ||
(!isNullOrUndefined(os.id_like) ? os.id_like.filter((v) => regex.test(v)).length >= 1 : false)
);
}
Expand Up @@ -216,7 +216,7 @@ describe('MongoBinaryDownloadUrl', () => {
os: 'linux',
dist: 'ManjaroLinux',
release: '20.2',
id_like: 'arch',
id_like: ['arch'],
},
});
expect(await du.getDownloadUrl()).toBe(
Expand All @@ -236,7 +236,7 @@ describe('MongoBinaryDownloadUrl', () => {
os: 'linux',
dist: 'Arch',
release: 'rolling',
id_like: 'arch',
id_like: ['arch'],
},
});
expect(await du.getDownloadUrl()).toBe(
Expand All @@ -256,7 +256,7 @@ describe('MongoBinaryDownloadUrl', () => {
os: 'linux',
dist: 'ArchStrike',
release: 'rolling',
id_like: 'arch',
id_like: ['arch'],
},
});
expect(await du.getDownloadUrl()).toBe(
Expand All @@ -275,7 +275,7 @@ describe('MongoBinaryDownloadUrl', () => {
os: 'linux',
dist: 'elementary OS',
release: '0.3',
id_like: 'ubuntu',
id_like: ['ubuntu'],
},
});

Expand All @@ -293,7 +293,7 @@ describe('MongoBinaryDownloadUrl', () => {
os: 'linux',
dist: 'elementary OS',
release: '5.1',
id_like: 'ubuntu',
id_like: ['ubuntu'],
},
});

Expand All @@ -314,7 +314,7 @@ describe('MongoBinaryDownloadUrl', () => {
os: 'linux',
dist: 'Linux Mint',
release: '',
id_like: 'ubuntu',
id_like: ['ubuntu'],
},
});
});
Expand Down
Expand Up @@ -59,7 +59,7 @@ UBUNTU_CODENAME=focal`;
dist: 'linuxmint',
release: '20.1',
codename: 'ulyssa',
id_like: 'ubuntu',
id_like: ['ubuntu'],
});
});

Expand All @@ -82,7 +82,27 @@ UBUNTU_CODENAME=focal`;
dist: 'linuxmint',
release: '20.2',
codename: 'uma',
id_like: 'ubuntu',
id_like: ['ubuntu'],
});
});

it('should parse multiple "id_like"', () => {
const example = `NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"`;

expect(getos.parseOS(example)).toEqual<getos.LinuxOS>({
os: 'linux',
dist: 'amzn',
release: '2',
codename: undefined,
id_like: ['centos', 'rhel', 'fedora'],
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/mongodb-memory-server-core/src/util/getos/index.ts
Expand Up @@ -17,7 +17,7 @@ const OSRegex = {
name: /^id\s*=\s*"?(\w*)"?$/im,
codename: /^version_codename\s*=\s*(.*)$/im,
release: /^version_id\s*=\s*"?(\d*(?:\.\d*)?)"?$/im,
id_like: /^id_like\s*=\s*"?(\w*)"?$/im,
id_like: /^id_like\s*=\s*"?([\w\s]*)"?$/im,
};

export interface OtherOS {
Expand All @@ -29,7 +29,7 @@ export interface LinuxOS extends OtherOS {
dist: string;
release: string;
codename?: string;
id_like?: string;
id_like?: string[];
}

export type AnyOS = OtherOS | LinuxOS;
Expand Down Expand Up @@ -128,6 +128,6 @@ export function parseOS(input: string): LinuxOS {
dist: input.match(OSRegex.name)?.[1].toLocaleLowerCase() ?? 'unknown',
codename: input.match(OSRegex.codename)?.[1].toLocaleLowerCase(),
release: input.match(OSRegex.release)?.[1].toLocaleLowerCase() ?? '',
id_like: input.match(OSRegex.id_like)?.[1].toLocaleLowerCase(),
id_like: input.match(OSRegex.id_like)?.[1].toLocaleLowerCase().split(' '),
};
}

0 comments on commit ce42fad

Please sign in to comment.