Skip to content

Commit

Permalink
feat: search sbt dep graph plugin in the new naming
Browse files Browse the repository at this point in the history
When looking for the needed plugin for our new inspect() flow, we were not globbing for the new method of including such plugin
in sbt versions 1.4+ which says to include addDependencyTreePlugin on your plugins.sbt.
  • Loading branch information
mika-bar committed Dec 27, 2021
1 parent ed8b5b4 commit 1d2c92b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const sbtCoursierPluginName = 'sbt-coursier';
export const sbtDependencyGraphPluginName = 'sbt-dependency-graph';
export const sbtDependencyGraphPluginNameNew = 'addDependencyTreePlugin';
13 changes: 7 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as debugModule from 'debug';
// To enable debugging output, run the CLI as `DEBUG=snyk-sbt-plugin snyk ...`
const debug = debugModule('snyk-sbt-plugin');

import { sbtCoursierPluginName, sbtDependencyGraphPluginName } from './constants';
import { sbtCoursierPluginName, sbtDependencyGraphPluginName, sbtDependencyGraphPluginNameNew } from './constants';
import * as subProcess from './sub-process';
import * as parser from './parse-sbt';
import * as types from './types';
Expand Down Expand Up @@ -41,7 +41,8 @@ export async function inspect(
root,
targetFile,
sbtDependencyGraphPluginName,
);
) || await isPluginInstalled(root,
targetFile, sbtDependencyGraphPluginNameNew);
Object.assign(options, { isCoursierPresent });
// in order to apply the pluginInspect, coursier should *not* be present and sbt-dependency-graph should be present
if (!isCoursierPresent && isSbtDependencyGraphPresent) {
Expand All @@ -54,9 +55,9 @@ export async function inspect(
} else {
debug(
'coursier present = ' +
isCoursierPresent +
', sbt-dependency-graph present = ' +
isSbtDependencyGraphPresent,
isCoursierPresent +
', sbt-dependency-graph present = ' +
isSbtDependencyGraphPresent,
);
debug('Falling back to legacy inspect');
// tslint:disable-next-line:no-console
Expand Down Expand Up @@ -206,7 +207,7 @@ async function pluginInspect(
} catch (error) {
debug(
'Failed to produce dependency tree with custom snyk plugin due to error: ' +
error.message,
error.message,
);
return null;
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addDependencyTreePlugin
38 changes: 35 additions & 3 deletions test/functional/plugin-search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path';
import * as os from 'os';
import * as version from '../../lib/version';
import { isPluginInstalled } from '../../lib/plugin-search';
import { sbtDependencyGraphPluginName} from '../../lib/constants';
import { sbtDependencyGraphPluginName, sbtDependencyGraphPluginNameNew } from '../../lib/constants';

describe('plugin-search test', () => {
describe('isPluginInstalled locally', () => {
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('plugin-search test', () => {
});
});

describe('isPluginInstalled globally into 1.0', () => {
describe('isPluginInstalled globally into 1.0, using sbt-dependency-graph old naming convention', () => {
beforeEach(() => {
const homedir = path.join(__dirname, '..', 'fixtures', 'homedir-1.0');
jest.spyOn(os, 'homedir').mockReturnValue(homedir);
Expand All @@ -74,7 +74,39 @@ describe('plugin-search test', () => {
it('returns true if ~/.sbt/1.0/plugins directory has sbt file with given plugin name', async () => {
const root = path.join(__dirname, '..', 'fixtures');
const targetFile = path.join('simple-app', 'build.sbt');
const received = await isPluginInstalled(root, targetFile, sbtDependencyGraphPluginName)
const received = await isPluginInstalled(root, targetFile, sbtDependencyGraphPluginName) ||
await isPluginInstalled(
root,
targetFile,
sbtDependencyGraphPluginNameNew
);
expect(received).toBe(true);
});
it('returns false if ~/.sbt/1.0/plugins directory has sbt file without plugin name', async () => {
const root = path.join(__dirname, '..', 'fixtures');
const targetFile = path.join('simple-app', 'build.sbt');
const received = await isPluginInstalled(root, targetFile, 'will.not.find');
expect(received).toBe(false);
});
});
});
describe('isPluginInstalled globally into 1.0, using addDependencyTreePlugin introduced for sbt versions 1.4+', () => {
beforeEach(() => {
const homedir = path.join(__dirname, '..', 'fixtures', 'homedir-1.0-sbt-1.4+');
jest.spyOn(os, 'homedir').mockReturnValue(homedir);
jest.spyOn(version, 'getSbtVersion').mockResolvedValue('1.0.0');
});
afterEach(() => jest.resetAllMocks());
describe('in users home directory', () => {
it('returns true if ~/.sbt/1.0/plugins directory has sbt file with given plugin name', async () => {
const root = path.join(__dirname, '..', 'fixtures');
const targetFile = path.join('simple-app', 'build.sbt');
const received = await isPluginInstalled(root, targetFile, sbtDependencyGraphPluginName) ||
await isPluginInstalled(
root,
targetFile,
sbtDependencyGraphPluginNameNew
);
expect(received).toBe(true);
});
it('returns false if ~/.sbt/1.0/plugins directory has sbt file without plugin name', async () => {
Expand Down

0 comments on commit 1d2c92b

Please sign in to comment.