[WIP] Updating require_hardcoding_lib AST to report an offense when a relative path is used to lib/ (and, not specifically focus on require calls) - #38
Conversation
…th lib Update tests according to the new AST Signed-off-by: Samyak Jain <samyak.jn11@gmail.com>
utkarsh2102
left a comment
There was a problem hiding this comment.
Hi @samyak-jn,
Thanks for working on this but I am not sure if this is what we need? We should essentially generalize the AST to catch all the "bad" calls and "require" ones were just a subset of those. I don't expect someone to go through the AST in detail unless we have the existing tests passing + we've added more generalized tests, which are passing as well. The AST should then "speak for itself" and maybe someone could then do the final edge-case testing and stuff and do a finish-up of the review/merge. Does that make sense? 😅
Let me know if we're not on the same page or something or if I am simply missing anything?
| context "when `require` call lies outside spec/" do | ||
| let(:filename) { "#{project_root}/spec/foo_spec.rb" } | ||
| let(:source) { "require '../lib/foo.rb'" } | ||
|
|
||
| it "registers an offense" do | ||
| expect_offense(<<~RUBY, filename) | ||
| #{source} | ||
| #{"^" * source.length} #{message} | ||
| RUBY | ||
|
|
||
| expect_correction(<<~RUBY) | ||
| require "foo.rb" | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context "when `require` call after using `unshift` lies outside spec/" do | ||
| let(:filename) { "#{project_root}/tests/foo/bar.rb" } | ||
| let(:source) { <<~RUBY.chomp } | ||
| $:.unshift('../../lib') | ||
| require '../../lib/foo/bar' | ||
| RUBY | ||
|
|
||
| it "registers an offense" do | ||
| expect_offense(<<~RUBY, filename) | ||
| #{source} | ||
| #{"^" * 27} #{message} | ||
| RUBY | ||
|
|
||
| expect_correction(<<~RUBY) | ||
| $:.unshift('../../lib') | ||
| require "foo/bar" | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context "when `require` call uses File#expand_path method with __FILE__" do |
There was a problem hiding this comment.
I expect these tests to be still working, really. Might need some fixing but in the end, it should work, too (even as-is, from a quick POV). Because we're essentially just generalizing the entire set of calls and the above tests are really just a subset of those. So I expect them to work + I expect new tests to be added (the generalized one, mentioned in the issue, for example).
| context "when the `require` call doesn't use relative path" do | ||
| let(:filename) { "#{project_root}/spec/bar_spec.rb" } | ||
| let(:source) { "require 'bar'" } | ||
|
|
||
| it "does not register an offense" do | ||
| expect_no_offenses(<<~RUBY, filename) | ||
| #{source} | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context "when the `require` call lies inside test/" do | ||
| let(:filename) { "#{project_root}/test/bar/foo_spec.rb" } | ||
| let(:source) { "require '../foo'" } | ||
|
|
||
| it "does not register an offense" do | ||
| expect_no_offenses(<<~RUBY, filename) | ||
| #{source} | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context "when the `require` call is made to lib/ but it lies under spec/" do | ||
| let(:filename) { "#{project_root}/spec/lib/baz/qux_spec.rb" } | ||
| let(:source) { "require '../../lib/foo'" } | ||
|
|
||
| it "does not register an offense" do | ||
| expect_no_offenses(<<~RUBY, filename) | ||
| #{source} | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context "when the `require` call is made to lib/ from inside lib/ itself" do | ||
| let(:filename) { "#{project_root}/lib/foo/bar.rb" } | ||
| let(:source) { "require '../foo'" } | ||
|
|
||
| it "does not register an offense" do | ||
| expect_no_offenses(<<~RUBY, filename) | ||
| #{source} | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context "when the `require` call is made from the gemspec file" do | ||
| let(:filename) { "#{project_root}/foo.gemspec" } | ||
| let(:source) { "require 'lib/foo/version'" } | ||
|
|
||
| it "does not register an offense" do | ||
| expect_no_offenses(<<~RUBY, filename) | ||
| #{source} | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context "when `require` call uses File#expand_path method with __FILE__ but lies inside lib/" do | ||
| context "when use of File#expand_path method with __FILE__ but lies inside lib/" do |
| return unless require?(node) | ||
| return unless File?(node) |
There was a problem hiding this comment.
We need a different name here, File is not very clear as to what it implies or means. Also preferably something that's not capitalized. Hope that makes sense?
There was a problem hiding this comment.
Yes, of course, thanks for PTO. :)
|
Hi @utkarsh2102,
Thanks for quite a verbose explanation, and yes I agree with your point on this. For the very same here's the AST below could you please verify them? Thanks for taking your out time for this :) |
|
Oh no, I don't think we want |
|
Plus, I guess, the CI should be green, that's the very first thing I'd like to see, heh. |
|
I'm not sure this direction is correct. |
Hi,
This is with regards to issue #35 for the same I have tweaked the AST so that it checks the usage of File.expand_path and File.dirname used with lib/.
I am not so much proficient with ruby, so please correct me if I'm wrong, thanks! :)
Once, the AST is finalized we can discuss more renaming the cop and etcetera.