From dcc0e8e05901d601a61f3b55b8d5aee002b9d133 Mon Sep 17 00:00:00 2001 From: Jacek Kubiak Date: Mon, 28 Sep 2015 17:16:50 +0200 Subject: [PATCH 1/2] Create a separate class which finds analyzable files --- lib/cc/engine/analyzable_files.rb | 40 +++++++++++++++++++++++++++++++ lib/cc/engine/coffeelint.rb | 14 +++++++---- 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 lib/cc/engine/analyzable_files.rb diff --git a/lib/cc/engine/analyzable_files.rb b/lib/cc/engine/analyzable_files.rb new file mode 100644 index 0000000..0a3a3f1 --- /dev/null +++ b/lib/cc/engine/analyzable_files.rb @@ -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 diff --git a/lib/cc/engine/coffeelint.rb b/lib/cc/engine/coffeelint.rb index 99123ef..057b78e 100644 --- a/lib/cc/engine/coffeelint.rb +++ b/lib/cc/engine/coffeelint.rb @@ -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 @@ -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", @@ -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 From 83829e740c6e99969ddc3e4a60ae4402f7a1ca5a Mon Sep 17 00:00:00 2001 From: Jacek Kubiak Date: Mon, 28 Sep 2015 17:17:00 +0200 Subject: [PATCH 2/2] Add a spec --- spec/cc/engine/coffeelint_spec.rb | 73 ++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/spec/cc/engine/coffeelint_spec.rb b/spec/cc/engine/coffeelint_spec.rb index 6499d11..9deeace 100644 --- a/spec/cc/engine/coffeelint_spec.rb +++ b/spec/cc/engine/coffeelint_spec.rb @@ -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", @@ -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