From e907de57371ffc2fba5c55f2c25964a88f9d1ff8 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Sat, 2 Apr 2016 19:40:39 +0200 Subject: [PATCH] fix all remaining rubocop offenses --- .hound.yml | 3 - .../rspec/inspectors/focused_inspector.rb | 4 +- lib/guard/rspec/options.rb | 2 +- lib/guard/rspec/rspec_process.rb | 2 - lib/guard/rspec/runner.rb | 2 +- lib/guard/rspec/version.rb | 2 +- lib/guard/rspec_defaults.rb | 2 +- lib/guard/rspec_formatter.rb | 81 +++++++++++-------- .../inspectors/keeping_inspector_spec.rb | 34 +++++--- spec/lib/guard/rspec/results_spec.rb | 2 +- spec/lib/guard/rspec/runner_spec.rb | 10 ++- 11 files changed, 83 insertions(+), 61 deletions(-) diff --git a/.hound.yml b/.hound.yml index 45b43016..14f6d1d7 100644 --- a/.hound.yml +++ b/.hound.yml @@ -196,9 +196,6 @@ StringLiterals: VariableInterpolation: Enabled: false -TrailingComma: - Enabled: false - TrivialAccessors: Enabled: false diff --git a/lib/guard/rspec/inspectors/focused_inspector.rb b/lib/guard/rspec/inspectors/focused_inspector.rb index f1fddb27..acdbad43 100644 --- a/lib/guard/rspec/inspectors/focused_inspector.rb +++ b/lib/guard/rspec/inspectors/focused_inspector.rb @@ -25,8 +25,8 @@ def paths(paths) def failed(locations) if locations.empty? @focused_locations = [] - else - @focused_locations = locations if focused_locations.empty? + elsif focused_locations.empty? + @focused_locations = locations end end diff --git a/lib/guard/rspec/options.rb b/lib/guard/rspec/options.rb index 32206190..bd99c877 100644 --- a/lib/guard/rspec/options.rb +++ b/lib/guard/rspec/options.rb @@ -12,7 +12,7 @@ module Options launchy: nil, notification: true, title: "RSpec results" - } + }.freeze class << self def with_defaults(options = {}) diff --git a/lib/guard/rspec/rspec_process.rb b/lib/guard/rspec/rspec_process.rb index c5fcc9c7..741f5124 100644 --- a/lib/guard/rspec/rspec_process.rb +++ b/lib/guard/rspec/rspec_process.rb @@ -56,8 +56,6 @@ def _without_bundler_env end end - private - attr_reader :command attr_reader :exit_code attr_reader :formatter_tmp_file diff --git a/lib/guard/rspec/runner.rb b/lib/guard/rspec/runner.rb index 6b6ab56d..59d67bd1 100644 --- a/lib/guard/rspec/runner.rb +++ b/lib/guard/rspec/runner.rb @@ -16,7 +16,7 @@ def initialize end # NOTE: must match with const in RSpecFormatter! - TEMPORARY_FILE_PATH ||= "tmp/rspec_guard_result" + TEMPORARY_FILE_PATH ||= "tmp/rspec_guard_result".freeze attr_accessor :options, :inspector, :notifier diff --git a/lib/guard/rspec/version.rb b/lib/guard/rspec/version.rb index 53d687b9..0fc1d42c 100644 --- a/lib/guard/rspec/version.rb +++ b/lib/guard/rspec/version.rb @@ -1,5 +1,5 @@ module Guard module RSpecVersion - VERSION = "4.6.4" + VERSION = "4.6.4".freeze end end diff --git a/lib/guard/rspec_defaults.rb b/lib/guard/rspec_defaults.rb index e2a49186..50ff4baa 100644 --- a/lib/guard/rspec_defaults.rb +++ b/lib/guard/rspec_defaults.rb @@ -1,5 +1,5 @@ module Guard class RSpecDefaults - TEMPORARY_FILE_PATH = "tmp/rspec_guard_result" + TEMPORARY_FILE_PATH = "tmp/rspec_guard_result".freeze end end diff --git a/lib/guard/rspec_formatter.rb b/lib/guard/rspec_formatter.rb index e1ec0fc8..a1f8b880 100644 --- a/lib/guard/rspec_formatter.rb +++ b/lib/guard/rspec_formatter.rb @@ -12,13 +12,17 @@ module Guard class RSpecFormatter < ::RSpec::Core::Formatters::BaseFormatter WIKI_ENV_WARN_URL = - "https://github.com/guard/guard-rspec/wiki/Warning:-no-environment" + "https://github.com/guard/guard-rspec/wiki/Warning:-no-environment".freeze - NO_ENV_WARNING_MSG = "no environment passed - see #{WIKI_ENV_WARN_URL}" - NO_RESULTS_VALUE_MSG = ":results_file value unknown (using defaults)" + NO_ENV_WARNING_MSG = + "no environment passed - see #{WIKI_ENV_WARN_URL}".freeze - UNSUPPORTED_PATTERN = "Your RSpec.configuration.pattern uses characters "\ - "unsupported by your Ruby version (File::FNM_EXTGLOB is undefined)" + NO_RESULTS_VALUE_MSG = + ":results_file value unknown (using defaults)".freeze + + UNSUPPORTED_PATTERN = + "Your RSpec.configuration.pattern uses characters "\ + "unsupported by your Ruby version (File::FNM_EXTGLOB is undefined)".freeze class Error < RuntimeError class UnsupportedPattern < Error @@ -44,41 +48,54 @@ def examples end end - # rspec issue https://github.com/rspec/rspec-core/issues/793 - def self.extract_spec_location(metadata) - root_metadata = metadata - location = metadata[:location] + class << self + # rspec issue https://github.com/rspec/rspec-core/issues/793 + def extract_spec_location(metadata) + root_metadata = metadata + location = metadata[:location] + + until spec_path?(location) + metadata = metadata[:parent_example_group] || metadata[:example_group] - until spec_path?(location) - metadata = metadata[:parent_example_group] || metadata[:example_group] + unless metadata + STDERR.puts "no spec file location in #{root_metadata.inspect}" + return root_metadata[:location] + end - unless metadata - STDERR.puts "no spec file location in #{root_metadata.inspect}" - return root_metadata[:location] + # rspec issue https://github.com/rspec/rspec-core/issues/1243 + location = first_colon_separated_entry(metadata[:location]) end - # rspec issue https://github.com/rspec/rspec-core/issues/1243 - location = (metadata[:location] || "").split(":").first + location end - location - end + def spec_path?(path) + pattern = ::RSpec.configuration.pattern + + flags = supported_fnmatch_flags(pattern) + path ||= "" + path = path.sub(/:\d+\z/, "") + path = Pathname.new(path).cleanpath.to_s + stripped = "{#{pattern.gsub(/\s*,\s*/, ',')}}" + File.fnmatch(stripped, path, flags) + end - def self.spec_path?(path) - pattern = ::RSpec.configuration.pattern + private - flags = File::FNM_PATHNAME | File::FNM_DOTMATCH - if File.const_defined?(:FNM_EXTGLOB) # ruby >= 2 - flags |= File::FNM_EXTGLOB - elsif pattern =~ /[{}]/ - fail Error::UnsupportedPattern + def first_colon_separated_entry(entries) + (entries || "").split(":").first end - path ||= "" - path = path.sub(/:\d+\z/, "") - path = Pathname.new(path).cleanpath.to_s - stripped = "{#{pattern.gsub(/\s*,\s*/, ',')}}" - File.fnmatch(stripped, path, flags) + def supported_fnmatch_flags(pattern) + flags = File::FNM_PATHNAME | File::FNM_DOTMATCH + + # ruby >= 2 + return flags |= File::FNM_EXTGLOB if File.const_defined?(:FNM_EXTGLOB) + + fail Error::UnsupportedPattern if pattern =~ /[{}]/ + + flags + end end def dump_summary(*args) @@ -135,8 +152,8 @@ def _status_failed?(example) def _results_file path = ENV["GUARD_RSPEC_RESULTS_FILE"] if path.nil? - STDERR.puts "Guard::RSpec: Warning: #{NO_ENV_WARNING_MSG}\n" \ - "Guard::RSpec: Warning: #{NO_RESULTS_VALUE_MSG}" + STDERR.puts("Guard::RSpec: Warning: #{NO_ENV_WARNING_MSG}\n" \ + "Guard::RSpec: Warning: #{NO_RESULTS_VALUE_MSG}") path = RSpecDefaults::TEMPORARY_FILE_PATH end diff --git a/spec/lib/guard/rspec/inspectors/keeping_inspector_spec.rb b/spec/lib/guard/rspec/inspectors/keeping_inspector_spec.rb index 3694d862..475702a6 100644 --- a/spec/lib/guard/rspec/inspectors/keeping_inspector_spec.rb +++ b/spec/lib/guard/rspec/inspectors/keeping_inspector_spec.rb @@ -58,11 +58,13 @@ # Line numbers in failed_locations needs to be omitted because of # https://github.com/rspec/rspec-core/issues/952 - expect(inspector.paths(other_paths)).to match_array([ - "spec/lib/guard/rspec/deprecator_spec.rb", - "spec/lib/guard/rspec/runner_spec.rb", - "spec/lib/guard/rspec/inspectors/simple_inspector_spec.rb" - ]) + expect(inspector.paths(other_paths)).to match_array( + [ + "spec/lib/guard/rspec/deprecator_spec.rb", + "spec/lib/guard/rspec/runner_spec.rb", + "spec/lib/guard/rspec/inspectors/simple_inspector_spec.rb" + ] + ) inspector.failed(other_failed_locations) # Now it returns other failed locations @@ -70,18 +72,24 @@ inspector.paths( %w(spec/lib/guard/rspec/inspectors/base_inspector_spec.rb) ) - ).to match_array([ - "spec/lib/guard/rspec/runner_spec.rb", - "spec/lib/guard/rspec/inspectors/simple_inspector_spec.rb", - "spec/lib/guard/rspec/inspectors/base_inspector_spec.rb" - ]) + ).to match_array( + [ + "spec/lib/guard/rspec/runner_spec.rb", + "spec/lib/guard/rspec/inspectors/simple_inspector_spec.rb", + "spec/lib/guard/rspec/inspectors/base_inspector_spec.rb" + ] + ) inspector.failed(other_failed_locations) - expect(inspector.paths(%w(spec/lib/guard/rspec/runner_spec.rb))). - to match_array([ + expect( + inspector.paths(%w(spec/lib/guard/rspec/runner_spec.rb)) + ).to match_array( + [ "spec/lib/guard/rspec/runner_spec.rb", "spec/lib/guard/rspec/inspectors/simple_inspector_spec.rb" - ]) + ] + ) + inspector.failed([]) # Now there is no failed locations diff --git a/spec/lib/guard/rspec/results_spec.rb b/spec/lib/guard/rspec/results_spec.rb index 1997ecaa..07bc792d 100644 --- a/spec/lib/guard/rspec/results_spec.rb +++ b/spec/lib/guard/rspec/results_spec.rb @@ -16,7 +16,7 @@ [ "5 examples, 2 failures (3 pending)\n", "foo1/bar1_spec.rb\n", - "foo1/bar2_spec.rb\n", + "foo1/bar2_spec.rb\n" ] end diff --git a/spec/lib/guard/rspec/runner_spec.rb b/spec/lib/guard/rspec/runner_spec.rb index e444fb53..2ebf5c88 100644 --- a/spec/lib/guard/rspec/runner_spec.rb +++ b/spec/lib/guard/rspec/runner_spec.rb @@ -272,10 +272,12 @@ context "with failed paths" do before do - allow(results).to receive(:failed_paths).and_return([ - "./failed_spec.rb:123", - "./other/failed_spec.rb:77" - ]) + allow(results).to receive(:failed_paths).and_return( + [ + "./failed_spec.rb:123", + "./other/failed_spec.rb:77" + ] + ) end it "notifies inspector about failed paths" do