From 412ea1f93648e7ebf8865c4ede7d5974add7e59a Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Sun, 23 Aug 2020 21:23:35 -0600 Subject: [PATCH 1/2] Use warnings_logger instead of warnings_spy `warnings_spy` was a little library I wrote to report warnings that came up during test runs and fail the CI build. A while back I extracted it to `warnings_logger`, so the version that exists here is obsolete. --- Gemfile | 1 + Gemfile.lock | 2 + gemfiles/rails_4_2.gemfile | 1 + gemfiles/rails_4_2.gemfile.lock | 2 + gemfiles/rails_5_0.gemfile | 1 + gemfiles/rails_5_0.gemfile.lock | 2 + gemfiles/rails_5_1.gemfile | 1 + gemfiles/rails_5_1.gemfile.lock | 2 + gemfiles/rails_5_2.gemfile | 1 + gemfiles/rails_5_2.gemfile.lock | 2 + gemfiles/rails_6_0.gemfile | 1 + gemfiles/rails_6_0.gemfile.lock | 2 + spec/report_warnings.rb | 11 ++-- spec/warnings_spy.rb | 64 ----------------------- spec/warnings_spy/filesystem.rb | 45 ---------------- spec/warnings_spy/partitioner.rb | 36 ------------- spec/warnings_spy/reader.rb | 53 ------------------- spec/warnings_spy/reporter.rb | 88 -------------------------------- 18 files changed, 24 insertions(+), 291 deletions(-) delete mode 100644 spec/warnings_spy.rb delete mode 100644 spec/warnings_spy/filesystem.rb delete mode 100644 spec/warnings_spy/partitioner.rb delete mode 100644 spec/warnings_spy/reader.rb delete mode 100644 spec/warnings_spy/reporter.rb diff --git a/Gemfile b/Gemfile index 2a0bc09cf..20ae645f3 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem 'rake', '13.0.1' gem 'rspec', '~> 3.9' gem 'rubocop', require: false gem 'rubocop-rails', require: false +gem 'warnings_logger' gem 'zeus', require: false # YARD diff --git a/Gemfile.lock b/Gemfile.lock index 00ae6d980..6dbe283a0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,6 +54,7 @@ GEM ruby-progressbar (1.10.1) thor (0.20.0) unicode-display_width (1.6.0) + warnings_logger (0.1.1) yard (0.9.20) zeus (0.15.14) method_source (>= 0.6.7) @@ -73,6 +74,7 @@ DEPENDENCIES rspec (~> 3.9) rubocop rubocop-rails + warnings_logger yard zeus diff --git a/gemfiles/rails_4_2.gemfile b/gemfiles/rails_4_2.gemfile index e5bead64e..b90bb032c 100644 --- a/gemfiles/rails_4_2.gemfile +++ b/gemfiles/rails_4_2.gemfile @@ -10,6 +10,7 @@ gem "rake", "13.0.1" gem "rspec", "~> 3.9" gem "rubocop", require: false gem "rubocop-rails", require: false +gem "warnings_logger" gem "zeus", require: false gem "fssm" gem "pygments.rb" diff --git a/gemfiles/rails_4_2.gemfile.lock b/gemfiles/rails_4_2.gemfile.lock index e30b4ceb9..d736ee532 100644 --- a/gemfiles/rails_4_2.gemfile.lock +++ b/gemfiles/rails_4_2.gemfile.lock @@ -206,6 +206,7 @@ GEM uglifier (4.2.0) execjs (>= 0.3.0, < 3) unicode-display_width (1.6.0) + warnings_logger (0.1.1) yard (0.9.20) zeus (0.15.14) method_source (>= 0.6.7) @@ -243,6 +244,7 @@ DEPENDENCIES sqlite3 (~> 1.3.6) turbolinks uglifier (>= 1.3.0) + warnings_logger yard zeus diff --git a/gemfiles/rails_5_0.gemfile b/gemfiles/rails_5_0.gemfile index 91f2a1ae6..8295baca9 100644 --- a/gemfiles/rails_5_0.gemfile +++ b/gemfiles/rails_5_0.gemfile @@ -10,6 +10,7 @@ gem "rake", "13.0.1" gem "rspec", "~> 3.9" gem "rubocop", require: false gem "rubocop-rails", require: false +gem "warnings_logger" gem "zeus", require: false gem "fssm" gem "pygments.rb" diff --git a/gemfiles/rails_5_0.gemfile.lock b/gemfiles/rails_5_0.gemfile.lock index 7dc0d5925..714c4f7a7 100644 --- a/gemfiles/rails_5_0.gemfile.lock +++ b/gemfiles/rails_5_0.gemfile.lock @@ -197,6 +197,7 @@ GEM tzinfo (1.2.6) thread_safe (~> 0.1) unicode-display_width (1.6.0) + warnings_logger (0.1.1) websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.4) @@ -235,6 +236,7 @@ DEPENDENCIES spring-watcher-listen (~> 2.0.0) sqlite3 (~> 1.3.6) turbolinks (~> 5) + warnings_logger yard zeus diff --git a/gemfiles/rails_5_1.gemfile b/gemfiles/rails_5_1.gemfile index 7ff5c2c23..ebe601837 100644 --- a/gemfiles/rails_5_1.gemfile +++ b/gemfiles/rails_5_1.gemfile @@ -10,6 +10,7 @@ gem "rake", "13.0.1" gem "rspec", "~> 3.9" gem "rubocop", require: false gem "rubocop-rails", require: false +gem "warnings_logger" gem "zeus", require: false gem "fssm" gem "pygments.rb" diff --git a/gemfiles/rails_5_1.gemfile.lock b/gemfiles/rails_5_1.gemfile.lock index 41d6274b6..60e3819c0 100644 --- a/gemfiles/rails_5_1.gemfile.lock +++ b/gemfiles/rails_5_1.gemfile.lock @@ -210,6 +210,7 @@ GEM tzinfo (1.2.6) thread_safe (~> 0.1) unicode-display_width (1.6.0) + warnings_logger (0.1.1) websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.4) @@ -251,6 +252,7 @@ DEPENDENCIES spring-watcher-listen (~> 2.0.0) sqlite3 (~> 1.3.6) turbolinks (~> 5) + warnings_logger yard zeus diff --git a/gemfiles/rails_5_2.gemfile b/gemfiles/rails_5_2.gemfile index 6d38c91c2..c53dac15c 100644 --- a/gemfiles/rails_5_2.gemfile +++ b/gemfiles/rails_5_2.gemfile @@ -10,6 +10,7 @@ gem "rake", "13.0.1" gem "rspec", "~> 3.9" gem "rubocop", require: false gem "rubocop-rails", require: false +gem "warnings_logger" gem "zeus", require: false gem "fssm" gem "pygments.rb" diff --git a/gemfiles/rails_5_2.gemfile.lock b/gemfiles/rails_5_2.gemfile.lock index e43cb6d84..dd4f1fe19 100644 --- a/gemfiles/rails_5_2.gemfile.lock +++ b/gemfiles/rails_5_2.gemfile.lock @@ -227,6 +227,7 @@ GEM tzinfo (1.2.6) thread_safe (~> 0.1) unicode-display_width (1.6.0) + warnings_logger (0.1.1) websocket-driver (0.7.1) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.4) @@ -270,6 +271,7 @@ DEPENDENCIES spring-watcher-listen (~> 2.0.0) sqlite3 (~> 1.3.6) turbolinks (~> 5) + warnings_logger yard zeus diff --git a/gemfiles/rails_6_0.gemfile b/gemfiles/rails_6_0.gemfile index 9d22fa5df..f7d5c94d5 100644 --- a/gemfiles/rails_6_0.gemfile +++ b/gemfiles/rails_6_0.gemfile @@ -10,6 +10,7 @@ gem "rake", "13.0.1" gem "rspec", "~> 3.9" gem "rubocop", require: false gem "rubocop-rails", require: false +gem "warnings_logger" gem "zeus", require: false gem "fssm" gem "pygments.rb" diff --git a/gemfiles/rails_6_0.gemfile.lock b/gemfiles/rails_6_0.gemfile.lock index 0663be35e..e68a298a1 100644 --- a/gemfiles/rails_6_0.gemfile.lock +++ b/gemfiles/rails_6_0.gemfile.lock @@ -238,6 +238,7 @@ GEM tzinfo (1.2.6) thread_safe (~> 0.1) unicode-display_width (1.6.0) + warnings_logger (0.1.1) webdrivers (4.1.3) nokogiri (~> 1.6) rubyzip (>= 1.3.0) @@ -286,6 +287,7 @@ DEPENDENCIES spring-watcher-listen (~> 2.0.0) sqlite3 (~> 1.4) turbolinks (~> 5) + warnings_logger webdrivers yard zeus diff --git a/spec/report_warnings.rb b/spec/report_warnings.rb index b6f0fc080..f3bf4bc99 100644 --- a/spec/report_warnings.rb +++ b/spec/report_warnings.rb @@ -1,7 +1,8 @@ -require File.expand_path('../warnings_spy', __FILE__) +require 'warnings_logger' -# Adapted from +WarningsLogger.configure do |config| + config.project_name = 'shoulda-matchers' + config.project_directory = Pathname.new('..').expand_path(__dir__) +end -warnings_spy = WarningsSpy.new('shoulda-matchers') -warnings_spy.capture_warnings -warnings_spy.report_warnings_at_exit +WarningsLogger.enable diff --git a/spec/warnings_spy.rb b/spec/warnings_spy.rb deleted file mode 100644 index 9dae2dde2..000000000 --- a/spec/warnings_spy.rb +++ /dev/null @@ -1,64 +0,0 @@ -require 'forwardable' - -require File.expand_path('../warnings_spy/filesystem', __FILE__) -require File.expand_path('../warnings_spy/reader', __FILE__) -require File.expand_path('../warnings_spy/partitioner', __FILE__) -require File.expand_path('../warnings_spy/reporter', __FILE__) - -class WarningsSpy - extend Forwardable - - def initialize(project_name) - filesystem = Filesystem.new - @warnings_file = filesystem.warnings_file - @reader = Reader.new(filesystem) - @partitioner = Partitioner.new(reader, filesystem) - @reporter = Reporter.new(partitioner, filesystem, project_name) - end - - def capture_warnings - $stderr.reopen(warnings_file.path) - end - - def report_warnings_at_exit - at_exit do - printing_exceptions do - report_and_exit - end - end - end - - protected - - attr_reader :warnings_file, :reader, :partitioner, :reporter - - private - - def_delegators :partitioner, :relevant_warning_groups, - :irrelevant_warning_groups - - def report_and_exit - reader.read - partitioner.partition - reporter.report - fail_build_if_there_are_any_warnings - end - - def fail_build_if_there_are_any_warnings - if relevant_warning_groups.any? - exit(1) - end - end - - def printing_exceptions - begin - yield - rescue => error - puts "\n--- ERROR IN AT_EXIT --------------------------------" - puts "#{error.class}: #{error.message}" - puts error.backtrace.join("\n") - puts "-----------------------------------------------------" - raise error - end - end -end diff --git a/spec/warnings_spy/filesystem.rb b/spec/warnings_spy/filesystem.rb deleted file mode 100644 index 753068148..000000000 --- a/spec/warnings_spy/filesystem.rb +++ /dev/null @@ -1,45 +0,0 @@ -require 'fileutils' - -class WarningsSpy - class Filesystem - PROJECT_DIR = File.expand_path('../../..', __FILE__) - TEMP_DIR = File.join(PROJECT_DIR, 'tmp') - - def initialize - @files_by_name = Hash.new do |hash, name| - FileUtils.mkdir_p(TEMP_DIR) - hash[name] = file_for(name) - end - end - - def warnings_file - files_by_name['all_warnings'] - end - - def irrelevant_warnings_file - files_by_name['irrelevant_warnings'] - end - - def relevant_warnings_file - files_by_name['relevant_warnings'] - end - - def project_dir - PROJECT_DIR - end - - protected - - attr_reader :files_by_name - - private - - def path_for(name) - File.join(TEMP_DIR, "#{name}.txt") - end - - def file_for(name) - File.open(path_for(name), 'w+') - end - end -end diff --git a/spec/warnings_spy/partitioner.rb b/spec/warnings_spy/partitioner.rb deleted file mode 100644 index c6a705317..000000000 --- a/spec/warnings_spy/partitioner.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'forwardable' - -class WarningsSpy - class Partitioner - extend Forwardable - - attr_reader :relevant_warning_groups, :irrelevant_warning_groups - - def initialize(reader, filesystem) - @reader = reader - @search_text = filesystem.project_dir - end - - def partition - @relevant_warning_groups, @irrelevant_warning_groups = - warning_groups.partition { |group| relevant_warnings?(group) } - end - - protected - - attr_reader :reader, :search_text - - private - - def_delegators :reader, :warning_groups - - def relevant_warnings?(group) - first_line = group[0] - first_line.start_with?("#{project_root}/lib") - end - - def project_root - File.expand_path('../../..') - end - end -end diff --git a/spec/warnings_spy/reader.rb b/spec/warnings_spy/reader.rb deleted file mode 100644 index 5dfcde096..000000000 --- a/spec/warnings_spy/reader.rb +++ /dev/null @@ -1,53 +0,0 @@ -class WarningsSpy - class Reader - attr_reader :warning_groups - - def initialize(filesystem) - @warnings_file = filesystem.warnings_file - - @current_group = [] - @warning_groups = [] - end - - def read - warnings_file.rewind - - warnings_file.each_line do |line| - process_line(line) - end - - add_group(current_group) - end - - protected - - attr_reader :warnings_file, :current_group - - private - - def process_line(line) - if start_of_group?(line) - add_group(current_group) - @current_group = [] - end - - current_group << line - end - - def start_of_group?(line) - line =~ /^\S/ - end - - def add_group(group) - unless group.empty? || group_already_added?(group) - warning_groups << group - end - end - - def group_already_added?(group_to_be_added) - warning_groups.any? do |group| - group == group_to_be_added - end - end - end -end diff --git a/spec/warnings_spy/reporter.rb b/spec/warnings_spy/reporter.rb deleted file mode 100644 index 5eff50eb3..000000000 --- a/spec/warnings_spy/reporter.rb +++ /dev/null @@ -1,88 +0,0 @@ -require 'forwardable' - -class WarningsSpy - class Reporter - extend Forwardable - - def initialize(partitioner, filesystem, project_name) - @partitioner = partitioner - @warnings_file = filesystem.warnings_file - @relevant_warnings_file = filesystem.relevant_warnings_file - @irrelevant_warnings_file = filesystem.irrelevant_warnings_file - @project_name = project_name - end - - def report - reporting_all_groups do - report_relevant_warning_groups - report_irrelevant_warning_groups - end - end - - protected - - attr_reader :partitioner, :warnings_file, :relevant_warnings_file, - :irrelevant_warnings_file, :project_name - - private - - def_delegators :partitioner, :relevant_warning_groups, - :irrelevant_warning_groups - - def report_relevant_warning_groups - if relevant_warning_groups.any? - print_divider('-', 75, header: " #{project_name} warnings:") - relevant_warning_groups.each do |group| - group.each do |line| - relevant_warnings_file.puts(line) - puts line - end - end - print_divider('-', 75) - puts "#{project_name} warnings written to #{relevant_warnings_file.path}." - end - end - - def report_irrelevant_warning_groups - if irrelevant_warning_groups.any? - irrelevant_warning_groups.each do |group| - group.each do |line| - irrelevant_warnings_file.puts(line) - end - end - puts "Non #{project_name} warnings were raised during the test run. These have been written to #{irrelevant_warnings_file.path}." - end - end - - def reporting_all_groups - if relevant_warning_groups.any? || irrelevant_warning_groups.any? - puts - yield - puts "All warnings were written to #{warnings_file.path}." - puts - end - end - - def print_divider(character, count, options = {}) - puts - - if options[:header] - first_count = 10 - second_count = options[:header].length - first_count - string = - horizontal_rule(character, first_count) + - options[:header] + - horizontal_rule(character, second_count) - puts string - else - puts horizontal_rule(character, count) - end - - puts - end - - def horizontal_rule(character, count) - character * count - end - end -end From 6a0e43629008ad1f78388822ca46fbbd5432a65e Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Sun, 23 Aug 2020 22:27:37 -0600 Subject: [PATCH 2/2] Remove warnings that fail the build --- lib/shoulda/matchers/independent.rb | 1 - .../delegate_method_matcher/stubbed_target.rb | 37 ---------------- .../matchers/doublespeak/double_spec.rb | 1 + .../stubbed_target_spec.rb | 43 ------------------- .../delegate_method_matcher_spec.rb | 6 ++- 5 files changed, 5 insertions(+), 83 deletions(-) delete mode 100644 lib/shoulda/matchers/independent/delegate_method_matcher/stubbed_target.rb delete mode 100644 spec/unit/shoulda/matchers/independent/delegate_method_matcher/stubbed_target_spec.rb diff --git a/lib/shoulda/matchers/independent.rb b/lib/shoulda/matchers/independent.rb index 3996e0232..5ff4dca24 100644 --- a/lib/shoulda/matchers/independent.rb +++ b/lib/shoulda/matchers/independent.rb @@ -1,5 +1,4 @@ require 'shoulda/matchers/independent/delegate_method_matcher' -require 'shoulda/matchers/independent/delegate_method_matcher/stubbed_target' require 'shoulda/matchers/independent/delegate_method_matcher/target_not_defined_error' module Shoulda diff --git a/lib/shoulda/matchers/independent/delegate_method_matcher/stubbed_target.rb b/lib/shoulda/matchers/independent/delegate_method_matcher/stubbed_target.rb deleted file mode 100644 index c2c6f449b..000000000 --- a/lib/shoulda/matchers/independent/delegate_method_matcher/stubbed_target.rb +++ /dev/null @@ -1,37 +0,0 @@ -module Shoulda - module Matchers - module Independent - class DelegateMethodMatcher - # @private - class StubbedTarget - def initialize(method) - @received_method = false - @received_arguments = [] - stub_method(method) - end - - def has_received_method? - received_method - end - - def has_received_arguments?(*args) - args == received_arguments - end - - protected - - def stub_method(method) - class_eval do - define_method method do |*args| - @received_method = true - @received_arguments = args - end - end - end - - attr_reader :received_method, :received_arguments - end - end - end - end -end diff --git a/spec/unit/shoulda/matchers/doublespeak/double_spec.rb b/spec/unit/shoulda/matchers/doublespeak/double_spec.rb index 737e20da7..815829c6b 100644 --- a/spec/unit/shoulda/matchers/doublespeak/double_spec.rb +++ b/spec/unit/shoulda/matchers/doublespeak/double_spec.rb @@ -233,6 +233,7 @@ module Shoulda::Matchers::Doublespeak doubles[0].activate was_called = false + klass.__send__(:remove_method, method_name) klass.__send__(:define_method, method_name) do was_called = true end diff --git a/spec/unit/shoulda/matchers/independent/delegate_method_matcher/stubbed_target_spec.rb b/spec/unit/shoulda/matchers/independent/delegate_method_matcher/stubbed_target_spec.rb deleted file mode 100644 index f44dcbf52..000000000 --- a/spec/unit/shoulda/matchers/independent/delegate_method_matcher/stubbed_target_spec.rb +++ /dev/null @@ -1,43 +0,0 @@ -require 'unit_spec_helper' - -describe Shoulda::Matchers::Independent::DelegateMethodMatcher::StubbedTarget do - subject(:target) { described_class.new(:stubbed_method) } - - describe '#has_received_method?' do - it 'returns true when the method has been called on the target' do - target.stubbed_method - - expect(target).to have_received_method - end - - it 'returns false when the method has not been called on the target' do - expect(target).not_to have_received_method - end - end - - describe '#has_received_arguments?' do - context 'method is called with specified arguments' do - it 'returns true' do - target.stubbed_method(:arg1, :arg2) - - expect(target).to have_received_arguments(:arg1, :arg2) - end - end - - context 'method is not called with specified arguments' do - it 'returns false' do - target.stubbed_method - - expect(target).not_to have_received_arguments(:arg1) - end - end - - context 'method is called with arguments in incorrect order' do - it 'returns false' do - target.stubbed_method(:arg2, :arg1) - - expect(target).not_to have_received_arguments(:arg1, :arg2) - end - end - end -end diff --git a/spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb b/spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb index 80e952ece..d0106b4c6 100644 --- a/spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb @@ -418,7 +418,7 @@ def country person = Person.new expect { - expect(person).to delegate_method(:hello). to(:country).with_prefix + expect(person).to delegate_method(:hello).to(:country).with_prefix }.to fail_with_message(message) end end @@ -592,7 +592,9 @@ def country; end MESSAGE expectation = lambda do - expect(person).to delegate_method(:hello).to(:country).allow_nil + silence_warnings do + expect(person).to delegate_method(:hello).to(:country).allow_nil + end end expect(&expectation).to fail_with_message(message)