Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix combination of rubygems and --all-projects #2333

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/lib/plugins/rubygems/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { inspectors, Spec } from './inspectors';
import { MissingTargetFileError } from '../../errors/missing-targetfile-error';
import gemfileLockToDependencies = require('./gemfile-lock-to-dependencies');
const get = require('lodash.get');
import * as get from 'lodash.get';
import { MultiProjectResult } from '@snyk/cli-interface/legacy/plugin';
import * as types from '../types';

export async function inspect(
root: string,
targetFile: string,
options: types.Options = {},
Copy link
Contributor Author

@admons admons Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already send the options when we run inspect, but never used it here.
All changes in this file is just to propagate options to the plugin

): Promise<MultiProjectResult> {
if (!targetFile) {
throw MissingTargetFileError(root);
}
const specs = await gatherSpecs(root, targetFile);
const specs = await gatherSpecs(root, targetFile, options);

return {
plugin: {
Expand Down Expand Up @@ -41,10 +43,14 @@ function getDependenciesFromSpecs(specs) {
return dependencies;
}

async function gatherSpecs(root, targetFile): Promise<Spec> {
async function gatherSpecs(
root: string,
targetFile: string,
options: types.Options,
): Promise<Spec> {
for (const inspector of inspectors) {
if (inspector.canHandle(targetFile)) {
return await inspector.gatherSpecs(root, targetFile);
return await inspector.gatherSpecs(root, targetFile, options);
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/lib/plugins/rubygems/inspectors/gemfile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import { tryGetSpec } from './try-get-spec';
import { Spec } from './index';
import * as types from '../../types';

/* Supported example patterns:
* Gemfile
Expand All @@ -18,7 +19,11 @@ export function canHandle(file: string): boolean {
return !!file && gemfileOrLockfilePattern.test(path.basename(file));
}

export async function gatherSpecs(root: string, target: string): Promise<Spec> {
export async function gatherSpecs(
root: string,
target: string,
options: types.Options,
): Promise<Spec> {
const { dir, name } = path.parse(target);
const isGemfileLock = gemfileLockPattern.test(target);
// if the target is a Gemfile we treat is as the lockfile
Expand All @@ -28,8 +33,11 @@ export async function gatherSpecs(root: string, target: string): Promise<Spec> {
);

if (gemfileLock) {
const basePackageName = path.basename(root);
return {
packageName: path.basename(root),
packageName: options.allSubProjects
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the name of the package here is the actual name of the project, and the project name is a part of the identifier, we only want to change this in the problematic area we're facing of 2 or more projects with the '--all-projects` flag.

? path.join(basePackageName, dir)
: basePackageName,
targetFile: path.join(dir, name),
files: { gemfileLock },
};
Expand Down
13 changes: 13 additions & 0 deletions test/acceptance/cli-test/cli-test.ruby.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const sortBy = require('lodash.sortby');
import { AcceptanceTests } from './cli-test.acceptance.test';
import { getWorkspaceJSON } from '../workspace-helper';
import { CommandResult } from '../../../src/cli/commands/types';
import * as path from 'path';

export const RubyTests: AcceptanceTests = {
language: 'Ruby',
Expand Down Expand Up @@ -785,5 +786,17 @@ export const RubyTests: AcceptanceTests = {
'Suggest using --all-projects',
);
},

'`test monorepo --all-projects`': (params, utils) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('monorepo', { allProjects: true });

const req = params.server.popRequest();

const rootNodePkgId = req.body.depGraph.graph.nodes.find(
(x) => x.nodeId == 'root-node',
).pkgId;
t.equal(rootNodePkgId, `monorepo${path.sep}sub-ruby-app@`);
},
},
};