diff --git a/lib/install/web.rb b/lib/install/web.rb index b071c4d7..b68567a1 100644 --- a/lib/install/web.rb +++ b/lib/install/web.rb @@ -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.present? + 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! diff --git a/lib/suspenders/generators.rb b/lib/suspenders/generators.rb index 9d342604..d0c085d4 100644 --- a/lib/suspenders/generators.rb +++ b/lib/suspenders/generators.rb @@ -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.present? + 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 diff --git a/test/generators/suspenders/install/web_generator_test.rb b/test/generators/suspenders/install/web_generator_test.rb index b2740732..fac8f2cb 100644 --- a/test/generators/suspenders/install/web_generator_test.rb +++ b/test/generators/suspenders/install/web_generator_test.rb @@ -48,10 +48,32 @@ class WebGeneratorTest < Rails::Generators::TestCase end end + test "evaluates supported 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.write("test/dummy/Gemfile", 'source "https://rubygems.org"') end def restore_destination