Skip to content

Commit

Permalink
fix: find wars and aars file in scan-all-unmanaged mode
Browse files Browse the repository at this point in the history
  • Loading branch information
admons committed Jan 25, 2021
1 parent 10afade commit 6fb5ba1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
10 changes: 5 additions & 5 deletions lib/jar.ts → lib/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function getDependencies(paths: string[]): Promise<MavenDependency[]> {
return dependencies;
}

export async function createPomForJar(
export async function createPomForArchive(
root: string,
targetFile: string,
): Promise<string> {
Expand All @@ -82,7 +82,7 @@ export async function createPomForJar(
}
}

export async function createPomForJars(
export async function createPomForArchives(
root: string,
jarPaths: string[],
): Promise<string> {
Expand All @@ -100,14 +100,14 @@ export async function createPomForJars(
}
}

export function isJar(file: string): boolean {
export function isArchive(file: string): boolean {
return !!file.match(/\.(([jwa]ar)|(zip))$/);
}

export function findJars(targetPath: string, recursive = false): string[] {
export function findArchives(targetPath: string, recursive = false): string[] {
const stats = fs.statSync(targetPath);
const dir = stats.isFile() ? path.dirname(targetPath) : targetPath;
return glob.sync(`${dir}/${recursive ? '**/' : ''}*.jar`);
return glob.sync(`${dir}/${recursive ? '**/' : ''}*.@(jar|war|aar|zip)`);
}

async function getMavenPackageInfo(
Expand Down
15 changes: 10 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import * as path from 'path';

import { parseTree, parseVersions } from './parse-mvn';
import * as subProcess from './sub-process';
import { createPomForJar, createPomForJars, findJars, isJar } from './jar';
import {
createPomForArchive,
createPomForArchives,
findArchives,
isArchive,
} from './archive';
import { formatGenericPluginError } from './error-format';
import { CallGraph, CallGraphResult } from '@snyk/cli-interface/legacy/common';
import debugModule = require('debug');
Expand Down Expand Up @@ -112,17 +117,17 @@ export async function inspect(
options = { dev: false, scanAllUnmanaged: false };
}

if (isJar(targetPath)) {
if (isArchive(targetPath)) {
debug(`Creating pom from jar ${targetFile}`);
targetFile = await createPomForJar(root, targetFile!);
targetFile = await createPomForArchive(root, targetFile!);
}

if (options.scanAllUnmanaged) {
const recursive = !!options.allProjects;
const jars = findJars(root, recursive);
const jars = findArchives(root, recursive);
if (jars.length > 0) {
debug(`Creating pom from jars in for ${root}`);
targetFile = await createPomForJars(root, jars);
targetFile = await createPomForArchives(root, jars);
} else {
throw Error(`Could not find any supported files in '${root}'.`);
}
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as test from 'tap-only';
import * as path from 'path';
import { findJars, isJar } from '../../lib/jar';
import { findArchives, isArchive } from '../../lib/archive';

test('isJar', async (t) => {
test('isArchive', async (t) => {
[
'mvn-app-1.0-SNAPSHOT.jar',
'mvn-app-1.0-SNAPSHOT.war',
'mvn-app-1.0-SNAPSHOT.zip',
'path/to/mvn-app-1.0-SNAPSHOT.jar',
'path/to/mvn-app-1.0-SNAPSHOT.war',
'path/to/mvn-app-1.0-SNAPSHOT.zip',
].forEach((i) => t.ok(isJar(i), 'should be true for ' + i));
].forEach((i) => t.ok(isArchive(i), 'should be true for ' + i));

[
'mvn-app-1.0-SNAPSHOTjar',
Expand All @@ -19,26 +19,28 @@ test('isJar', async (t) => {
'path/to/jar/mvn-app-1.0-SNAPSHOTjar',
'path/to/war/mvn-app-1.0-SNAPSHOTwar',
'path/to/zip/mvn-app-1.0-SNAPSHOTzip',
].forEach((i) => t.notOk(isJar(i), 'should be false for ' + i));
].forEach((i) => t.notOk(isArchive(i), 'should be false for ' + i));
});

const fixturesPath = path.join(__dirname, '..', 'fixtures');
const springCorePath = path.join(fixturesPath, 'spring-core');
const badPath = path.join(fixturesPath, 'bad');
const dummyPath = path.join(fixturesPath, 'dummy');
const nestedJarsPath = path.join(fixturesPath, 'nested-jars');
const nestedWarsAarsPath = path.join(fixturesPath, 'nested-wars-aars');

test('findJars', async (t) => {
test('findArchives', async (t) => {
[
{ dir: springCorePath, expectedNumOfJars: 1 },
{ dir: badPath, expectedNumOfJars: 2 },
{ dir: fixturesPath, expectedNumOfJars: 0 },
{ dir: dummyPath, expectedNumOfJars: 0 },
{ dir: nestedJarsPath, expectedNumOfJars: 1 },
{ dir: nestedJarsPath, expectedNumOfJars: 2, recursive: true },
{ dir: nestedWarsAarsPath, expectedNumOfJars: 2, recursive: true },
].forEach(({ dir, expectedNumOfJars, recursive }) =>
t.same(
findJars(dir, recursive).length,
findArchives(dir, recursive).length,
expectedNumOfJars,
`should find ${expectedNumOfJars} jars for "${path.basename(dir)}" ${
recursive ? '(recursive)' : ''
Expand Down

0 comments on commit 6fb5ba1

Please sign in to comment.