Skip to content

Commit

Permalink
Prerequisites: Raise if Node version unsupported
Browse files Browse the repository at this point in the history
Follow-up to #1201

It's not enough to ensure Node is installed. We also need to ensure the
consumer has the supported minimum version installed. Otherwise,
subsequent generators will raise errors like so:

```
error [email protected]: The engine "node" is incompatible with this module. Expected version ">=18.12.0". Got "18.0.0"
error Found incompatible module.
```

We select `v20.0.0` as our minimum supported version because it is
slated for Active LTS, but is not bleeding edge at [this time][]

[this time]: https://nodejs.org/en/about/previous-releases
  • Loading branch information
stevepolitodesign committed May 8, 2024
1 parent 7bf6e82 commit 2d9e64e
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/generators/suspenders/install/web_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class WebGenerator < Rails::Generators::Base
include Suspenders::Generators::APIAppUnsupported
include Suspenders::Generators::DatabaseUnsupported
include Suspenders::Generators::NodeNotInstalled
include Suspenders::Generators::NodeVersionUnsupported

source_root File.expand_path("../../../templates/install/web", __FILE__)
desc <<~MARKDOWN
Expand Down
1 change: 1 addition & 0 deletions lib/generators/suspenders/prerequisites_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Generators
class PrerequisitesGenerator < Rails::Generators::Base
include Suspenders::Generators::Helpers
include Suspenders::Generators::NodeNotInstalled
include Suspenders::Generators::NodeVersionUnsupported

source_root File.expand_path("../../templates/prerequisites", __FILE__)

Expand Down
23 changes: 23 additions & 0 deletions lib/suspenders/generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def database_unsupported?
end

module NodeNotInstalled
# TODO: We should add a message
# TODO: Add coverage for this
class Error < StandardError
end

Expand All @@ -99,5 +101,26 @@ def node_not_installed?
!node_version.present?
end
end

module NodeVersionUnsupported
class Error < StandardError
end

extend ActiveSupport::Concern

included do
def raise_if_node_version_unsupported
if node_version_unsupported?
raise Suspenders::Generators::NodeVersionUnsupported::Error
end
end
end

private

def node_version_unsupported?
node_version < Suspenders::MINIMUM_NODE_VERSION
end
end
end
end
1 change: 1 addition & 0 deletions lib/suspenders/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module Suspenders
VERSION = "3.0.0".freeze
RAILS_VERSION = "~> 7.0".freeze
MINIMUM_RUBY_VERSION = ">= 3.1".freeze
MINIMUM_NODE_VERSION = "20.0.0".freeze
end
10 changes: 10 additions & 0 deletions test/generators/suspenders/install/web_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ class WebGeneratorTest < Rails::Generators::TestCase
end
end

test "raises if Node is unsupported" do
Object.any_instance.stubs(:`).returns("v19.9.9\n")

with_database "postgresql" do
assert_raises Suspenders::Generators::NodeVersionUnsupported::Error do
run_generator
end
end
end

private

def prepare_destination
Expand Down
18 changes: 14 additions & 4 deletions test/generators/suspenders/prerequisites_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ class PrerequisitesGeneratorTest < Rails::Generators::TestCase
teardown :restore_destination

test "generates .node-version file (from ENV)" do
ClimateControl.modify NODE_VERSION: "1.2.3" do
ClimateControl.modify NODE_VERSION: "20.0.0" do
run_generator

assert_file app_root(".node-version") do |file|
assert_match(/1\.2\.3/, file)
assert_match(/20\.0\.0/, file)
end
end
end

test "generates .node-version file (from system)" do
Object.any_instance.stubs(:`).returns("v1.2.3\n")
Object.any_instance.stubs(:`).returns("v20.0.0\n")

run_generator

assert_file app_root(".node-version") do |file|
assert_match(/1\.2\.3/, file)
assert_match(/20\.0\.0/, file)
end
end

Expand All @@ -41,6 +41,16 @@ class PrerequisitesGeneratorTest < Rails::Generators::TestCase
assert_no_file app_root(".node-version")
end

test "raises if Node is unsupported" do
Object.any_instance.stubs(:`).returns("v19.9.9\n")

assert_raises Suspenders::Generators::NodeVersionUnsupported::Error do
run_generator
end

assert_no_file app_root(".node-version")
end

private

def restore_destination
Expand Down
4 changes: 2 additions & 2 deletions test/suspenders/cleanup/generate_readme_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Suspenders
module Cleanup
class GenerateReadmeTest < ActiveSupport::TestCase
test "generates README using generator descriptions" do
Object.any_instance.stubs(:`).returns("v1.2.3\n")
Object.any_instance.stubs(:`).returns("v20.0.0\n")

Tempfile.create "README.md" do |readme|
path = readme.path
Expand All @@ -20,7 +20,7 @@ class GenerateReadmeTest < ActiveSupport::TestCase

assert_match "## Prerequisites", readme
assert_match Suspenders::MINIMUM_RUBY_VERSION, readme
assert_match "Node: `1.2.3`", readme
assert_match "Node: `20.0.0`", readme

assert_match "## Configuration", readme
assert_match "### Test", readme
Expand Down
4 changes: 4 additions & 0 deletions test/suspenders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ class SuspendersTest < ActiveSupport::TestCase
test "it has a Minimum Ruby version number" do
assert Suspenders::MINIMUM_RUBY_VERSION
end

test "it has a Minimum Node version number" do
assert Suspenders::MINIMUM_NODE_VERSION
end
end

0 comments on commit 2d9e64e

Please sign in to comment.