diff --git a/lib/rubocop/extension/generator/generator.rb b/lib/rubocop/extension/generator/generator.rb index c9c30d7..1833b59 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' @@ -159,12 +155,20 @@ 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, all: true) + end + end end private def root_path diff --git a/smoke/smoke.rb b/smoke/smoke.rb index 15707af..e7b9772 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?('{') } + 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 + + if !unexpected_failures.empty? + raise "rspec failed. Unknown failures:\n#{unexpected_failures.join("\n")}" + end end