Skip to content

Commit

Permalink
User Gem:::Version for Node version comparisons
Browse files Browse the repository at this point in the history
Simplify writing of `source` to test Gemfile
  • Loading branch information
louis-antonopoulos committed Oct 30, 2024
1 parent b8a7db1 commit 83817db
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
10 changes: 8 additions & 2 deletions lib/install/web.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
def node_version
ENV["NODE_VERSION"] || `node --version`[/\d+\.\d+\.\d+/]
version = ENV["NODE_VERSION"] || `node --version`[/\d+\.\d+\.\d+/]

return if version.blank?

Gem::Version.new(version)
end

def node_not_installed?
node_version.blank?
end

def node_version_unsupported?
node_version < "20.0.0"
minimum_node_version = Gem::Version.new("20.0.0")

node_version < minimum_node_version
end

def apply_template!
Expand Down
10 changes: 8 additions & 2 deletions lib/suspenders/generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ def rspec_test_helper_present?
end

def node_version
ENV["NODE_VERSION"] || `node --version`[/\d+\.\d+\.\d+/]
version = ENV["NODE_VERSION"] || `node --version`[/\d+\.\d+\.\d+/]

return if version.blank?

Gem::Version.new(version)
end

def node_not_installed?
node_version.blank?
end

def node_version_unsupported?
node_version < Suspenders::MINIMUM_NODE_VERSION
minimum_node_version = Gem::Version.new(Suspenders::MINIMUM_NODE_VERSION)

node_version < minimum_node_version
end
end

Expand Down
24 changes: 21 additions & 3 deletions test/generators/suspenders/install/web_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,32 @@ class WebGeneratorTest < Rails::Generators::TestCase
end
end

test "evaluates support for Node versions correctly" do
web_generator = Generators::Install::WebGenerator.new

unsupported_versions = %w[1.0.0 1.100.200 10.0.0 19.0.0 19.9.9 19.9999.99999]

unsupported_versions.each do |unsupported_version|
Generators::Install::WebGenerator.any_instance.stubs(:node_version).returns(unsupported_version)

assert_predicate web_generator, :node_version_unsupported?, "Node version #{unsupported_version} should not be supported"
end

supported_versions = %w[20.0.0 20.1.0 20.100.200 50.0.0 100.0.0]

supported_versions.each do |supported_version|
Generators::Install::WebGenerator.any_instance.stubs(:node_version).returns(supported_version)

assert_not_predicate web_generator, :node_version_unsupported?, "Node version #{supported_version} should be supported"
end
end

private

def prepare_destination
touch "Gemfile"

File.open("test/dummy/Gemfile", "w") do |f|
f.write('source "https://rubygems.org"')
end
File.write("test/dummy/Gemfile", 'source "https://rubygems.org"')
end

def restore_destination
Expand Down

0 comments on commit 83817db

Please sign in to comment.