From 83817db4c609495e486460afa6921e64d70797d6 Mon Sep 17 00:00:00 2001 From: Louis Antonopoulos Date: Tue, 29 Oct 2024 19:34:34 -0400 Subject: [PATCH] User Gem:::Version for Node version comparisons Simplify writing of `source` to test Gemfile --- lib/install/web.rb | 10 ++++++-- lib/suspenders/generators.rb | 10 ++++++-- .../suspenders/install/web_generator_test.rb | 24 ++++++++++++++++--- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/install/web.rb b/lib/install/web.rb index 8f89ccd6..b68567a1 100644 --- a/lib/install/web.rb +++ b/lib/install/web.rb @@ -1,5 +1,9 @@ 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? @@ -7,7 +11,9 @@ def node_not_installed? 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 78cf37c2..d0c085d4 100644 --- a/lib/suspenders/generators.rb +++ b/lib/suspenders/generators.rb @@ -20,7 +20,11 @@ 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? @@ -28,7 +32,9 @@ def node_not_installed? 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 8e9ff1b0..fcf719aa 100644 --- a/test/generators/suspenders/install/web_generator_test.rb +++ b/test/generators/suspenders/install/web_generator_test.rb @@ -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