From d8a0e990d81031af61d1557c78234a3f43f7e869 Mon Sep 17 00:00:00 2001 From: Nurahmadie Date: Thu, 9 Apr 2020 06:57:58 +0700 Subject: [PATCH 1/2] Fix gem generation and smoke test run. The newer version of bundle (e.g. 2.1.4) seems to have new option to automatically include either rspec or minitest, e.g. via: ``` BUNDLE_GEM__TEST: "rspec" ``` Check existing `gem 'rspec'` declaration and skip Gemfile patching if it's exist. Also fix smoke test to patch the spec first so we can have success rspec run again. --- lib/rubocop/extension/generator/generator.rb | 16 ++++++++++++---- smoke/smoke.rb | 9 ++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/rubocop/extension/generator/generator.rb b/lib/rubocop/extension/generator/generator.rb index 667ed3e..04116d6 100644 --- a/lib/rubocop/extension/generator/generator.rb +++ b/lib/rubocop/extension/generator/generator.rb @@ -98,6 +98,7 @@ def self.defaults! patch "lib/#{dirname}.rb", 'module Rubocop', 'module RuboCop' patch "lib/#{dirname}/version.rb", 'module Rubocop', 'module RuboCop' patch "#{name}.gemspec", 'Rubocop', 'RuboCop' + patch "spec/#{dirname}_spec.rb", 'Rubocop::', 'RuboCop::' patch "#{name}.gemspec", /^end/, <<~RUBY @@ -136,9 +137,7 @@ def self.defaults! end RUBY - patch 'Gemfile', /\z/, <<~RUBY - gem 'rspec' - RUBY + patch_gemfile patch 'README.md', /^gem '#{name}'$/, "gem '#{name}', require: false" @@ -151,6 +150,15 @@ def self.defaults! MESSAGE end + private def patch_gemfile + path = root_path / 'Gemfile' + file = path.read + return if file =~ (/gem ('|")rspec('|")/) + patch 'Gemfile', /\z/, <<~RUBY + gem 'rspec' + RUBY + end + private def put(path, content) path = root_path / path puts "create #{path}" @@ -163,7 +171,7 @@ def self.defaults! 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 file.gsub(pattern, replacement) end private def root_path diff --git a/smoke/smoke.rb b/smoke/smoke.rb index 15707af..485ba94 100644 --- a/smoke/smoke.rb +++ b/smoke/smoke.rb @@ -9,6 +9,7 @@ Dir.mktmpdir('-rubocop-extension-generator-smoke') do |base_dir| base_dir = Pathname(base_dir) gem_name = 'rubocop-smoke' + gem_namespace = gem_name.sub('-', '/') gem_dir = base_dir / gem_name system({ 'RUBYLIB' => load_path }, 'ruby', exe_path, gem_name, exception: true, chdir: base_dir) @@ -22,7 +23,13 @@ gemspec_path.write gemspec + spec = gem_dir / "spec/#{gem_namespace}_spec.rb" + specfile = spec.read + specfile.gsub!('expect(false).to eq(true)', 'expect(true).to eq(true)') + + spec.write specfile + 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) + system('bundle', 'exec', 'rspec', exception: true, chdir: gem_dir) end From 716e123714cbf49c6c836de776904a3aaa7a8d70 Mon Sep 17 00:00:00 2001 From: Nurahmadie Date: Thu, 9 Apr 2020 09:32:36 +0700 Subject: [PATCH 2/2] Prevent double rake_task include --- lib/rubocop/extension/generator/generator.rb | 29 ++++++++++++++------ smoke/smoke.rb | 2 +- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/rubocop/extension/generator/generator.rb b/lib/rubocop/extension/generator/generator.rb index 04116d6..02d90e5 100644 --- a/lib/rubocop/extension/generator/generator.rb +++ b/lib/rubocop/extension/generator/generator.rb @@ -106,13 +106,9 @@ def self.defaults! end RUBY - patch "Rakefile", /\z/, <<~RUBY - - require 'rspec/core/rake_task' + patch_rakefile - RSpec::Core::RakeTask.new(:spec) do |spec| - spec.pattern = FileList['spec/**/*_spec.rb'] - end + patch "Rakefile", /\z/, <<~RUBY desc 'Generate a new cop with a template' task :new_cop, [:cop] do |_task, args| @@ -150,15 +146,32 @@ def self.defaults! MESSAGE end - private def patch_gemfile + private def has_rspec? path = root_path / 'Gemfile' file = path.read - return if file =~ (/gem ('|")rspec('|")/) + return true if file =~ (/gem ('|")rspec('|")/) + end + + private def patch_gemfile + return if has_rspec? patch 'Gemfile', /\z/, <<~RUBY gem 'rspec' RUBY end + private def patch_rakefile + return if has_rspec? + patch 'Rakefile', /\z/, <<~RUBY + + require 'rspec/core/rake_task' + + RSpec::Core::RakeTask.new(:spec) do |spec| + spec.pattern = FileList['spec/**/*_spec.rb'] + end + RUBY + + end + private def put(path, content) path = root_path / path puts "create #{path}" diff --git a/smoke/smoke.rb b/smoke/smoke.rb index 485ba94..1ff891f 100644 --- a/smoke/smoke.rb +++ b/smoke/smoke.rb @@ -31,5 +31,5 @@ system('bundle', 'install', exception: true, chdir: gem_dir) system('bundle', 'exec', 'rake', 'new_cop[Smoke/Foo]', exception: true, chdir: gem_dir) - system('bundle', 'exec', 'rspec', exception: true, chdir: gem_dir) + system('bundle', 'exec', 'rake', 'spec', exception: true, chdir: gem_dir) end