From d3275e866fc589e17ebf072897a040e94e04ce4c Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Sat, 24 Mar 2018 19:19:00 -0700 Subject: [PATCH] [CLI::Gem] Add a --rubocop option --- lib/bundler/cli.rb | 1 + lib/bundler/cli/gem.rb | 11 +++- lib/bundler/templates/newgem/Rakefile.tt | 13 +++-- .../templates/newgem/newgem.gemspec.tt | 3 ++ spec/commands/newgem_spec.rb | 50 +++++++++++++++++-- 5 files changed, 71 insertions(+), 7 deletions(-) diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index d86441f5aee..b926ec4a670 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -515,6 +515,7 @@ def viz :desc => "Open generated gemspec in the specified editor (defaults to $EDITOR or $BUNDLER_EDITOR)" method_option :ext, :type => :boolean, :default => false, :desc => "Generate the boilerplate for C extension code" method_option :mit, :type => :boolean, :desc => "Generate an MIT license file. Set a default with `bundle config gem.mit true`." + method_option :rubocop, :type => :boolean, :desc => "Add rubocop to the generated Rakefile and gemspec" method_option :test, :type => :string, :lazy_default => "rspec", :aliases => "-t", :banner => "rspec", :desc => "Generate a test directory for your library, either rspec or minitest. Set a default with `bundle config gem.test rspec`." def gem(name) diff --git a/lib/bundler/cli/gem.rb b/lib/bundler/cli/gem.rb index aad4341f5d7..4548db4e7de 100644 --- a/lib/bundler/cli/gem.rb +++ b/lib/bundler/cli/gem.rb @@ -101,7 +101,7 @@ def run end end - config[:test_task] = config[:test] == "minitest" ? "test" : "spec" + config[:test_task] = config[:test] == "minitest" ? :test : :spec if ask_and_set(:mit, "Do you want to license your code permissively under the MIT license?", "This means that any other developer or company will be legally allowed to use your code " \ @@ -125,6 +125,15 @@ def run templates.merge!("CODE_OF_CONDUCT.md.tt" => "CODE_OF_CONDUCT.md") end + if ask_and_set(:rubocop, "Do you want to add rubocop as a dependency for gems you generate?", + "RuboCop is a static code analyzer that has out-of-the-box rules for many" \ + "of the guidelines in the community style guide." \ + "For more information, see the RuboCop docs (https://rubocop.readthedocs.io/en/latest/)" \ + "and the Ruby Style Guides (https://github.com/bbatsov/ruby-style-guide).") + config[:rubocop] = true + Bundler.ui.info "RuboCop enabled in config" + end + templates.merge!("exe/newgem.tt" => "exe/#{name}") if config[:exe] if options[:ext] diff --git a/lib/bundler/templates/newgem/Rakefile.tt b/lib/bundler/templates/newgem/Rakefile.tt index 099da6f3ecd..1262457c72c 100644 --- a/lib/bundler/templates/newgem/Rakefile.tt +++ b/lib/bundler/templates/newgem/Rakefile.tt @@ -1,4 +1,5 @@ require "bundler/gem_tasks" +<% default_task_names = [config[:test_task]] -%> <% if config[:test] == "minitest" -%> require "rake/testtask" @@ -13,8 +14,16 @@ require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) +<% end -%> +<% if config[:rubocop] -%> +<% default_task_names << :rubocop -%> +require "rubocop/rake_task" + +RuboCop::RakeTask.new + <% end -%> <% if config[:ext] -%> +<% default_task_names.unshift(:clobber, :compile) -%> require "rake/extensiontask" task :build => :compile @@ -23,7 +32,5 @@ Rake::ExtensionTask.new("<%= config[:underscored_name] %>") do |ext| ext.lib_dir = "lib/<%= config[:namespaced_path] %>" end -task :default => [:clobber, :compile, :<%= config[:test_task] %>] -<% else -%> -task :default => :<%= config[:test_task] %> <% end -%> +task :default => <%= default_task_names.size == 1 ? default_task_names.first.inspect : default_task_names.inspect %> diff --git a/lib/bundler/templates/newgem/newgem.gemspec.tt b/lib/bundler/templates/newgem/newgem.gemspec.tt index 717e0ac399c..03d8ca11b41 100644 --- a/lib/bundler/templates/newgem/newgem.gemspec.tt +++ b/lib/bundler/templates/newgem/newgem.gemspec.tt @@ -45,4 +45,7 @@ Gem::Specification.new do |spec| <%- if config[:test] -%> spec.add_development_dependency "<%= config[:test] %>", "~> <%= config[:test_framework_version] %>" <%- end -%> +<%- if config[:rubocop] -%> + spec.add_development_dependency "rubocop" +<%- end -%> end diff --git a/spec/commands/newgem_spec.rb b/spec/commands/newgem_spec.rb index d79162c5668..821e1c75dac 100644 --- a/spec/commands/newgem_spec.rb +++ b/spec/commands/newgem_spec.rb @@ -3,7 +3,7 @@ RSpec.describe "bundle gem" do def reset! super - global_config "BUNDLE_GEM__MIT" => "false", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__COC" => "false" + global_config "BUNDLE_GEM__MIT" => "false", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__COC" => "false", "BUNDLE_GEM__RUBOCOP" => "false" end def remove_push_guard(gem_name) @@ -128,6 +128,36 @@ def gem_skeleton_assertions(gem_name) end end + shared_examples_for "--rubocop flag" do + before do + execute_bundle_gem(gem_name, "--rubocop", false) + end + + it "generates a gem skeleton with rubocop" do + gem_skeleton_assertions(gem_name) + expect(bundled_app("test-gem/Rakefile")).to read_as( + include('require "rubocop/rake_task"'). + and(include("RuboCop::RakeTask.new"). + and(match(/:default.+:rubocop/))) + ) + expect(bundled_app("test-gem/#{gem_name}.gemspec")).to read_as(include('spec.add_development_dependency "rubocop"')) + end + end + + shared_examples_for "--no-rubocop flag" do + define_negated_matcher :exclude, :include + + before do + execute_bundle_gem(gem_name, "--no-rubocop", false) + end + + it "generates a gem skeleton without rubocop" do + gem_skeleton_assertions(gem_name) + expect(bundled_app("test-gem/Rakefile")).to read_as(exclude("rubocop")) + expect(bundled_app("test-gem/#{gem_name}.gemspec")).to read_as(exclude("rubocop")) + end + end + context "README.md" do let(:gem_name) { "test_gem" } let(:generated_gem) { Bundler::GemHelper.new(bundled_app(gem_name).to_s) } @@ -532,7 +562,7 @@ module TestGem context "with mit option in bundle config settings set to true" do before do - global_config "BUNDLE_GEM__MIT" => "true", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__COC" => "false" + global_config "BUNDLE_GEM__MIT" => "true", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__RUBOCOP" => "false", "BUNDLE_GEM__COC" => "false" end after { reset! } it_behaves_like "--mit flag" @@ -546,7 +576,7 @@ module TestGem context "with coc option in bundle config settings set to true" do before do - global_config "BUNDLE_GEM__MIT" => "false", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__COC" => "true" + global_config "BUNDLE_GEM__MIT" => "false", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__RUBOCOP" => "false", "BUNDLE_GEM__COC" => "true" end after { reset! } it_behaves_like "--coc flag" @@ -557,6 +587,20 @@ module TestGem it_behaves_like "--coc flag" it_behaves_like "--no-coc flag" end + + context "with rubocop option in bundle config settings set to true" do + before do + global_config "BUNDLE_GEM__MIT" => "false", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__COC" => "false", "BUNDLE_GEM__RUBOCOP" => "true" + end + after { reset! } + it_behaves_like "--rubocop flag" + it_behaves_like "--no-rubocop flag" + end + + context "with rubocop option in bundle config settings set to false" do + it_behaves_like "--rubocop flag" + it_behaves_like "--no-rubocop flag" + end end context "gem naming with dashed" do