Skip to content
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

[#5473] Infer TargetRailsVersion from bundler lock files #5518

Merged
merged 1 commit into from
Feb 26, 2018
Merged

[#5473] Infer TargetRailsVersion from bundler lock files #5518

merged 1 commit into from
Feb 26, 2018

Conversation

roberts1000
Copy link
Contributor

Closes #5473.

Change the config init process slightly for TargetRailsVersion to look in gems.locked or Gemfile.lock for the best value. Either of those files will lock Rails to a specific version and we can use that information to dynamically pick the best value for TargetRailsVersion.

If TargetRailsVersion is set in the config file, it still takes precedence and is always used. If Rails is not in gems.locked or Gemfile.lock, and the user hasn't specified a version in the config, the default version is still used as the fallback.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run rake default or rake parallel. It executes all tests and RuboCop for itself, and generates the documentation.

@roberts1000
Copy link
Contributor Author

@pocke I took a stab at the issue. You mentioned using bundler to read the lock file, but I wasn't sure if we wanted to add a dependency on bundler at that point in the code. I stuck with the regex matching approach for the first cut, but I'm happy to change it.

before do
allow(configuration).to(
receive(:bundler_lock_file_name).and_return(lock_file_path)
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably you can use isolated_environment instead of the mock.
https://github.com/bbatsov/rubocop/blob/698bd2ee06aa365d85850a2ff23f71c46e0fab06/lib/rubocop/rspec/shared_contexts.rb#L6
it creates a temporary directory for the test.
I think that we should avoid mock as much as possible.

after { FileUtils.rm_f(lock_file_path) }

it 'uses the Rails version when Rails is present in the lock file' do
create_file(lock_file_path, "Foo\n rails (4.1.0)\n bar\n\n")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer using a more realistic lock file. For example:

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.1.0)
      actionpack (= 4.1.0)
      actionview (= 4.1.0)
      mail (~> 2.5.4)
    rails (4.1.0)
      actionmailer (= 4.1.0)
      actionpack (= 4.1.0)
      actionview (= 4.1.0)
      activemodel (= 4.1.0)
      activerecord (= 4.1.0)
      activesupport (= 4.1.0)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.1.0)
      sprockets-rails (~> 2.0)

PLATFORMS
  ruby

DEPENDENCIES
  rails (= 4.1.0)

BUNDLED WITH
   1.16.1

create_file(lock_file_path, "Foo\nbar\n\n")
default = RuboCop::Config::DEFAULT_RAILS_VERSION
expect(configuration.target_rails_version).to eq default
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need a test case for "when Gemfile.lock does not exist" case.

@roberts1000
Copy link
Contributor Author

Thanks for the feedback. I'll make the changes later today.

@roberts1000
Copy link
Contributor Author

I think I've made the necessary adjusts. It's ready for a second look.

end

def read_rails_version_from_bundler_lock_file
lock_file_path = bundler_lock_file_path
Copy link
Member

@koic koic Jan 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, If this cop search strictly for lockfile, it need to use the following Bundler's method.

Bundler.default_lockfile

Because the mechanism to search the lockfile is complicated as follows.
https://github.com/bundler/bundler/blob/v1.16.1/lib/bundler/shared_helpers.rb#L240-L246

Of course, now RuboCop doesn't depend on Bundler, so we can not use Bundler.default_lockfile. Please refer to when considering @pocke's advice 😃
#5473 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big concern right now.

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 24, 2018

It has to be rebased on top of the current master, but it looks OK to me overall. My only slight concern is that users might not ever realize such an inference is happening, which might not be a real problem in practice.

@roberts1000
Copy link
Contributor Author

roberts1000 commented Feb 25, 2018

Thanks for taking a look. I rebased the branch.

@bbatsov bbatsov merged commit f1540a2 into rubocop:master Feb 26, 2018
@bbatsov
Copy link
Collaborator

bbatsov commented Feb 26, 2018

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants