diff --git a/lib/rubocop/extension/generator/generator.rb b/lib/rubocop/extension/generator/generator.rb index 667ed3e..02d90e5 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 @@ -105,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| @@ -136,9 +133,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 +146,32 @@ def self.defaults! MESSAGE end + private def has_rspec? + path = root_path / 'Gemfile' + file = path.read + 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}" @@ -163,7 +184,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..1ff891f 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,6 +23,12 @@ 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)