Skip to content

Add support for include_paths #5

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

Merged
merged 2 commits into from
Oct 7, 2015
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
40 changes: 40 additions & 0 deletions lib/cc/engine/analyzable_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module CC
module Engine
class AnalyzableFiles
def initialize(directory, config)
@directory = directory
@config = config
end

def all
@results ||= if @config["include_paths"]
build_files_with_inclusions(@config["include_paths"])
else
build_files_with_exclusions(@config["exclude_paths"] || [])
end
end

private

def fetch_files(paths)
paths.map do |path|
if path =~ %r{/$}
Dir.glob("#{path}/**/*.coffee")
else
path if path =~ /\.coffee$/
end
end.flatten.compact
end

def build_files_with_inclusions(inclusions)
fetch_files(inclusions)
end

def build_files_with_exclusions(exclusions)
files = Dir.glob("#{@directory}/**/*.coffee")
excluded_files = fetch_files(exclusions)
files.reject { |f| exclusions.include?(f) }
end
end
end
end
14 changes: 9 additions & 5 deletions lib/cc/engine/coffeelint.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "json"
require 'cc/engine/coffeelint_results'
require "cc/engine/coffeelint_results"
require "cc/engine/analyzable_files"

module CC
module Engine
Expand All @@ -13,7 +14,7 @@ def initialize(directory: , io: , engine_config: )
def run
coffeelint_results.each do |path, errors|
path = path.gsub(/\A\.\//, '')
unless exclude_path?(path)
if include_path?(path)
errors.each do |error|
issue = {
type: "Issue",
Expand All @@ -37,9 +38,12 @@ def run

private

def exclude_path?(path)
exclusions = @engine_config['exclude_paths'] || []
exclusions.include?(path)
def include_path?(path)
analyzable_files.include?(path)
end

def analyzable_files
@files ||= AnalyzableFiles.new(@directory, @engine_config).all
end

def coffeelint_results
Expand Down
73 changes: 47 additions & 26 deletions spec/cc/engine/coffeelint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
module CC::Engine
describe Coffeelint do
describe "#run" do
it "should process results" do
results = {
let(:mock_io) { double }
let(:result) do
{
'./cool.coffee' => [
{
"name" => "indentation",
Expand All @@ -19,41 +20,61 @@ module CC::Engine
}
]
}
allow_any_instance_of(CoffeelintResults).to receive(:results).
and_return(results)
mock_io = double

expect(mock_io).to receive(:print).
with("{\"type\":\"Issue\",\"description\":\"Line contains inconsistent indentation\",\"check_name\":\"indentation\",\"categories\":[\"Style\"],\"location\":{\"path\":\"cool.coffee\",\"lines\":{\"begin\":2,\"end\":2}},\"remediation_points\":250000}\u0000")

Coffeelint.new(directory: '.', io: mock_io, engine_config: {}).run
end

it "doesn't return excluded files" do
results = {
'./cool.coffee' => [
let(:second_result) do
{
'./uncool.coffee' => [
{
"name" => "indentation",
"name" => "max_line_length",
"value" => 2,
"level" => "error",
"message" => "Line contains inconsistent indentation",
"message" => "Line exceeds maximum allowed length",
"description" => "",
"context" => "Expected 2 got 4",
"context" => "Length is 114, max is 80",
"lineNumber" => 2,
"line" => " if num is 1",
"rule" => "indentation"
"rule" => "max_line_length"
}
]
}
allow_any_instance_of(CoffeelintResults).to receive(:results).
and_return(results)
mock_io = double
expect(mock_io).to receive(:print).never
Coffeelint.new(
directory: '.',
io: mock_io,
engine_config: {"exclude_paths" => ["cool.coffee"]}
).run
end

let(:results) { result.merge(second_result) }

it "should process results" do
allow_any_instance_of(CoffeelintResults).to receive(:results).and_return(result)
allow_any_instance_of(AnalyzableFiles).to receive(:all).and_return(["cool.coffee"])
expect(mock_io).to receive(:print).
with("{\"type\":\"Issue\",\"description\":\"Line contains inconsistent indentation\",\"check_name\":\"indentation\",\"categories\":[\"Style\"],\"location\":{\"path\":\"cool.coffee\",\"lines\":{\"begin\":2,\"end\":2}},\"remediation_points\":250000}\u0000")

Coffeelint.new(directory: '.', io: mock_io, engine_config: {}).run
end

describe "with exclude paths config" do
it "doesn't return excluded files" do
allow_any_instance_of(CoffeelintResults).to receive(:results).and_return(result)
expect(mock_io).to receive(:print).never

Coffeelint.new(
directory: '.',
io: mock_io,
engine_config: {"exclude_paths" => ["cool.coffee"]}
).run
end
end

describe "with include paths config" do
it "returns only included files" do
allow_any_instance_of(CoffeelintResults).to receive(:results).and_return(results)
expect(mock_io).to receive(:print).once

Coffeelint.new(
directory: '.',
io: mock_io,
engine_config: {"include_paths" => ["cool.coffee"]}
).run
end
end
end
end
Expand Down