Skip to content

Commit f5f8120

Browse files
committed
Merge pull request #56 from codeclimate/will/resolve-nonexistent-files
FileListResolver: handle non-existent files
2 parents 8181759 + cdf9912 commit f5f8120

File tree

5 files changed

+79
-7
lines changed

5 files changed

+79
-7
lines changed

lib/cc/engine/file_list_resolver.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ def expanded_list
2020
private
2121

2222
def absolute_include_paths
23-
@include_paths.map { |path| Pathname.new(path).realpath.to_s }
23+
@include_paths.map do |path|
24+
begin
25+
Pathname.new(path).realpath.to_s
26+
rescue Errno::ENOENT
27+
nil
28+
end
29+
end.compact
2430
end
2531

2632
def rubocop_file_to_include?(file)
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require "spec_helper"
2+
require "rubocop"
3+
require "cc/engine/file_list_resolver"
4+
5+
module CC::Engine
6+
describe FileListResolver do
7+
include FilesystemHelpers
8+
9+
before { @code = Dir.mktmpdir }
10+
let(:rubocop_config) { RuboCop::ConfigStore.new }
11+
12+
it "uses default include path" do
13+
Dir.chdir(@code) do
14+
create_source_file("a.rb", "def a; true; end")
15+
create_source_file("not_ruby.txt", "some text")
16+
17+
resolver = FileListResolver.new(root: @code, engine_config: {}, config_store: rubocop_config)
18+
expect(resolver.expanded_list).to eq [Pathname.new("a.rb").realpath.to_s]
19+
end
20+
end
21+
22+
it "finds ruby scripts without extensions" do
23+
Dir.chdir(@code) do
24+
create_source_file("a.rb", "def a; true; end")
25+
create_source_file("bin/some_script", "#!/usr/bin/env ruby")
26+
27+
resolver = FileListResolver.new(root: @code, engine_config: {}, config_store: rubocop_config)
28+
expect(resolver.expanded_list).to eq %w[a.rb bin/some_script].map { |fn| Pathname.new(fn).realpath.to_s }
29+
end
30+
end
31+
32+
it "respects engine config include_paths" do
33+
Dir.chdir(@code) do
34+
create_source_file("a.rb", "def a; true; end")
35+
create_source_file("src/b.rb", "def a; true; end")
36+
37+
resolver = FileListResolver.new(root: @code, engine_config: { "include_paths" => %w[src/] }, config_store: rubocop_config)
38+
expect(resolver.expanded_list).to eq [Pathname.new("src/b.rb").realpath.to_s]
39+
end
40+
end
41+
42+
it "respects rubocop excludes" do
43+
Dir.chdir(@code) do
44+
create_source_file("src/b.rb", "def a; true; end")
45+
create_source_file("src/c.rb", "def a; true; end")
46+
create_source_file(".rubocop.yml", "AllCops:\n Exclude:\n - src/c.rb")
47+
48+
resolver = FileListResolver.new(root: @code, engine_config: { "include_paths" => %w[src/] }, config_store: rubocop_config)
49+
expect(resolver.expanded_list).to eq [Pathname.new("src/b.rb").realpath.to_s]
50+
end
51+
end
52+
53+
it "handles missing files" do
54+
Dir.chdir(@code) do
55+
create_source_file("src/b.rb", "def a; true; end")
56+
57+
resolver = FileListResolver.new(root: @code, engine_config: { "include_paths" => %w[src/ public/assets] }, config_store: rubocop_config)
58+
expect(resolver.expanded_list).to eq [Pathname.new("src/b.rb").realpath.to_s]
59+
end
60+
end
61+
end
62+
end

spec/cc/engine/rubocop_spec.rb

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
module CC::Engine
66
describe Rubocop do
7+
include FilesystemHelpers
78
before { @code = Dir.mktmpdir }
89

910
describe "#run" do
@@ -333,12 +334,6 @@ def issues(output)
333334
output.split("\0").map { |x| JSON.parse(x) }
334335
end
335336

336-
def create_source_file(path, content)
337-
abs_path = File.join(@code, path)
338-
FileUtils.mkdir_p(File.dirname(abs_path))
339-
File.write(abs_path, content)
340-
end
341-
342337
def run_engine(config = nil)
343338
io = StringIO.new
344339
rubocop = Rubocop.new(@code, config, io)

spec/spec_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
require "rspec"
2+
3+
Dir.glob("spec/support/**/*.rb").each(&method(:load))

spec/support/filesystem_helpers.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module FilesystemHelpers
2+
def create_source_file(path, content)
3+
abs_path = File.join(@code, path)
4+
FileUtils.mkdir_p(File.dirname(abs_path))
5+
File.write(abs_path, content)
6+
end
7+
end

0 commit comments

Comments
 (0)