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

Feat: allow custom ruby file names #1345

Merged
merged 2 commits into from
Aug 19, 2020
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
2 changes: 1 addition & 1 deletion src/lib/plugins/rubygems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ async function gatherSpecs(root, targetFile): Promise<Spec> {
}
}

throw new Error(`Could not handle file: ${targetFile}`);
throw new Error(`Could not handle rubygems file: ${targetFile}`);
}
48 changes: 25 additions & 23 deletions src/lib/plugins/rubygems/inspectors/gemfile.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import * as path from 'path';
import { Files, tryGetSpec } from './try-get-spec';
import { tryGetSpec } from './try-get-spec';
import { Spec } from './index';

const pattern = /^Gemfile(\.lock)*$/;
/* Supported example patterns:
* Gemfile
* Gemfile.lock
* rails.2.4.5.gemfile
* rails.2.4.5.gemfile.lock
* gemfiles/Gemfile.rails-2.4.5.lock
* gemfiles/Gemfile.lock.rails-2.4.5
*/

const gemfileOrLockfilePattern = /.*[gG]emfile.*(\.lock)?.*$/;
const gemfileLockPattern = /.*[gG]emfile.*(\.lock).*$/;

export function canHandle(file: string): boolean {
return !!file && pattern.test(path.basename(file));
return !!file && gemfileOrLockfilePattern.test(path.basename(file));
}

export async function gatherSpecs(root: string, target: string): Promise<Spec> {
const targetName = path.basename(target);
const targetDir = path.dirname(target);
const files: Files = {};

const { dir, name } = path.parse(target);
const isGemfileLock = gemfileLockPattern.test(target);
// if the target is a Gemfile we treat is as the lockfile
const gemfileLock = await tryGetSpec(
root,
path.join(targetDir, 'Gemfile.lock'),
isGemfileLock ? target : path.join(dir, name + '.lock'),
);

if (gemfileLock) {
files.gemfileLock = gemfileLock;
return {
packageName: path.basename(root),
targetFile: path.join(dir, name),
files: { gemfileLock },
};
} else {
throw new Error(
"Missing Gemfile.lock file: we can't test " +
'without dependencies.\nPlease run `bundle install` first.',
`Could not read ${target || 'Gemfile.lock'} lockfile: can't test ` +
'without dependencies.\nPlease run `bundle install` first or' +
' if this is a custom file name re-run with --file=path/to/custom.gemfile.lock --package-manager=rubygems',
);
}

const gemfile = await tryGetSpec(root, path.join(targetDir, 'Gemfile'));

if (gemfile) {
files.gemfile = gemfile;
}

return {
packageName: path.basename(root),
targetFile: path.join(targetDir, targetName),
files,
};
}
6 changes: 0 additions & 6 deletions src/lib/plugins/rubygems/inspectors/gemspec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ export async function gatherSpecs(root: string, target: string): Promise<Spec> {
files.gemfileLock = gemfileLock;
}

const gemfile = await tryGetSpec(root, path.join(targetDir, 'Gemfile'));

if (gemfile) {
files.gemfile = gemfile;
}

