Skip to content

Commit 4a4212c

Browse files
committed
fix rails tests
1 parent afad196 commit 4a4212c

File tree

3 files changed

+41
-73
lines changed

3 files changed

+41
-73
lines changed

Gemfile

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,7 @@ unless ENV["RUBY_VERSION"]
1111
ruby File.read(File.expand_path(".ruby-version", __dir__)).strip
1212
end
1313

14-
# Define Rails version based on environment variable
15-
rails_version = ENV["RAILS_VERSION"] || "7.0"
16-
17-
case rails_version
18-
when "7.0"
19-
gem "rails", "~> 7.0"
20-
when "7.1"
21-
gem "rails", "~> 7.1"
22-
when "8.0"
23-
gem "rails", "~> 8.0"
24-
end
14+
gem "rails", "~> #{ENV["RAILS_VERSION"] || "7.2.2.1"}", require: false
2515

2616
# Add these gems to silence Ruby 3.4 warnings
2717
gem "bigdecimal"

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ DEPENDENCIES
423423
minitest (~> 5.20)
424424
minitest-reporters (~> 1.6)
425425
mutex_m
426-
rails (~> 7.0)
426+
rails (~> 7.2.2.1)
427427
redcarpet
428428
rollbar (~> 3.4)
429429
rubocop

rails_test_app/create_app.rb

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@
2525
rails_version = latest_version
2626
end
2727

28-
2928
# Get currently installed Rails versions
3029
rails_gems = `gem list rails -l`
3130
installed_versions = rails_gems.scan(/rails \(([^)]+)\)/).flatten.first&.split(", ") || []
3231

33-
3432
# Check if we need to install this version
3533
if !installed_versions.include?(rails_version)
3634
puts "Rails #{rails_version} not found. Installing..."
@@ -39,12 +37,32 @@
3937
puts "Rails #{rails_version} already installed"
4038
end
4139

40+
# Fix for Rails 7.0 compatibility with concurrent-ruby
41+
# (Have to run this before bundler/setup)
42+
# See: https://github.com/rails/rails/pull/54264
43+
if rails_version.start_with?("7.0")
44+
puts "Rails 7.0 detected - checking concurrent-ruby version..."
45+
46+
# Check if concurrent-ruby 1.3.5+ is installed
47+
gem_list_output = `gem list concurrent-ruby`
48+
puts "gem_list_output: #{gem_list_output}"
49+
if gem_list_output.include?("1.3.5")
50+
puts "Downgrading concurrent-ruby to 1.3.4 for Rails 7.0 compatibility..."
51+
# First uninstall the incompatible version
52+
system("gem uninstall concurrent-ruby -v '>= 1.3.5' -I")
53+
end
54+
55+
# Always make sure 1.3.4 is installed
56+
puts "Installing concurrent-ruby 1.3.4 for Rails 7.0 compatibility..."
57+
system("gem install concurrent-ruby -v '1.3.4' --no-document")
4258

43-
# Don't require bundler/setup - we want to avoid loading our project's Gemfile
59+
# Update GEM_PATH so the gem is available to this script
60+
ENV["GEM_PATH"] = `gem env gempath`.strip
61+
end
4462

63+
require "bundler/setup"
4564
require "fileutils"
4665
require "erb"
47-
require "sorbet-runtime"
4866

4967
# Make the rails_version available as an instance variable for the ERB templates
5068
@rails_version = rails_version
@@ -54,14 +72,17 @@
5472
TEMPLATE_DIR = File.expand_path("templates", __dir__)
5573
RAILS_APP_DIR = File.expand_path("logstruct_test_app", __dir__)
5674

