From 0957063982dbd6c040b858937f4f87f7889864c3 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Fri, 21 Aug 2020 15:44:07 -0700 Subject: [PATCH] Add specs Signed-off-by: Tim Smith --- .expeditor/verify.pipeline.yml | 20 ++++---- Gemfile | 11 +++++ Rakefile | 36 +++++++++++++- chefstyle.gemspec | 4 -- .../ruby_27_keyword_argument_warnings_spec.rb | 49 +++++++++++++++++++ spec/shared/autocorrect_behavior.rb | 8 +++ spec/spec_helper.rb | 24 +++++++++ 7 files changed, 137 insertions(+), 15 deletions(-) create mode 100644 spec/rubocop/cop/chef/ruby/ruby_27_keyword_argument_warnings_spec.rb create mode 100755 spec/shared/autocorrect_behavior.rb create mode 100755 spec/spec_helper.rb diff --git a/.expeditor/verify.pipeline.yml b/.expeditor/verify.pipeline.yml index f3aecf9..be96224 100644 --- a/.expeditor/verify.pipeline.yml +++ b/.expeditor/verify.pipeline.yml @@ -11,42 +11,42 @@ expeditor: steps: -- label: run-chefstyle-ruby-2.4 +- label: run-chefstyle-and-specs-ruby-2.4 command: - - .expeditor/run_linux_tests.sh rake style + - .expeditor/run_linux_tests.sh rake expeditor: executor: docker: image: ruby:2.4-buster -- label: run-chefstyle-ruby-2.5 +- label: run-chefstyle-and-specs-ruby-2.5 command: - - .expeditor/run_linux_tests.sh rake style + - .expeditor/run_linux_tests.sh rake expeditor: executor: docker: image: ruby:2.5-buster -- label: run-chefstyle-ruby-2.6 +- label: run-chefstyle-and-specs-ruby-2.6 command: - - .expeditor/run_linux_tests.sh rake style + - .expeditor/run_linux_tests.sh rake expeditor: executor: docker: image: ruby:2.6-buster -- label: run-chefstyle-ruby-2.7 +- label: run-chefstyle-and-specs-ruby-2.7 command: - - .expeditor/run_linux_tests.sh rake style + - .expeditor/run_linux_tests.sh rake expeditor: executor: docker: image: ruby:2.7-buster -- label: run-chefstyle-windows +- label: run-chefstyle-and-specs-windows command: - bundle install --jobs=7 --retry=3 --without docs debug - - bundle exec rake style + - bundle exec rake expeditor: executor: docker: diff --git a/Gemfile b/Gemfile index f5788f3..aed4cdc 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,14 @@ source "https://rubygems.org" # Specify your gem's dependencies in chefstyle.gemspec gemspec + +group :debug do + gem "pry" + gem "pry-byebug" + gem "pry-stack_explorer", "~> 0.4.0" # 0.5.0 drops support for Ruby < 2.6 +end + +group :development do + gem "rake", ">= 12.0" + gem "rspec", ">= 3.4" +end \ No newline at end of file diff --git a/Rakefile b/Rakefile index 2916741..f68b35e 100644 --- a/Rakefile +++ b/Rakefile @@ -26,6 +26,40 @@ RuboCop::RakeTask.new(:style) do |task| task.options << "--display-cop-names" end +require "rspec/core/rake_task" +RSpec::Core::RakeTask.new(:spec) do |spec| + spec.pattern = FileList["spec/cop/**/*.rb"] +end + +desc "Run RSpec with code coverage" +task :coverage do + ENV["COVERAGE"] = "true" + Rake::Task["spec"].execute +end + +desc "Ensure that all cops are defined in the chefstyle.yml config" +task :validate_config do + require "chefstyle" + require "yaml" unless defined?(YAML) + status = 0 + config = YAML.load_file("config/chefstyle.yml") + + puts "Checking that all cops are defined in config/chefstyle.yml:" + + RuboCop::Cop::Chef.constants.each do |dep| + RuboCop::Cop::Chef.const_get(dep).constants.each do |cop| + unless config["#{dep}/#{cop}"] + puts "Error: #{dep}/#{cop} not found in config/chefstyle.yml" + status = 1 + end + end + end + + puts "All Cops found in the config. Good work." if status == 0 + + exit status +end + begin require "yard" unless defined?(YARD) YARD::Rake::YardocTask.new(:docs) @@ -39,4 +73,4 @@ task :console do ARGV.clear IRB.start end -task default: %i{build install} +task default: %i{style spec validate_config} diff --git a/chefstyle.gemspec b/chefstyle.gemspec index f229346..043f325 100644 --- a/chefstyle.gemspec +++ b/chefstyle.gemspec @@ -18,9 +18,5 @@ Gem::Specification.new do |spec| spec.executables = %w{chefstyle} spec.require_paths = ["lib"] - spec.add_development_dependency "bundler" - spec.add_development_dependency "rake", ">= 12.0" - spec.add_development_dependency "rspec" - spec.add_development_dependency "pry" spec.add_dependency("rubocop", Chefstyle::RUBOCOP_VERSION) end diff --git a/spec/rubocop/cop/chef/ruby/ruby_27_keyword_argument_warnings_spec.rb b/spec/rubocop/cop/chef/ruby/ruby_27_keyword_argument_warnings_spec.rb new file mode 100644 index 0000000..3403fc0 --- /dev/null +++ b/spec/rubocop/cop/chef/ruby/ruby_27_keyword_argument_warnings_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true +# +# Copyright:: Chef Software, Inc. +# Author:: Tim Smith () +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "spec_helper" + +describe RuboCop::Cop::Chef::ChefRuby::Ruby27KeywordArgumentWarnings, :config do + subject(:cop) { described_class.new(config) } + + it "registers an offense when passing a hash with brackets to shell_out" do + expect_offense(<<~RUBY) + shell_out('hostnamectl status', {returns: [0, 1]}) + ^^^^^^^^^^^^^^^^^ Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings. + RUBY + + expect_correction(<<~RUBY) + shell_out('hostnamectl status', returns: [0, 1]) + RUBY + end + + it "registers an offense when passing a hash with brackets to shell_out!" do + expect_offense(<<~RUBY) + shell_out!('hostnamectl status', {returns: [0, 1]}) + ^^^^^^^^^^^^^^^^^ Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings. + RUBY + + expect_correction(<<~RUBY) + shell_out!('hostnamectl status', returns: [0, 1]) + RUBY + end + + it "doesn't register an offense when properly passing options to the helpers" do + expect_no_offenses("shell_out!('hostnamectl status', returns: [0, 1])") + end +end diff --git a/spec/shared/autocorrect_behavior.rb b/spec/shared/autocorrect_behavior.rb new file mode 100755 index 0000000..f652b09 --- /dev/null +++ b/spec/shared/autocorrect_behavior.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +RSpec.shared_examples "autocorrect" do |original, corrected| + it "autocorrects `#{original}` to `#{corrected}`" do + autocorrected = autocorrect_source(original, "spec/foo_spec.rb") + + expect(autocorrected).to eql(corrected) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100755 index 0000000..21de65f --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true +require "rubocop" +require "rubocop/rspec/support" + +module SpecHelper + ROOT = Pathname.new(__dir__).parent.freeze +end + +spec_helper_glob = File.expand_path("{support,shared}/*.rb", __dir__) +Dir.glob(spec_helper_glob).map(&method(:require)) + +RSpec.configure do |config| + # Basic configuration + config.run_all_when_everything_filtered = true + config.filter_run(:focus) + config.order = :random + + config.include RuboCop::RSpec::ExpectOffense +end + +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) +$LOAD_PATH.unshift(File.dirname(__FILE__)) + +require "chefstyle"