Skip to content

Commit 847edcd

Browse files
justin808claude
andauthored
Improve validation skip documentation and test coverage (#1924)
* Improve validation skip documentation and test coverage Addresses feedback from PR #1923: 1. Enhanced documentation: - Added comprehensive method-level docs explaining ENV variable approach - Documented why ENV is preferred over ARGV (Rails modifies ARGV during init) - Added thread safety notes explaining process-global ENV scope - Cross-referenced engine.rb and install_generator.rb for clarity 2. Improved test coverage: - Added tests verifying ENV variable takes precedence over other checks - Tests confirm ENV check short-circuits before package.json/ARGV checks - Added 4 new test cases covering precedence scenarios 3. Thread safety documentation: - Documented that generators run in single process (not a concern) - Added note about parallel test scenarios (separate processes) - Clarified ENV cleanup in ensure block Changes: - lib/react_on_rails/engine.rb: Enhanced skip_validation? docs - lib/generators/react_on_rails/install_generator.rb: Added run_generators docs - spec/react_on_rails/engine_spec.rb: Added 4 precedence tests All specs pass (21 examples, 0 failures) RuboCop clean 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Refine documentation and test structure per code review Addresses additional code review suggestions: 1. Edge Case Documentation: - Added @note about manual ENV variable setting - Documented use case for debugging/setup scenarios - Warned against bypassing validation in production 2. Improved Test Structure: - Refactored precedence tests to use nested contexts - Changed structure: "with other skip conditions also present" -> "when package.json exists and ARGV indicates generator" -> "when package.json is missing" - More descriptive test names focusing on behavior - Better organization showing ENV precedence clearly 3. Simplified Comments: - Reduced redundancy in install_generator.rb ensure block - Changed from 2 lines to 1 concise line - Removed redundant statement about concurrent generators Changes maintain all functionality while improving readability and addressing edge cases in documentation. All specs pass (21 examples, 0 failures) RuboCop clean 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent ae5425b commit 847edcd

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

lib/generators/react_on_rails/install_generator.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,23 @@ class InstallGenerator < Rails::Generators::Base
3636

3737
# Removed: --skip-shakapacker-install (Shakapacker is now a required dependency)
3838

39+
# Main generator entry point
40+
#
41+
# Sets up React on Rails in a Rails application by:
42+
# 1. Validating prerequisites
43+
# 2. Installing required packages
44+
# 3. Generating configuration files
45+
# 4. Setting up example components
46+
#
47+
# @note Validation Skipping: Sets ENV["REACT_ON_RAILS_SKIP_VALIDATION"] to prevent
48+
# version validation from running during generator execution. The npm package
49+
# isn't installed until midway through the generator, so validation would fail
50+
# if run during Rails initialization. The ensure block guarantees cleanup even
51+
# if the generator fails.
3952
def run_generators
4053
# Set environment variable to skip validation during generator run
4154
# This is inherited by all invoked generators and persists through Rails initialization
55+
# See lib/react_on_rails/engine.rb for the validation skip logic
4256
ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = "true"
4357

4458
if installation_prerequisites_met? || options.ignore_warnings?
@@ -59,6 +73,7 @@ def run_generators
5973
GeneratorMessages.add_error(error)
6074
end
6175
ensure
76+
# Always clean up ENV variable, even if generator fails
6277
ENV.delete("REACT_ON_RAILS_SKIP_VALIDATION")
6378
print_generator_messages
6479
end

lib/react_on_rails/engine.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,28 @@ class Engine < ::Rails::Engine
1818
end
1919

2020
# Determine if version validation should be skipped
21+
#
22+
# This method checks multiple conditions to determine if package version validation
23+
# should be skipped. Validation is skipped during setup scenarios where the npm
24+
# package isn't installed yet (e.g., during generator execution).
25+
#
2126
# @return [Boolean] true if validation should be skipped
27+
#
28+
# @note Thread Safety: ENV variables are process-global. In practice, Rails generators
29+
# run in a single process, so concurrent execution is not a concern. If running
30+
# generators concurrently (e.g., in parallel tests), ensure tests run in separate
31+
# processes to avoid ENV variable conflicts.
32+
#
33+
# @note Manual ENV Setting: While this ENV variable is designed to be set by generators,
34+
# users can manually set it (e.g., `REACT_ON_RAILS_SKIP_VALIDATION=true rails server`)
35+
# to bypass validation. This should only be done temporarily during debugging or
36+
# setup scenarios. The validation helps catch version mismatches early, so bypassing
37+
# it in production is not recommended.
2238
def self.skip_version_validation?
2339
# Skip if explicitly disabled via environment variable (set by generators)
40+
# Using ENV variable instead of ARGV because Rails can modify/clear ARGV during
41+
# initialization, making ARGV unreliable for detecting generator context. The ENV
42+
# variable persists through the entire Rails initialization process.
2443
if ENV["REACT_ON_RAILS_SKIP_VALIDATION"] == "true"
2544
Rails.logger.debug "[React on Rails] Skipping validation - disabled via environment variable"
2645
return true
@@ -33,6 +52,7 @@ def self.skip_version_validation?
3352
end
3453

3554
# Skip during generator runtime since packages are installed during execution
55+
# This is a fallback check in case ENV wasn't set, though ENV is the primary mechanism
3656
if running_generator?
3757
Rails.logger.debug "[React on Rails] Skipping validation during generator runtime"
3858
return true

spec/react_on_rails/engine_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,45 @@ module ReactOnRails
3131
expect(Rails.logger).to have_received(:debug)
3232
.with("[React on Rails] Skipping validation - disabled via environment variable")
3333
end
34+
35+
context "with other skip conditions also present" do
36+
context "when package.json exists and ARGV indicates generator" do
37+
before do
38+
allow(File).to receive(:exist?).with(package_json_path).and_return(true)
39+
stub_const("ARGV", ["generate", "react_on_rails:install"])
40+
end
41+
42+
it "prioritizes ENV over ARGV check" do
43+
expect(described_class.skip_version_validation?).to be true
44+
end
45+
46+
it "short-circuits before checking ARGV" do
47+
described_class.skip_version_validation?
48+
expect(Rails.logger).to have_received(:debug)
49+
.with("[React on Rails] Skipping validation - disabled via environment variable")
50+
expect(Rails.logger).not_to have_received(:debug)
51+
.with("[React on Rails] Skipping validation during generator runtime")
52+
end
53+
end
54+
55+
context "when package.json is missing" do
56+
before do
57+
allow(File).to receive(:exist?).with(package_json_path).and_return(false)
58+
end
59+
60+
it "prioritizes ENV over package.json check" do
61+
expect(described_class.skip_version_validation?).to be true
62+
end
63+
64+
it "short-circuits before checking package.json" do
65+
described_class.skip_version_validation?
66+
expect(Rails.logger).to have_received(:debug)
67+
.with("[React on Rails] Skipping validation - disabled via environment variable")
68+
expect(Rails.logger).not_to have_received(:debug)
69+
.with("[React on Rails] Skipping validation - package.json not found")
70+
end
71+
end
72+
end
3473
end
3574

3675
context "when package.json doesn't exist" do

0 commit comments

Comments
 (0)