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

Use Gem::Version to compare versions #1236

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

louis-antonopoulos
Copy link
Contributor

This PR:

  • Uses Gem::Version to determine if Node is the correct version
  • Fixes a potential warning in WebGeneratorTest

String comparison of version numbers can produce incorrect results. For example, "20.10.0" < "20.2.0" resolves to true.

Comment on lines 76 to 78
File.open("test/dummy/Gemfile", "w") do |gemfile|
gemfile.write('source "https://rubygems.org"')
end
Copy link
Contributor Author

@louis-antonopoulos louis-antonopoulos Oct 30, 2024

Choose a reason for hiding this comment

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

While working in this file, if nothing was raised when a run_generator command ran, then there was a big slowdown and the following message would appear multiple times:

...[DEPRECATED] This Gemfile does not include an explicit global 
source. Not using an explicit global source may result in a different 
lockfile being generated depending on the gems you have 
installed locally before bundler is run. Instead, define a global 
source in your Gemfile like this: source "https://rubygems.org".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding source "https://rubygems.org" to the Gemfile resolved this warning, making it easier to test the tests in the file.

Note that run_generator still fails for different reasons, but it's a faster fail with less noise.

Comment on lines +4 to +6
return if version.blank?

Gem::Version.new(version)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think these two lines are clearer as written or combined like this?

Gem::Version.new(version) unless version.blank?

end

def node_not_installed?
!node_version.present?
node_version.blank?
Copy link
Contributor Author

@louis-antonopoulos louis-antonopoulos Oct 30, 2024

Choose a reason for hiding this comment

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

I changed this to avoid the double negative of not_installed and
!(...).present?.

@louis-antonopoulos louis-antonopoulos force-pushed the use-gem-version-to-compare-versions branch 6 times, most recently from 483c690 to b0015d3 Compare October 30, 2024 15:46
[DEPRECATED] This Gemfile does not include an explicit global source. Not using an explicit global source may result in a different lockfile being generated depending on the gems you have installed locally before bundler is run. Instead, define a global source in your Gemfile like this: source "https://rubygems.org".
Simplify writing of source to Gemfile
@louis-antonopoulos louis-antonopoulos force-pushed the use-gem-version-to-compare-versions branch from b0015d3 to 579a24d Compare November 1, 2024 20:12
Comment on lines +2 to +6
version = ENV["NODE_VERSION"] || `node --version`[/\d+\.\d+\.\d+/]

return if version.blank?

Gem::Version.new(version)
Copy link

Choose a reason for hiding this comment

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

Would the following work?

Suggested change
version = ENV["NODE_VERSION"] || `node --version`[/\d+\.\d+\.\d+/]
return if version.blank?
Gem::Version.new(version)
if version = ENV.fetch(ENV["NODE_VERSION"], `node --version`[/\d+\.\d+\.\d+/])
Gem::Version.new(version)
else
""
end

It solves a couple of issues.

  1. Deals with #[] returning something falsy but not nil
  2. Gets rid of the trailing conditional and makes the method more idiomatic
  3. Ensures the method always returns a string

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.

2 participants