From db3b8b3e7bedeca834ce9cac01f9b259f6c926a4 Mon Sep 17 00:00:00 2001 From: Zach Margolis Date: Wed, 22 May 2024 16:52:29 -0700 Subject: [PATCH 1/2] Fix error with RuboCop capitalization in specs - Update smoke tests to run, and pass for known failures --- lib/rubocop/extension/generator/generator.rb | 18 +++++++++++------- smoke/smoke.rb | 20 +++++++++++++++++++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/rubocop/extension/generator/generator.rb b/lib/rubocop/extension/generator/generator.rb index c9c30d7..8a45d86 100644 --- a/lib/rubocop/extension/generator/generator.rb +++ b/lib/rubocop/extension/generator/generator.rb @@ -95,10 +95,6 @@ def self.defaults! end RUBY - patch "lib/#{dirname}.rb", 'module Rubocop', 'module RuboCop' - patch "lib/#{dirname}/version.rb", 'module Rubocop', 'module RuboCop' - patch "#{name}.gemspec", 'Rubocop', 'RuboCop' - patch "#{name}.gemspec", /^end/, <<~RUBY spec.add_runtime_dependency 'rubocop' @@ -133,9 +129,9 @@ def self.defaults! end RUBY - patch 'Gemfile', /\z/, <<~RUBY - gem 'rspec' - RUBY + patch_all "**/*.rb", 'Rubocop', 'RuboCop' + + patch "#{name}.gemspec", 'Rubocop', 'RuboCop' if Gem::Version.new(Bundler::VERSION) >= Gem::Version.new('2.3.9') patch 'README.md', /\$ bundle add (.*)$/, '$ bundle add \1 --require=false' @@ -167,6 +163,14 @@ def self.defaults! path.write file.sub(pattern, replacement) end + private def patch_all(glob, pattern, replacement) + Dir[root_path / glob].each do |path| + if File.read(path).match(pattern) + patch(path.sub(root_path.to_s, '').sub(%r|^/|, ''), pattern, replacement) + end + end + end + private def root_path @root_path ||= Pathname(name) end diff --git a/smoke/smoke.rb b/smoke/smoke.rb index 15707af..a310886 100644 --- a/smoke/smoke.rb +++ b/smoke/smoke.rb @@ -2,6 +2,8 @@ require 'tmpdir' require 'pathname' +require 'open3' +require 'json' exe_path = File.expand_path('../exe/rubocop-extension-generator', __dir__) load_path = File.expand_path('../lib', __dir__) @@ -24,5 +26,21 @@ system('bundle', 'install', exception: true, chdir: gem_dir) system('bundle', 'exec', 'rake', 'new_cop[Smoke/Foo]', exception: true, chdir: gem_dir) - system('bundle', 'exec', 'rake', 'spec', exception: true, chdir: gem_dir) + + stdout, status = Open3.capture2( + { 'SPEC_OPTS' => '--format json' }, + *['bundle', 'exec', 'rake', 'spec'], + chdir: gem_dir, + ) + stdout_json = stdout.lines.find { |l| l.start_with?('{') } + failures = JSON.parse(stdout_json)['examples'].select { |example| example['status'] == 'failed' } + + unexpected_failures = failures.map { |failure| failure['full_description'] } - [ + 'RuboCop::Smoke has a version number', + 'RuboCop::Smoke does something useful', + 'RuboCop::Cop::Smoke::Foo registers an offense when using `#bad_method`', + ] + if !unexpected_failures.empty? + raise "rspec failed. Unknown failures:\n#{unexpected_failures.join("\n")}" + end end From fac4ac755f0c1fe3b92b0a4321f0b25b86273b5a Mon Sep 17 00:00:00 2001 From: Zach Margolis Date: Wed, 22 May 2024 17:06:55 -0700 Subject: [PATCH 2/2] - Add all: option to patch to replace all instances via gsub - Update rspec JSON parsing to be a little smarter and format better descriptions --- lib/rubocop/extension/generator/generator.rb | 6 +++--- smoke/smoke.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/rubocop/extension/generator/generator.rb b/lib/rubocop/extension/generator/generator.rb index 8a45d86..1833b59 100644 --- a/lib/rubocop/extension/generator/generator.rb +++ b/lib/rubocop/extension/generator/generator.rb @@ -155,18 +155,18 @@ def self.defaults! path.write(content) end - private def patch(path, pattern, replacement) + private def patch(path, pattern, replacement, all: false) puts "update #{path}" path = root_path / path file = path.read raise "Cannot apply patch for #{path} because #{pattern} is missing" unless file.match?(pattern) - path.write file.sub(pattern, replacement) + path.write(all ? file.gsub(pattern, replacement) : file.sub(pattern, replacement)) end private def patch_all(glob, pattern, replacement) Dir[root_path / glob].each do |path| if File.read(path).match(pattern) - patch(path.sub(root_path.to_s, '').sub(%r|^/|, ''), pattern, replacement) + patch(path.sub(root_path.to_s, '').sub(%r|^/|, ''), pattern, replacement, all: true) end end end diff --git a/smoke/smoke.rb b/smoke/smoke.rb index a310886..e7b9772 100644 --- a/smoke/smoke.rb +++ b/smoke/smoke.rb @@ -33,13 +33,13 @@ chdir: gem_dir, ) stdout_json = stdout.lines.find { |l| l.start_with?('{') } - failures = JSON.parse(stdout_json)['examples'].select { |example| example['status'] == 'failed' } + unexpected_failures = JSON.parse(stdout_json)['examples']. + select { |example| example['status'] != 'passed' }. + select { |example| example.dig('exception', 'class') != 'RSpec::Expectations::ExpectationNotMetError' }. + map do |example| + [example['full_description'], example.dig('exception', 'message')].join(': ') + end - unexpected_failures = failures.map { |failure| failure['full_description'] } - [ - 'RuboCop::Smoke has a version number', - 'RuboCop::Smoke does something useful', - 'RuboCop::Cop::Smoke::Foo registers an offense when using `#bad_method`', - ] if !unexpected_failures.empty? raise "rspec failed. Unknown failures:\n#{unexpected_failures.join("\n")}" end