Giter VIP home page Giter VIP logo

rubocop-packaging's Introduction

RuboCop::Packaging

RuboCop::Packaging is an extension of RuboCop, which is a Ruby static code analyzer (a.k.a. linter) and code formatter.

It helps enforcing some of the guidelines that are expected of upstream maintainers so that the downstream can build their packages in a clean environment without any problems.

Documentation

A detailed documentation, explaining what this extension is doing and the reasoning behind it, can be found here: https://docs.rubocop.org/rubocop-packaging/

We also have a packaging-style-guide, listing some good and bad examples and the rationale behind these cops.

In case anything is not clear, please feel free to raise an issue, asking for more explanation!

Installation

Add this line to your application's Gemfile:

gem 'rubocop-packaging'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install rubocop-packaging

Usage

You need to tell RuboCop to load the Packaging extension. There are three ways to do this:

RuboCop configuration file

Put this into your .rubocop.yml file:

require: rubocop-packaging

Alternatively, use the following array notation when specifying multiple extensions:

require:
  - rubocop-other-extension
  - rubocop-packaging

Now you can run rubocop and it will automatically load the RuboCop Packaging cops together with the standard cops.

Command line

rubocop --require rubocop-packaging

Rake task

RuboCop::RakeTask.new do |task|
  task.requires << 'rubocop-packaging'
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

As always, bug reports and pull requests are heartily welcomed! ๐Ÿ’–
This project is intended to be a safe and welcoming space for collaboration.

License

rubocop-packaging is available as open-source under the MIT License.

rubocop-packaging's People

Contributors

aryan1101 avatar deivid-rodriguez avatar jasl avatar koic avatar marcandre avatar marcotc avatar micwoj92 avatar mvz avatar petergoldstein avatar r7kamura avatar terceiro avatar utkarsh2102 avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

rubocop-packaging's Issues

Style/StringConcatenation's autocorrect hinder's with the RequireHardcodingLib AST

StringConcatenation and RequireHardcodingLib both catch the same offense:

spec/spec_helper.rb:8:1: C: Packaging/RequireHardcodingLib: Avoid using require with relative path to lib/.
require File.dirname(__FILE__) + "/../lib/rubocop"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/spec_helper.rb:8:9: C: Style/StringConcatenation: Prefer string interpolation to string concatenation.
require File.dirname(__FILE__) + "/../lib/rubocop"
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/spec_helper.rb:9:1: C: Packaging/RequireHardcodingLib: Avoid using require with relative path to lib/.
require File.dirname(__dir__) + "/../lib/rubocop"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/spec_helper.rb:9:9: C: Style/StringConcatenation: Prefer string interpolation to string concatenation.
require File.dirname(__dir__) + "/../lib/rubocop"
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

But the autocorrect of StringConcatenation fixes these offenses first to:

require "#{File.dirname(__FILE__)}/../../lib/rubocop"
require "#{File.dirname(__dir__)}/../../lib/rubocop"

Now that the offensive line has changed to use string interpolation, the RequireHardcodingLib cop is no longer catching it as an offense, while it still is and that is because the AST formed pre and post autocorrection are very different.

README link is broken

When I go to this link from the README I'm presented with this.

Screen Shot 2020-08-12 at 14 25 19

I realize that some information regarding development is nice to have behind a login. But the links in a readme for OOS software should preferably be publically accessible?

Should we try mutant?

Hey,

I am inclined towards giving a shot to using mutant.
What do you think? Should we do it, should we not?

I'd love to know your opinions on this! :)

Packaging/GemspecGit throws an offense if the file is empty

Here's the log while running the Packaging/GemspecGit cop on GitLab's omnibus-gitlab library:

โžœ  omnibus-gitlab git:(master) โœ— rubocop -a              
Inspecting 659 files
..................................................................................
..................................................................................
..................................An error occurred while Packaging/GemspecGit cop was
inspecting /home/utkarsh/github/omnibus-gitlab/files/gitlab-cookbooks/gitlab/recipes/bootstrap_disable.rb.
To see the complete backtrace run rubocop -d.
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
.....................................................

659 files inspected, no offenses detected

1 error occurred:
An error occurred while Packaging/GemspecGit cop was inspecting
/home/utkarsh/github/omnibus-gitlab/files/gitlab-cookbooks/gitlab/recipes/bootstrap_disable.rb.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.82.0 (using Parser 2.7.1.4, running on ruby 2.7.1 x86_64-linux-gnu)

And files/gitlab-cookbooks/gitlab/recipes/bootstrap_disable.rb is an empty file with dummy comments.

An error occurred while Packaging/RequireHardcodingLib cop was inspecting `file`

Hi, seems with latest update of rubocop to v1.33.0 some changes introduced and rubocop-packaging started to fail

Error looks like this:

