Giter Site home page Giter Site logo

chefstyle's Introduction

Chefstyle - Version Pinned RuboCop with Chef approved Cop list for linting

Gem Version

Umbrella Project: Chef Foundation

Project State: Deprecated

This project has been merged into Cookstyle as of Cookstyle 7.32.7, where it can be triggered by running cookstyle --chefstyle.

This is an internal style guide for chef ruby projects (chef-client, ohai, mixlib-shellout, mixlib-config, etc).

It is not meant for consumption by cookbooks or for any general purpose uses. Chef Users and Customers Should Generally Not Use This Tool and Should Use Cookstyle. It is not intended for any audience outside of chef core ruby development.

It will conflict with rubocop defaults, cookstyle, finstyle and other ruby style guides entirely by design. The point is that the core chef authors vehemently disagree with them on points of style and this point is generally not up for debate.

It will have many rules that are disabled simply because fixing a project as large as chef-client would be tedious and have little value. It will have other rules that are disabled because chef exposes edge conditions that make them falsely alert. Other rules will be selected based on the biases of the core chef developers which are often violently at odds with the rubocop core developers over ruby style.

Pull requests to this repo will not be accepted without corresponding PRs into at least the chef-client and ohai codebases to clean the code up. PRs will not be accepted that assume unfunded mandates for other people to finish the work. Do not open PRs offering opinions or suggestions without offering to do the work.

The project itself is a derivative of finstyle, but starts with all rules disabled. The active ruleset is in the config/chefstyle.yml file.

How It Works

This library has a direct dependency on one specific version of RuboCop (at a time), and [patches it][patch] to load the [upstream configuration][upstream] and [custom set][config] of rule updates. When a new RuboCop release comes out, this library can rev its pinned version dependency to determine if any breaking style or lint rules were added/dropped/reversed/etc.

Installation

Add this line to your application's Gemfile:

gem 'chefstyle'

And then execute:

$ bundle

Or install it yourself as:

$ gem install chefstyle

Usage

Vanilla RuboCop

Run RuboCop as normal, simply add a -r chefstyle option when running:

rubocop -r chefstyle -D --format offenses

chefstyle Command

Use this tool just as you would RuboCop, but invoke the chefstyle binary instead which patches RuboCop to load rules from the chefstyle gem. For example:

chefstyle -D --format offenses

Rake

In a Rakefile, the setup is exactly the same, except you need to require the chefstyle library first:

require "chefstyle"
require "rubocop/rake_task"
RuboCop::RakeTask.new do |task|
  task.options << "--display-cop-names"
end

guard-rubocop

You can use one of two methods. The simplest is to add the -r chefstyle option to the :cli option in your Guardfile:

guard :rubocop, cli: "-r chefstyle" do
  watch(%r{.+\.rb$})
  watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
end

Alternatively you could pass the path to Chefstyle's configuration by using the Chefstyle.config method:

require "chefstyle"

guard :rubocop, cli: "--config #{Chefstyle.config}" do
  watch(%r{.+\.rb$})
  watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
end

git pre-commit hoooks

In any repo a pre-commit hook can be used in .git/hooks/pre-commit to catch offenses before checkin:

#!/usr/bin/env ruby

changed_files = `git diff --name-only --cached`.split.select { |f| File.extname(f) == ".rb" }

unless changed_files.empty?
  system "chefstyle #{changed_files.join(" ")}"
  unless $?.success?
    puts "\n\nthere was a chefstyle error, please fix before commiting:"
    puts "(chefstyle -a may be able to autofix these issues for you)\n\n"
  end
end
exit $?.to_s[-1].to_i

For whatever $REASON git does not allow easily distributing hooks in the repo itself so each individual user needs to set this up.

.rubocop.yml

As with vanilla RuboCop, any custom settings can still be placed in a .rubocop.yml file in the root of your project.

chefstyle's People

Contributors

chef-ci avatar chef-expeditor[bot] avatar dafyddcrosby avatar danielsdeleo avatar dependabot-preview[bot] avatar lamont-granquist avatar mattray avatar ramereth avatar skeshari12 avatar tas50 avatar tduffield avatar thommay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chefstyle's Issues