return {
packageName: path.basename(root),
targetFile: path.join(targetDir, targetName),
Expand Down
2 changes: 0 additions & 2 deletions src/lib/plugins/rubygems/inspectors/try-get-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ interface File {

export interface Files {
gemfileLock?: File;
gemfile?: File;
gemspec?: File;
}

Expand All @@ -17,7 +16,6 @@ export async function tryGetSpec(
name: string,
): Promise<File | null> {
const filePath = path.resolve(dir, name);

if (fs.existsSync(filePath)) {
return {
name,
Expand Down
75 changes: 74 additions & 1 deletion test/acceptance/cli-test/cli-test.ruby.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as path from 'path';
import * as _ from '@snyk/lodash';
import { AcceptanceTests } from './cli-test.acceptance.test';
import { getWorkspaceJSON } from '../workspace-helper';
Expand Down Expand Up @@ -42,6 +41,80 @@ export const RubyTests: AcceptanceTests = {
);
},

'`test ruby-app-custom-names --file=123.gemfile.lock --package-manager=rubygems`': (
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test where we test also e.g. rails.2.4.5.gemfile files and not only lockfiles? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

params,
utils,
) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('ruby-app-custom-names', {
file: '123.gemfile.lock',
packageManager: 'rubygems',
});

const req = params.server.popRequest();
t.equal(req.method, 'POST', 'makes POST request');
t.equal(
req.headers['x-snyk-cli-version'],
params.versionNumber,
'sends version number',
);
t.match(req.url, '/test-dep-graph', 'posts to correct url');

const depGraph = req.body.depGraph;
t.equal(depGraph.pkgManager.name, 'rubygems');
t.same(
depGraph.pkgs.map((p) => p.id).sort(),
[
'crass@1.0.4',
'lynx@0.4.0',
'mini_portile2@2.3.0',
'nokogiri@1.8.5',
'nokogumbo@1.5.0',
'ruby-app-custom-names@',
'sanitize@4.6.2',
'yard@0.8.0',
].sort(),
'depGraph looks fine',
);
},

'`test ruby-app-custom-names --file=gemfiles/Gemfile.rails-2.4.5.lock --package-manager=rubygems`': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();
await params.cli.test('ruby-app-custom-names', {
file: 'gemfiles/Gemfile.rails-2.4.5.lock',
packageManager: 'rubygems',
});

const req = params.server.popRequest();
t.equal(req.method, 'POST', 'makes POST request');
t.equal(
req.headers['x-snyk-cli-version'],
params.versionNumber,
'sends version number',
);
t.match(req.url, '/test-dep-graph', 'posts to correct url');

const depGraph = req.body.depGraph;
t.equal(depGraph.pkgManager.name, 'rubygems');
t.same(
depGraph.pkgs.map((p) => p.id).sort(),
[
'crass@1.0.4',
'lynx@0.4.0',
'mini_portile2@2.3.0',
'nokogiri@1.8.5',
'nokogumbo@1.5.0',
'ruby-app-custom-names@',
'sanitize@4.6.2',
'yard@0.8.0',
].sort(),
'depGraph looks fine',
);
},

'`test ruby-app` meta when no vulns': (params, utils) => async (t) => {
utils.chdirWorkspaces();
const commandResult: CommandResult = await params.cli.test('ruby-app');
Expand Down
5 changes: 5 additions & 0 deletions test/acceptance/workspaces/ruby-app-custom-names/123.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source :rubygems

gem "sanitize", "4.6.2"
gem "yard", "0.8.0"
gem "lynx", "0.4.0"
26 changes: 26 additions & 0 deletions test/acceptance/workspaces/ruby-app-custom-names/123.gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
GEM
remote: http://rubygems.org/
specs:
crass (1.0.4)
lynx (0.4.0)
mini_portile2 (2.3.0)
nokogiri (1.8.5)
mini_portile2 (~> 2.3.0)
nokogumbo (1.5.0)
nokogiri
sanitize (4.6.2)
crass (~> 1.0.2)
nokogiri (>= 1.4.4)
nokogumbo (~> 1.4)
yard (0.8.0)

PLATFORMS
ruby

DEPENDENCIES
lynx (= 0.4.0)
sanitize (= 4.6.2)
yard (= 0.8.0)

BUNDLED WITH
1.16.5
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source :rubygems

gem "sanitize", "4.6.2"
gem "yard", "0.8.0"
gem "lynx", "0.4.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
GEM
remote: http://rubygems.org/
specs:
crass (1.0.4)
lynx (0.4.0)
mini_portile2 (2.3.0)
nokogiri (1.8.5)
mini_portile2 (~> 2.3.0)
nokogumbo (1.5.0)
nokogiri
sanitize (4.6.2)
crass (~> 1.0.2)
nokogiri (>= 1.4.4)
nokogumbo (~> 1.4)
yard (0.8.0)

PLATFORMS
ruby

DEPENDENCIES
lynx (= 0.4.0)
sanitize (= 4.6.2)
yard (= 0.8.0)

BUNDLED WITH
1.16.5