Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gem generation and smoke test run. #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions lib/rubocop/extension/generator/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,17 @@ 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

spec.add_runtime_dependency 'rubocop'
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|
Expand All @@ -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"

Expand All @@ -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}"
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions smoke/smoke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down