57-
# Use a clean environment with minimal variables
75+
# IMPORTANT - Use a clean environment with minimal variables
76+
# Requiring Bundler and Rails sets a bunch of environment variables that break everything
77+
# if we're not careful.
5878
clean_env = {
5979
"PATH" => ENV["PATH"],
6080
"HOME" => ENV["HOME"],
6181
"RAILS_ENV" => "development",
6282
"RAILS_VERSION" => nil,
6383
"RUBY_VERSION" => nil,
6484
"BUNDLE_GEMFILE" => nil,
85+
"BUNDLER_SETUP" => nil,
6586
"GEM_HOME" => nil,
6687
"GEM_PATH" => nil,
6788
"RUBYOPT" => nil
@@ -71,32 +92,11 @@
7192
skip_app_creation = ENV["SKIP_APP_CREATION"] == "true"
7293

7394
# Extract major and minor version for migrations and load_defaults
74-
@rails_major_minor = T.must(rails_version.split(".")[0..1]).join(".")
95+
@rails_major_minor = (rails_version.split(".")[0..1] || []).join(".")
7596

7697
# Create directories
7798
FileUtils.mkdir_p(RAILS_APP_DIR)
7899

79-
# Fix for Rails 7.0 compatibility with concurrent-ruby
80-
# See: https://github.com/rails/rails/pull/54264
81-
if rails_version == "7.0"
82-
puts "Rails 7.0 detected - checking concurrent-ruby version..."
83-
84-
# Check if concurrent-ruby 1.3.5+ is installed
85-
gem_list_output = `gem list concurrent-ruby`
86-
if gem_list_output.include?("1.3.5")
87-
puts "Downgrading concurrent-ruby to 1.3.4 for Rails 7.0 compatibility..."
88-
# First uninstall the incompatible version
89-
system("gem uninstall concurrent-ruby -v '>= 1.3.5' -I")
90-
end
91-
92-
# Always make sure 1.3.4 is installed
93-
puts "Installing concurrent-ruby 1.3.4 for Rails 7.0 compatibility..."
94-
system("gem install concurrent-ruby -v '1.3.4' --no-document")
95-
96-
# Update GEM_PATH so the gem is available to this script
97-
ENV["GEM_PATH"] = `gem env gempath`.strip
98-
end
99-
100100
# Copy template files into the test app
101101
def copy_template(file, target_path = nil)
102102
source = File.join(TEMPLATE_DIR, file)
@@ -133,30 +133,6 @@ def copy_template(file, target_path = nil)
133133
puts "=> Deleting default .rubocop.yml"
134134
FileUtils.rm_f(File.join(RAILS_APP_DIR, ".rubocop.yml"))
135135

136-
# Verify we're using the correct Rails version defaults
137-
if rails_version.start_with?("8.0")
138-
defaults_dir = File.join(RAILS_APP_DIR, "config/initializers")
139-
rails72_defaults = File.join(defaults_dir, "new_framework_defaults_7_2.rb")
140-
rails80_defaults = File.join(defaults_dir, "new_framework_defaults_8_0.rb")
141-
142-
# Check if we have the wrong defaults file
143-
if File.exist?(rails72_defaults) && !File.exist?(rails80_defaults)
144-
abort(<<~ERROR)
145-
ERROR: Expected Rails 8.0 but found Rails 7.2 defaults file!
146-
147-
This indicates a serious problem with how Rails is being executed.
148-
You have Rails 8.0.1 installed, but the generator is using Rails 7.2 behavior.
149-
150-
Possible solutions to try:
151-
1. Make sure your Ruby environment is set up correctly
152-
2. Try using a direct path to the rails executable
153-
3. Try running the command manually outside of this script
154-
155-
Command attempted: #{rails_new_command}
156-
ERROR
157-
end
158-
end
159-
160136
# Update Gemfile to include the local logstruct gem and test gems
161137
gemfile_path = File.join(RAILS_APP_DIR, "Gemfile")
162138
gemfile_content = File.read(gemfile_path)
@@ -171,14 +147,11 @@ def copy_template(file, target_path = nil)
171147
end
172148

173149
# Common gems for all Rails versions
174-
# Common gems for all Rails versions
175-
gem "bigdecimal"
176-
gem "mutex_m"
177-
gem "drb"
178-
gem "benchmark"
179-
180-
# Add test gems
181-
test_gems = <<~GEMS
150+
gemfile_content += <<~GEMS
151+
gem "bigdecimal"
152+
gem "mutex_m"
153+
gem "drb"
154+
gem "benchmark"
182155
183156
# Test gems
184157
group :test do
@@ -187,8 +160,14 @@ def copy_template(file, target_path = nil)
187160
gem 'simplecov-json'
188161
end
189162
GEMS
190-
gemfile_content += test_gems
191163

164+
# Have to pin concurrent-ruby to 1.3.4 for Rails 7.0 compatibility
165+
if rails_version.start_with?("7.0")
166+
gemfile_content += <<~GEMS
167+
gem "logger"
168+
gem "concurrent-ruby", "<= 1.3.4"
169+
GEMS
170+
end
192171
File.write(gemfile_path, gemfile_content)
193172

194173
# Run initial bundle install
@@ -212,7 +191,6 @@ def copy_template(file, target_path = nil)
212191
copy_template(relative_path)
213192
end
214193

215-
216194
# Set up the database
217195
puts "Setting up Rails application in #{RAILS_APP_DIR}..."
218196
Dir.chdir(RAILS_APP_DIR) do

0 commit comments

Comments
 (0)