Skip to content

Commit

Permalink
Merge pull request #56 from codeclimate/will/resolve-nonexistent-files
Browse files Browse the repository at this point in the history
FileListResolver: handle non-existent files
  • Loading branch information
wfleming committed Feb 17, 2016
2 parents 8181759 + cdf9912 commit f5f8120
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
8 changes: 7 additions & 1 deletion lib/cc/engine/file_list_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ def expanded_list
private

def absolute_include_paths
@include_paths.map { |path| Pathname.new(path).realpath.to_s }
@include_paths.map do |path|
begin
Pathname.new(path).realpath.to_s
rescue Errno::ENOENT
nil
end
end.compact
end

def rubocop_file_to_include?(file)
Expand Down
62 changes: 62 additions & 0 deletions spec/cc/engine/file_list_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "spec_helper"
require "rubocop"
require "cc/engine/file_list_resolver"

module CC::Engine
describe FileListResolver do
include FilesystemHelpers

before { @code = Dir.mktmpdir }
let(:rubocop_config) { RuboCop::ConfigStore.new }

it "uses default include path" do
Dir.chdir(@code) do
create_source_file("a.rb", "def a; true; end")
create_source_file("not_ruby.txt", "some text")

resolver = FileListResolver.new(root: @code, engine_config: {}, config_store: rubocop_config)
expect(resolver.expanded_list).to eq [Pathname.new("a.rb").realpath.to_s]
end
end

it "finds ruby scripts without extensions" do
Dir.chdir(@code) do
create_source_file("a.rb", "def a; true; end")
create_source_file("bin/some_script", "#!/usr/bin/env ruby")

resolver = FileListResolver.new(root: @code, engine_config: {}, config_store: rubocop_config)
expect(resolver.expanded_list).to eq %w[a.rb bin/some_script].map { |fn| Pathname.new(fn).realpath.to_s }
end
end

it "respects engine config include_paths" do
Dir.chdir(@code) do
create_source_file("a.rb", "def a; true; end")
create_source_file("src/b.rb", "def a; true; end")

resolver = FileListResolver.new(root: @code, engine_config: { "include_paths" => %w[src/] }, config_store: rubocop_config)
expect(resolver.expanded_list).to eq [Pathname.new("src/b.rb").realpath.to_s]
end
end

it "respects rubocop excludes" do
Dir.chdir(@code) do
create_source_file("src/b.rb", "def a; true; end")
create_source_file("src/c.rb", "def a; true; end")
create_source_file(".rubocop.yml", "AllCops:\n Exclude:\n - src/c.rb")

resolver = FileListResolver.new(root: @code, engine_config: { "include_paths" => %w[src/] }, config_store: rubocop_config)
expect(resolver.expanded_list).to eq [Pathname.new("src/b.rb").realpath.to_s]
end
end

it "handles missing files" do
Dir.chdir(@code) do
create_source_file("src/b.rb", "def a; true; end")

resolver = FileListResolver.new(root: @code, engine_config: { "include_paths" => %w[src/ public/assets] }, config_store: rubocop_config)
expect(resolver.expanded_list).to eq [Pathname.new("src/b.rb").realpath.to_s]
end
end
end
end
7 changes: 1 addition & 6 deletions spec/cc/engine/rubocop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module CC::Engine
describe Rubocop do
include FilesystemHelpers
before { @code = Dir.mktmpdir }

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

def create_source_file(path, content)
abs_path = File.join(@code, path)
FileUtils.mkdir_p(File.dirname(abs_path))
File.write(abs_path, content)
end

def run_engine(config = nil)
io = StringIO.new
rubocop = Rubocop.new(@code, config, io)
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
require "rspec"

Dir.glob("spec/support/**/*.rb").each(&method(:load))
7 changes: 7 additions & 0 deletions spec/support/filesystem_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module FilesystemHelpers
def create_source_file(path, content)
abs_path = File.join(@code, path)
FileUtils.mkdir_p(File.dirname(abs_path))
File.write(abs_path, content)
end
end

0 comments on commit f5f8120

Please sign in to comment.