Skip to content

Commit

Permalink
Merge pull request #64 from snyk/feat/MYC-167-add-modules-support
Browse files Browse the repository at this point in the history
feat: add support for modules
  • Loading branch information
shlomiSnyk authored Feb 23, 2023
2 parents 5879b17 + 1f3ccee commit 0292ed6
Show file tree
Hide file tree
Showing 7 changed files with 503 additions and 21 deletions.
10 changes: 10 additions & 0 deletions lib/rpm/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ export async function getPackageInfo(
packageInfo.version = extractString(entry.data);
break;

case RpmTag.MODULARITYLABEL:
if (entry.info.type !== RpmType.STRING) {
throw new ParserError('Unexpected type for module tag', {
type: entry.info.type,
});
}

packageInfo.module = extractString(entry.data);
break;

default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/rpm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface PackageInfo {
size: number;
arch?: string;
epoch?: number;
module?: string;
}

export enum RpmTag {
Expand All @@ -50,6 +51,7 @@ export enum RpmTag {
EPOCH = 1003,
SIZE = 1009,
ARCH = 1022,
MODULARITYLABEL = 5096,
}

export enum RpmType {
Expand Down
4 changes: 3 additions & 1 deletion test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ yum groupinstall -y "Development tools"

# Use RPM to produce a list of expected outputs.
# Put the output in a file under outputs/<OutputName>.
rpm -qa --queryformat "%{NAME}\t%{EPOCH}:%{VERSION}-%{RELEASE}\t%{SIZE}\n" | sed "s/(none)://g" | sed "s/0://g"
# The MODULARITYLABEL will include the module of the package, this will also
# need to be declared in the test.
rpm -qa --queryformat "%{MODULARITYLABEL}\t%{NAME}\t%{EPOCH}:%{VERSION}-%{RELEASE}\t%{SIZE}\n" | sed "s/(none)://g" | sed "s/0://g"

# In a separate shell copy the RPM database file into fixtures/.
# Make sure the file is named similarly to the one under outputs/.
Expand Down
Binary file added test/fixtures/modules
Binary file not shown.
51 changes: 31 additions & 20 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ function fixturePath(path: string): string {
}

describe('Testing various RPM Berkeley databases', () => {
const fixturePaths = [
'amazonlinux2_plain',
'centos6_dev_tools',
'centos6_many',
'centos6_plain',
'centos7_dev_tools',
'centos7_httpd24',
'centos7_many',
'centos7_plain',
'centos7_python35',
'fedora26_many',
'rpm4_empty',
'ubi7_plain',
'ubi8_plain',
const fixtures: [string, boolean][] = [
['amazonlinux2_plain', false],
['centos6_dev_tools', false],
['centos6_many', false],
['centos6_plain', false],
['centos7_dev_tools', false],
['centos7_httpd24', false],
['centos7_many', false],
['centos7_plain', false],
['centos7_python35', false],
['fedora26_many', false],
['rpm4_empty', false],
['ubi7_plain', false],
['ubi8_plain', false],
['modules', true],
];

for (const path of fixturePaths) {
for (const [path, withModules] of fixtures) {
// Create a test run for every fixture
test(path, async () => {
const fixture = fixturePath(`fixtures/${path}`);
Expand All @@ -50,7 +51,10 @@ describe('Testing various RPM Berkeley databases', () => {
expect(parserOutput.rpmMetadata!.packagesSkipped).toEqual(0);

const expectedEntries = expectedOutput.trim().split('\n').sort();
const parserEntries = formatRpmPackages(parserOutput.response).sort();
const parserEntries = formatRpmPackages(
parserOutput.response,
withModules,
).sort();

for (let j = 0; j < expectedEntries.length; j++) {
const expectedEntry = expectedEntries[j];
Expand Down Expand Up @@ -78,10 +82,17 @@ describe('Testing various RPM sqlite databases', async () => {
}
});

function formatRpmPackages(packages: PackageInfo[]): string[] {
function formatRpmPackages(
packages: PackageInfo[],
enableModules = false,
): string[] {
return packages.map((packageInfo) => {
return `${packageInfo.name}\t${formatRpmPackageVersion(packageInfo)}\t${
packageInfo.size
}`;
let prefix = '';
if (enableModules) {
prefix = (packageInfo.module ? packageInfo.module : '(none)') + '\t';
}
return `${prefix}${packageInfo.name}\t${formatRpmPackageVersion(
packageInfo,
)}\t${packageInfo.size}`;
});
}
Loading

0 comments on commit 0292ed6

Please sign in to comment.