Scanning /home/lobashov/sources/onlyoffice_webdriver_wrapper/spec/webdriver_spec.rb
An error occurred while Packaging/RequireHardcodingLib cop was inspecting /home/lobashov/sources/onlyoffice_webdriver_wrapper/spec/webdriver_spec.rb:3:0.
undefined method `project_root' for RuboCop::ConfigLoader:Class

        RuboCop::ConfigLoader.project_root
                             ^^^^^^^^^^^^^
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-packaging-0.5.1/lib/rubocop/packaging/lib_helper_module.rb:10:in `root_dir'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-packaging-0.5.1/lib/rubocop/packaging/lib_helper_module.rb:15:in `target_falls_in_lib?'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-packaging-0.5.1/lib/rubocop/cop/packaging/require_hardcoding_lib.rb:87:in `falls_in_lib?'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-packaging-0.5.1/lib/rubocop/cop/packaging/require_hardcoding_lib.rb:55:in `require?'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-packaging-0.5.1/lib/rubocop/cop/packaging/require_hardcoding_lib.rb:68:in `on_send'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/commissioner.rb:100:in `public_send'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/commissioner.rb:100:in `block (2 levels) in trigger_responding_cops'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/commissioner.rb:99:in `block in trigger_responding_cops'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/commissioner.rb:98:in `each'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/commissioner.rb:98:in `trigger_responding_cops'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/commissioner.rb:69:in `on_send'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.19.1/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.19.1/lib/rubocop/ast/traversal.rb:136:in `each'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.19.1/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.19.1/lib/rubocop/ast/traversal.rb:20:in `walk'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/team.rb:155:in `investigate_partial'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cop/team.rb:83:in `investigate'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:311:in `inspect_file'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:255:in `block in do_inspection_loop'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:289:in `block in iterate_until_no_changes'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:282:in `loop'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:282:in `iterate_until_no_changes'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:251:in `do_inspection_loop'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:134:in `block in file_offenses'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:159:in `file_offense_cache'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:133:in `file_offenses'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:124:in `process_file'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:105:in `block in each_inspected_file'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:104:in `each'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:104:in `reduce'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:104:in `each_inspected_file'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:90:in `inspect_files'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/runner.rb:47:in `run'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cli/command.rb:11:in `run'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cli/environment.rb:18:in `run'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cli.rb:72:in `run_command'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cli.rb:79:in `execute_runners'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/lib/rubocop/cli.rb:48:in `run'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/exe/rubocop:19:in `block in <top (required)>'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/3.1.0/benchmark.rb:311:in `realtime'
/home/lobashov/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.33.0/exe/rubocop:19:in `<top (required)>'
/home/lobashov/.rbenv/versions/3.1.2/bin/rubocop:25:in `load'
/home/lobashov/.rbenv/versions/3.1.2/bin/rubocop:25:in `<main>'

You can reproduce it on my project onlyoffice_webdriver_wrapper

  1. git clone https://github.com/ONLYOFFICE-QA/onlyoffice_webdriver_wrapper.git
  2. cd onlyoffice_webdriver_wrapper && bundle update
  3. rubocop -d

There will be countless failures

Env:

1.33.0 (using Parser 3.1.2.0, rubocop-ast 1.19.1, running on ruby 3.1.2 x86_64-linux)

Possible cop ideas

I've recently found this awesome article explaining the contents of a gemspec file. I was wondering if some of these suggestions could be useful enough to be a cop for this gem.

I want to highlight two of them. WDYT?

require_relative and the $LOAD_PATH

For example the article mentions by using require_relative we can replace

lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "emoticon/version"

with

require_relative "lib/emoticon/version"

I've seen the style guide about require_relative but it appears this would not apply here. Or rather, after seeing #26 it seems this might even be okay to do.

Removal of required_rubygems_version

The article states that this might not be useful to have around because:

The required_rubygems_version attribute works exactly the same way as the Ruby version one. However, I tend to skip specifying this attribute. Why? By default Ruby installation is packaged with rubygems that limits its compatible versions.

Packaging/RequireHardcodingLib: Warning when absolute path was used

Offenses:

spec/generators/paper_trail/install_generator_spec.rb:5:1: C: Packaging/RequireHardcodingLib: Avoid using require with relative path to lib/. Use require with absolute path instead.
require File.expand_path("../../../lib/generators/paper_trail/install/install_generator", __dir__)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This warning happened.

But, File.expand_path was used, so it is an absolute path.

Perhaps the check can include knowing what File.expand_path does to a path?

`RequireRelativeToLib` cop shouldn't flag offenses from `lib` into `lib`

Upon using the cop on autoprefixer-rails' codebase, here's what it shows:

โžœ  autoprefixer-rails git:(use-rubocop) โœ— rubocop --only Packaging
Inspecting 25 files
..CC....................C

Offenses:

autoprefixer-rails.gemspec:13:24: C: Packaging/GemspecGit: Avoid using git to produce lists of files.
Downstreams often need to build your package in an environment that does not have git (on purpose).
Use some pure Ruby alternative, like Dir or Dir.glob.
  s.files            = `git ls-files`.split("\n")
                       ^^^^^^^^^^^^^^
lib/autoprefixer-rails.rb:39:1: C: Packaging/RelativeRequireToLib: Avoid using require_relative with
relative path to lib. Use require instead.
require_relative "autoprefixer-rails/result"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/autoprefixer-rails.rb:40:1: C: Packaging/RelativeRequireToLib: Avoid using require_relative with
relative path to lib. Use require instead.
require_relative "autoprefixer-rails/version"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/autoprefixer-rails.rb:41:1: C: Packaging/RelativeRequireToLib: Avoid using require_relative with
relative path to lib. Use require instead.
require_relative "autoprefixer-rails/processor"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/autoprefixer-rails.rb:43:1: C: Packaging/RelativeRequireToLib: Avoid using require_relative with
relative path to lib. Use require instead.
require_relative "autoprefixer-rails/railtie" if defined?(Rails)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/spec_helper.rb:6:1: C: Packaging/RelativeRequireToLib: Avoid using require_relative with
relative path to lib. Use require instead.
require_relative "../lib/autoprefixer-rails"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

25 files inspected, 6 offenses detected

The first and last offenses are correct. However, the middle ones don't look right.
The cop, essentially, shouldn't flag offenses from lib to lib. Just the ones that map from outside to lib.

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.