Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: support custom ruby filenames with --file
Allow custom ruby file names to be tested when scanned with:

snyk test --file=rails.2.4.5.gemfile --package-manager=rubygems
snyk monitor --file=gemfiles/Gemfile.lock.rails-2.4.5 --package-manager=rubygems
  • Loading branch information
lili2311 committed Aug 18, 2020
1 parent de5db44 commit 42b3a51
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/lib/plugins/rubygems/index.ts
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}`);
}
29 changes: 20 additions & 9 deletions src/lib/plugins/rubygems/inspectors/gemfile.ts
Expand Up @@ -2,31 +2,42 @@ import * as path from 'path';
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 { 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) {
return {
packageName: path.basename(root),
targetFile: path.join(targetDir, targetName),
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',
);
}
}
1 change: 0 additions & 1 deletion src/lib/plugins/rubygems/inspectors/try-get-spec.ts
Expand Up @@ -16,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
@@ -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`': (
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
@@ -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
@@ -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
@@ -0,0 +1,5 @@
source :rubygems

gem "sanitize", "4.6.2"
gem "yard", "0.8.0"
gem "lynx", "0.4.0"
@@ -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

0 comments on commit 42b3a51

Please sign in to comment.