Skip to content

Commit ebd73f8

Browse files
committed
Merge pull request #1 from codeclimate/gd-gemfile-absence
Emit warning when Gemfile.lock does not exist
2 parents 3881855 + 4283d5a commit ebd73f8

File tree

6 files changed

+78
-11
lines changed

6 files changed

+78
-11
lines changed

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ source "https://rubygems.org"
33
gem 'bundler-audit'
44
gem 'json'
55
gem 'versionomy'
6+
gem "rake"
7+
8+
group :test do
9+
gem "rspec", require: false
10+
gem "fakefs", require: false
11+
end

Gemfile.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@ GEM
55
bundler-audit (0.3.1)
66
bundler (~> 1.2)
77
thor (~> 0.18)
8+
diff-lcs (1.2.5)
9+
fakefs (0.6.7)
810
json (1.8.3)
11+
rake (10.4.2)
12+
rspec (3.3.0)
13+
rspec-core (~> 3.3.0)
14+
rspec-expectations (~> 3.3.0)
15+
rspec-mocks (~> 3.3.0)
16+
rspec-core (3.3.1)
17+
rspec-support (~> 3.3.0)
18+
rspec-expectations (3.3.0)
19+
diff-lcs (>= 1.2.0, < 2.0)
20+
rspec-support (~> 3.3.0)
21+
rspec-mocks (3.3.1)
22+
diff-lcs (>= 1.2.0, < 2.0)
23+
rspec-support (~> 3.3.0)
24+
rspec-support (3.3.0)
925
thor (0.19.1)
1026
versionomy (0.4.4)
1127
blockenspiel (>= 0.4.5)
@@ -15,7 +31,10 @@ PLATFORMS
1531

1632
DEPENDENCIES
1733
bundler-audit
34+
fakefs
1835
json
36+
rake
37+
rspec
1938
versionomy
2039

2140
BUNDLED WITH

Rakefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'rspec/core/rake_task'
2+
RSpec::Core::RakeTask.new(:spec)
3+
4+
task default: :spec

lib/cc/engine/bundler_audit.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,34 @@ def initialize(directory: , io: , engine_config: )
1111
end
1212

1313
def run
14-
Dir.chdir(@directory)
15-
raw_output = `bundle-audit`
16-
raw_issues = raw_output.split(/\n\n/).select { |chunk|
17-
chunk =~ /^Name: /
18-
}
19-
@gemfile_lock_lines = File.read(
20-
File.join(@directory, 'Gemfile.lock')
21-
).lines
22-
raw_issues.each do |raw_issue|
23-
issue = issue_from_raw(raw_issue)
24-
@io.print("#{issue.to_json}\0")
14+
if gemfile_lock_exists?
15+
Dir.chdir(@directory)
16+
raw_output = `bundle-audit`
17+
raw_issues = raw_output.split(/\n\n/).select { |chunk|
18+
chunk =~ /^Name: /
19+
}
20+
@gemfile_lock_lines = File.read(
21+
File.join(@directory, 'Gemfile.lock')
22+
).lines
23+
raw_issues.each do |raw_issue|
24+
issue = issue_from_raw(raw_issue)
25+
@io.print("#{issue.to_json}\0")
26+
end
27+
else
28+
warning = {
29+
type: "warning",
30+
description: "No Gemfile.lock file found"
31+
}
32+
@io.print("#{warning.to_json}\0")
2533
end
2634
end
2735

2836
private
2937

38+
def gemfile_lock_exists?
39+
File.exist?(File.join(@directory, 'Gemfile.lock'))
40+
end
41+
3042
def issue_from_raw(raw_issue)
3143
raw_issue_hash = {}
3244
raw_issue.lines.each do |l|

spec/cc/engine/bundler_audit_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "spec_helper"
2+
3+
module CC::Engine
4+
describe BundlerAudit do
5+
describe "#run" do
6+
it "emits a warning when no Gemfile exists" do
7+
FakeFS do
8+
directory = "/c"
9+
FileUtils.mkdir_p(directory)
10+
io = StringIO.new
11+
config = {}
12+
13+
BundlerAudit.new(directory: directory, io: io, engine_config: config).run
14+
15+
expect(io.string).to match(%{{"type":"warning","description":"No Gemfile.lock file found"}})
16+
end
17+
end
18+
end
19+
end
20+
end

spec/spec_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "../lib")))
2+
3+
require "rspec"
4+
require "fakefs/safe"
5+
6+
require "cc/engine/bundler_audit"

0 commit comments

Comments
 (0)