chefspec - undefined method shell_out' - aftercookstyle -a`

reference issue https://github.com/chef/customer-bugs/issues/300

Version:

[Version of the project installed]

Environment:

[Details about the environment such as the Operating System, cookbook details, etc...]

Scenario:

Cookstyle auto correcting Mixlib::ShellOut.new(cmd).run_command to shell_out(cmd) and inspec is failing with undefined method `shell_out'

Cookstyle autocorrected as follows.

$ chef exec cookstyle -aInspecting 6 files...R..Offenses:lib/windjammer/deployer.rb:123:13: R: [Corrected] Chef/Modernize/ShellOutHelper: Use the built-in shell_out helper available in Chef Infra Client 12.11+ instead of calling Mixlib::ShellOut.new('foo').run_command. res = Mixlib::ShellOut.new(cmd).run_command ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^6 files inspected, 1 offense detected, 1 offense corrected

Where the change:

$ git diffdiff --git a/lib/windjammer/deployer.rb b/lib/windjammer/deployer.rbindex 4dab5dc..b7ec1b8 100644--- a/lib/windjammer/deployer.rb+++ b/lib/windjammer/deployer.rb@@ -120,7 +120,7 @@ class Windjammer def run_step(id, step, params) cmd = "run_step --id=#{id} --name=#{step} --params='#{params.to_json}'"- res = Mixlib::ShellOut.new(cmd).run_command+ res = shell_out(cmd) fail "Command Failed with #{res.exitstatus}" unless res.exitstatus.zero? end end

ChefSpec starts to fail with the undefined method `shell_out'.

$ chef exec rspecFFF.Failed to get JSON: Caught error response: 404 # このエラーは謎なのですが関係ないとおもいます。.Failed to get JSON: Caught error response: 404Failed to get JSON: Caught error response: 404Failed to get JSON: Caught error response: 404Failed to get JSON: Caught error response: 404Failed to get JSON: Caught error response: 404...FFFailures: 1) Windjammer::Deployer down status Failure/Error: res = shell_out(cmd) NoMethodError: undefined methodshell_out' for #Windjammer::Deployer:0x00000000014bb300 # ./lib/windjammer/deployer.rb:123:in run_step' # ./lib/windjammer/deployer.rb:27:in down' # ./spec/unit/windjammer/deployer_spec.rb:71:in block (2 levels) in <top (required)>'

Please advise us on how to change the chef spec to make it pass.

Unfortunately, the chef spec content cannot be shared due to customer policy so please tell us what should be included when testing shell_out with chef spec.

Steps to Reproduce:

[If you are filing an issue what are the things we need to do in order to repro your problem?]

Expected Result:

After cookstyle auto correcting Mixlib::ShellOut.new(cmd).run_command to shell_out(cmd) and inspec is failing with undefined method shell_out

Actual Result:

ChefSpec should respect the change and should have shell_out(cmd) as a defined method.

Needs a rubocop engine upgrade

  • rubocop 0.41.1 failed miserably since rubocop and rubocop -a disagreed about which cops were failing and autofixing didn't fix the right stuff at all.
  • rubocop 0.41.2 failed due to infinite looping:
930 files inspected, 35 offenses detected, 35 offenses corrected
Infinite loop detected in /Users/lamont/oc/chef4/spec/functional/resource/dsc_script_spec.rb.
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/runner.rb:202:in `check_for_infinite_loop'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/runner.rb:173:in `block in do_inspection_loop'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/runner.rb:172:in `loop'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/runner.rb:172:in `do_inspection_loop'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/runner.rb:87:in `process_file'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/runner.rb:59:in `block in inspect_files'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/runner.rb:57:in `each'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/runner.rb:57:in `inspect_files'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/runner.rb:35:in `run'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/cli.rb:72:in `execute_runner'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/lib/rubocop/cli.rb:28:in `run'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/bin/rubocop:14:in `block in <top (required)>'
/Users/lamont/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/rubocop-0.41.2/bin/rubocop:13:in `<top (required)>'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/chefstyle-0.4.0/bin/chefstyle:13:in `load'
/Users/lamont/.rvm/gems/ruby-2.2.5/gems/chefstyle-0.4.0/bin/chefstyle:13:in `<top (required)>'
/Users/lamont/.rvm/gems/ruby-2.2.5/bin/chefstyle:22:in `load'
/Users/lamont/.rvm/gems/ruby-2.2.5/bin/chefstyle:22:in `<main>'

Dependabot can't resolve your Ruby dependency files

Dependabot can't resolve your Ruby dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Bundler::VersionConflict with message: Bundler could not find compatible versions for gem "rubocop":
  In Gemfile:
    chefstyle was resolved to 0.0.1, which depends on
      rubocop (= 0.0.1)

Could not find gem 'rubocop (= 0.0.1)', which is required by gem 'chefstyle', in any of the sources.

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Dependabot can't resolve your Ruby dependency files

Dependabot can't resolve your Ruby dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Bundler::VersionConflict with message: Bundler could not find compatible versions for gem "rubocop":
  In Gemfile:
    chefstyle was resolved to 0.0.1, which depends on
      rubocop (= 0.0.1)

Could not find gem 'rubocop (= 0.0.1)', which is required by gem 'chefstyle', in any of the sources.

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Double Quotes vs. Single Quotes

Before, I was flag for using double-quotes when not needed with Chef 13, but now with Chef 14, I am flagged for using single-quotes. What gives? How can I shut this off, why was it changed?

recipes/service.rb:28:9: C: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
service 'nginx' do
        ^^^^^^^

Enable Style/MultipleComparison

In RuboCop 1.1 this will autocorrect and it's a good one to have enabled. It rewrites some hard to read conditions into much simpler code.

seems to fail if there's no .rubocop.yml

% chefstyle
private method `select' called for nil:NilClass
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/rubocop-0.45.0/lib/rubocop/target_finder.rb:113:in `excluded_dirs'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/rubocop-0.45.0/lib/rubocop/target_finder.rb:90:in `find_files'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/rubocop-0.45.0/lib/rubocop/target_finder.rb:60:in `target_files_in_dir'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/rubocop-0.45.0/lib/rubocop/target_finder.rb:31:in `find'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/rubocop-0.45.0/lib/rubocop/runner.rb:48:in `find_target_files'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/rubocop-0.45.0/lib/rubocop/runner.rb:32:in `run'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/rubocop-0.45.0/lib/rubocop/cli.rb:72:in `execute_runner'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/rubocop-0.45.0/lib/rubocop/cli.rb:27:in `run'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/rubocop-0.45.0/bin/rubocop:13:in `block in <top (required)>'
/Users/lamont/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/rubocop-0.45.0/bin/rubocop:12:in `<top (required)>'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/chefstyle-0.4.0/bin/chefstyle:13:in `load'
/Users/lamont/.rvm/gems/ruby-2.3.1/gems/chefstyle-0.4.0/bin/chefstyle:13:in `<top (required)>'
/Users/lamont/.rvm/gems/ruby-2.3.1/bin/chefstyle:22:in `load'
/Users/lamont/.rvm/gems/ruby-2.3.1/bin/chefstyle:22:in `<main>'

touch .rubocop.yml will fix it.

i think this changed at some point with an engine upgrade? possibly some weird interaction with our monkeypatching?

Dependabot can't resolve your Ruby dependency files

Dependabot can't resolve your Ruby dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Bundler::VersionConflict with message: Bundler could not find compatible versions for gem "rubocop":
  In Gemfile:
    chefstyle was resolved to 0.0.1, which depends on
      rubocop (= 0.0.1)

Could not find gem 'rubocop (= 0.0.1)', which is required by gem 'chefstyle', in any of the sources.

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Dependabot can't resolve your Ruby dependency files

Dependabot can't resolve your Ruby dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Bundler::VersionConflict with message: Bundler could not find compatible versions for gem "rubocop":
  In Gemfile:
    chefstyle was resolved to 0.0.1, which depends on
      rubocop (= 0.0.1)

Could not find gem 'rubocop (= 0.0.1)', which is required by gem 'chefstyle', in any of the sources.

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.