From fa2a5ffe8a93cc446d78deddf589a806d4d2046c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20T=C3=B3th?= Date: Mon, 26 Feb 2018 16:15:24 +0100 Subject: [PATCH] No longer used files removed (lib/linter/) Request in #406 to remove files in lib/linter/ which are not used --- lib/linter/base.rb | 128 ----------------------------------- lib/linter/haml.rb | 21 ------ lib/linter/rubocop.rb | 30 -------- lib/linter/yaml.rb | 58 ---------------- spec/lib/linter/yaml_spec.rb | 96 -------------------------- 5 files changed, 333 deletions(-) delete mode 100644 lib/linter/base.rb delete mode 100644 lib/linter/haml.rb delete mode 100644 lib/linter/rubocop.rb delete mode 100644 lib/linter/yaml.rb delete mode 100644 spec/lib/linter/yaml_spec.rb diff --git a/lib/linter/base.rb b/lib/linter/base.rb deleted file mode 100644 index 8167c10e..00000000 --- a/lib/linter/base.rb +++ /dev/null @@ -1,128 +0,0 @@ -module Linter - class Base - attr_reader :branch - delegate :logger, :to => :branch - - def initialize(branch) - @branch = branch - end - - def run - logger.info("#{log_header} Starting run...") - if files_to_lint.empty? - logger.info("#{log_header} Skipping run due to no candidate files.") - return - end - - require 'tempfile' - result = Dir.mktmpdir do |dir| - files = collected_config_files(dir) - if files.blank? - logger.error("#{log_header} Failed to run due to missing config files.") - return failed_linter_offenses("missing config files") - else - files += collected_files_to_lint(dir) - logger.info("#{log_header} Collected #{files.length} files.") - logger.debug { "#{log_header} File list: #{files.inspect}"} - run_linter(dir) - end - end - - offenses = parse_output(result.output) - logger.info("#{log_header} Completed run with #{offenses.fetch_path('summary', 'offense_count')} offenses") - logger.debug { "#{log_header} Offenses: #{offenses.inspect}" } - offenses - end - - private - - def parse_output(output) - JSON.parse(output.chomp) - rescue JSON::ParserError => error - logger.error("#{log_header} #{error.message}") - logger.error("#{log_header} Failed to parse JSON result #{output.inspect}") - return failed_linter_offenses("error parsing JSON result") - end - - def collected_config_files(dir) - config_files.select { |path| extract_file(path, dir, branch.pull_request?) } - end - - def collected_files_to_lint(dir) - files_to_lint.select { |path| extract_file(path, dir) } - end - - def extract_file(path, destination_dir, merged = false) - content = branch_service.content_at(path, merged) - return false unless content - - temp_file = File.join(destination_dir, path) - FileUtils.mkdir_p(File.dirname(temp_file)) - File.write(temp_file, content, :mode => "wb") # To prevent Encoding::UndefinedConversionError: "\xD0" from ASCII-8BIT to UTF-8 - true - end - - def branch_service - @branch_service ||= branch.git_service - end - - def diff_service - @diff_service ||= branch_service.diff - end - - def files_to_lint - @files_to_lint ||= begin - unfiltered_files = branch.pull_request? ? diff_service.new_files : branch.git_service.tip_files - filtered_files(unfiltered_files) - end - end - - def run_linter(dir) - logger.info("#{log_header} Executing linter...") - require 'awesome_spawn' - result = AwesomeSpawn.run(linter_executable, :params => options, :chdir => dir) - handle_linter_output(result) - end - - def handle_linter_output(result) - # rubocop exits 1 both when there are errors and when there are style issues. - # Instead of relying on just exit_status, we check if there is anything on stderr. - return result if result.exit_status.zero? || result.error.blank? - return FailedLinterRun.new(failed_linter_offenses("#{self.class.name} STDERR:\n```\n#{result.error}\n```")) if branch.pull_request? - raise result.error - end - - def failed_linter_offenses(message) - { - "files" => [ - { - "path" => "\\*\\*", - "offenses" => [ - { - "severity" => "fatal", - "message" => message, - "cop_name" => self.class.name.titleize - } - ] - } - ], - "summary" => { - "offense_count" => 1, - "target_file_count" => files_to_lint.length, - "inspected_file_count" => files_to_lint.length - } - } - end - - def log_header - "#{self.class.name} Repo: #{branch.repo.name} Branch #{branch.name} -" - end - - class FailedLinterRun - attr_reader :output - def initialize(message) - @output = message.to_json - end - end - end -end diff --git a/lib/linter/haml.rb b/lib/linter/haml.rb deleted file mode 100644 index 0ae2e1f4..00000000 --- a/lib/linter/haml.rb +++ /dev/null @@ -1,21 +0,0 @@ -module Linter - class Haml < Base - private - - def config_files - [".haml-lint.yml"] - end - - def linter_executable - 'haml-lint *' - end - - def options - {:reporter => 'json'} - end - - def filtered_files(files) - files.select { |file| file.end_with?(".haml") } - end - end -end diff --git a/lib/linter/rubocop.rb b/lib/linter/rubocop.rb deleted file mode 100644 index dcc255a7..00000000 --- a/lib/linter/rubocop.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'rubocop' - -module Linter - class Rubocop < Base - private - - def config_files - [".rubocop.yml", ".rubocop_base.yml", ".rubocop_local.yml"] - end - - def linter_executable - 'rubocop' - end - - def options - {:format => 'json', :no_display_cop_names => nil} - end - - def filtered_files(files) - files.select do |file| - file.end_with?(".rb") || - file.end_with?(".ru") || - file.end_with?(".rake") || - File.basename(file).in?(%w(Gemfile Rakefile)) - end.reject do |file| - file.end_with?("db/schema.rb") - end - end - end -end diff --git a/lib/linter/yaml.rb b/lib/linter/yaml.rb deleted file mode 100644 index 14321d77..00000000 --- a/lib/linter/yaml.rb +++ /dev/null @@ -1,58 +0,0 @@ -module Linter - class Yaml < Base - private - - def parse_output(output) - lines = output.chomp.split("\n") - parsed = lines.collect { |line| line_to_hash(line) } - grouped = parsed.group_by { |hash| hash["filename"] } - file_count = parsed.collect { |hash| hash["filename"] }.uniq.count - { - "files" => grouped.collect do |filename, offenses| - { - "path" => filename.sub(%r{\A\./}, ""), - "offenses" => offenses.collect { |offense_hash| offense_hash.except("filename") } - } - end, - "summary" => { - "offense_count" => lines.size, - "target_file_count" => file_count, - "inspected_file_count" => file_count - } - } - end - - def linter_executable - "yamllint" - end - - def config_files - [".yamllint"] - end - - def options - {:f => "parsable", nil => ["."]} - end - - def filtered_files(files) - files.select { |f| f.end_with?(".yml", ".yaml") } - end - - def line_to_hash(line) - filename, line, column, severity_message_cop = line.split(":", 4) - severity_message, cop = severity_message_cop.split(/ \((.*)\)\Z/) - severity, message = severity_message.match(/\[(.*)\] (.*)/).captures - - { - "filename" => filename, - "severity" => severity, - "message" => message, - "cop_name" => cop, - "location" => { - "line" => line.to_i, - "column" => column.to_i - } - } - end - end -end diff --git a/spec/lib/linter/yaml_spec.rb b/spec/lib/linter/yaml_spec.rb deleted file mode 100644 index 3570fe32..00000000 --- a/spec/lib/linter/yaml_spec.rb +++ /dev/null @@ -1,96 +0,0 @@ -RSpec.describe Linter::Yaml do - describe "#parse_output" do - it "formats the output in the same form as rubocop" do - output = <<-EOOUTPUT -config/settings.yml:8:5: [warning] wrong indentation: expected 2 but found 4 (indentation) -config/settings.yml:11:1: [error] duplication of key ":a" in mapping (key-duplicates) -lib/generators/provider/templates/config/settings.yml:8:15: [error] syntax error: could not find expected ':' -EOOUTPUT - - actual = described_class.new(double("branch")).send(:parse_output, output) - - expected = { - "files" => [ - { - "path" => "config/settings.yml", - "offenses" => a_collection_containing_exactly( - { - "severity" => "warning", - "message" => "wrong indentation: expected 2 but found 4", - "cop_name" => "indentation", - "location" => { - "line" => 8, - "column" => 5 - } - }, - { - "severity" => "error", - "message" => "duplication of key \":a\" in mapping", - "cop_name" => "key-duplicates", - "location" => { - "line" => 11, - "column" => 1 - } - } - ) - }, - { - "path" => "lib/generators/provider/templates/config/settings.yml", - "offenses" => [ - { - "severity" => "error", - "message" => "syntax error: could not find expected ':'", - "cop_name" => nil, - "location" => { - "line" => 8, - "column" => 15 - } - }, - ] - } - ], - "summary" => { - "offense_count" => 3, - "target_file_count" => 2, - "inspected_file_count" => 2 - }, - } - expect(actual).to include(expected) - end - end - - describe "#line_to_hash" do - it "with a syntax error" do - line = "./lib/generators/provider/templates/config/settings.yml:8:15: [error] syntax error: could not find expected ':'" - expect(described_class.new(double).send(:line_to_hash, line)).to eq( - "cop_name" => nil, - "filename" => "./lib/generators/provider/templates/config/settings.yml", - "location" => {"line" => 8, "column" => 15}, - "message" => "syntax error: could not find expected ':'", - "severity" => "error" - ) - end - - it "with an indentation warning" do - line = "config/settings.yml:8:5: [warning] wrong indentation: expected 2 but found 4 (indentation)" - expect(described_class.new(double).send(:line_to_hash, line)).to eq( - "cop_name" => "indentation", - "filename" => "config/settings.yml", - "location" => {"line" => 8, "column" => 5}, - "message" => "wrong indentation: expected 2 but found 4", - "severity" => "warning" - ) - end - - it "with a duplicate key error" do - line = "config/settings.yml:11:1: [error] duplication of key \":a\" in mapping (key-duplicates)" - expect(described_class.new(double).send(:line_to_hash, line)).to eq( - "cop_name" => "key-duplicates", - "filename" => "config/settings.yml", - "location" => {"line" => 11, "column" => 1}, - "message" => "duplication of key \":a\" in mapping", - "severity" => "error" - ) - end - end -end