Skip to content

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 committed Aug 21, 2020
1 parent 2e5adef commit 0957063
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 15 deletions.
20 changes: 10 additions & 10 deletions .expeditor/verify.pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 35 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -39,4 +73,4 @@ task :console do
ARGV.clear
IRB.start
end
task default: %i{build install}
task default: %i{style spec validate_config}
4 changes: 0 additions & 4 deletions chefstyle.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true
#
# Copyright:: Chef Software, Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
#
# 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
8 changes: 8 additions & 0 deletions spec/shared/autocorrect_behavior.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 0957063

Please sign in to comment.