New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement the new RelativeRequireToLib cop + add tests #4
Conversation
90e4bfe
to
c08ac3a
Compare
2bdb52b
to
fdbb59f
Compare
|
Rather than reviewing the specifics, I'll do a recap of the expectations to make sure we're all on the same page. I see two "families of cases" we are interested in. One of them "solvable" and one of the "ambiguous". When the
|
|
thanks @deivid-rodriguez for the thoughts you put into this. for the "When the require call depends on the CWD" case, I think we should also emit an offense because people should not be doing this. it can even be exploited for security flaws. |
|
Yeah, I think it's a good cop idea. But I believe this could be added upstream since it's not specific to packaging. |
|
In any case, that case is super simple, just check that the require argument starts with a dot. The could be other cases, like |
ada2cc4
to
2e4c192
Compare
09630e0
to
76fd7fc
Compare
TODO: document stuff. TODO: use expand_path to see if the expanded path is outside spec/. Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
Previously, the AST pattern could just match simple
`require` calls with relative paths (via regex).
With this new fix, the pattern can correctly
find `require` calls in different formats, like:
- require File.expand_path('../lib/rubocop', __FILE__)
- require File.expand_path("../lib/rubocop", __dir__)
- $:.unshift("../lib")
require '../lib/file.rb'
- require "#{path}/../lib/rubocop"
- require File.dirname(__FILE__) + '/../lib/rubocop'
- require File.dirname(__dir__) + '/../lib/rubocop'
Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
Previously, only the `relative` calls with relative path to lib directoy were being caught as an offense. With this, even `relative_require` calls will now be caught as an offense. And also add its tests. Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
Previously, both `require` and `require_relative` calls were being inspected by the same cop. This had various problems as catching all `require` calls can be a little tricky and not as straight-forward as `require_relative` ones. Hence, the split. For now, the root directory is hardcoded. And the specs of `require` calls have been filtered out. Moreover, AST pattern has been fixed to just accomodate the `require_relative` calls. Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
Mostly because #delete_suffix and #delete_prefix methods are not available in Ruby 2.4.x and were added in Ruby 2.5.x. Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
The tests were written wrt a single directory, "spec" and a single file, "foo". This diversifies these tests to include all possible directories like "spec", "specs", "test", and "tests" and all sorts of files like "foo", "bar", "baz", and "qux". Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
RuboCop::ConfigLoader.project_root correctly gets the root directory of the path. Thanks to @deivid-rodriguez. Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
400318d
to
3400dac
Compare
Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
6976559
to
a093351
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
FYI, I wrote this ruby-style-guide entry which I think goes exactly against this cop... Let me know if the justification isn't clear enough. |
|
Hi @marcandre! Thanks for your help here and for writing that guideline in the style guide. We're aware of that best practice and we do support that. As a matter of fact I've been migrating code over the years to follow that 👍. With this cop, we don't intend to break that rule in general, only to add one exception to it. It turns out that for gem packagers it's useful to be able to "break" a gem into its different components ( The cases we are preventing don't look great to me as A similar situation, but that affects not only packaging contexts, would be requiring your lib code from your gem executable relatively. That wouldn't work for default gems, because the layout of the installed gem doesn't match the development layout of the gem. Anwyays, what I mean is that we don't intend to go against that rule in general, only to provide a tool that people interested in getting their gems more easily packaged for OSs can follow. |
a093351
to
a1aa8c0
Compare
with #starts_with_relative_path? as the naming doesn't have anything to do with what the method does. Thanks, @deivid-rodriguez. Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
a1aa8c0
to
ead6d30
Compare
ead6d30
to
7e27ea5
Compare
Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
7e27ea5
to
c7e27e5
Compare
Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
8793331
to
aababc1
Compare
|
Also, since it might not be clear from my previous message, and from the cops implementation and naming. This cop only intends to detect requires from the specs folder to the lib folder. So, it should only ever be configured with Include:
- 'specs/**/*.rb'or Include:
- 'test/**/*.rb'Maybe we could validate the configuration of the |
|
Thanks for the feedback so far! 😄 I am going to merge this and carry on with the next tasks at hand, starting with documenting things properly! 🚀 |
TODO:
fix regex to match proper calls.useFile.expand_pathto check if the expanded path lies inside thelibdirectory or not.find a way to only check calls in the spec(s)/test(s) dir.Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>