Skip to content

Commit 0a1d647

Browse files
authored
MONGOID-5566 fix breaking app_spec.rb when run locally (#5550)
* app_spec fails if MONGODB_URI is not set; let's relax that a bit * when possible, specify the Rails gem version when invoking 'rails new' * don't insert a blank string into the argument list
1 parent 636d533 commit 0a1d647

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

spec/integration/app_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ def check_call(cmd, **opts)
1010
Mrss::ChildProcessHelper.check_call(cmd, **opts)
1111
end
1212

13+
def gem_version_argument(version)
14+
"_#{version}_" if version
15+
end
16+
17+
def insert_rails_gem_version(cmd)
18+
gem_version = gem_version_argument(SpecConfig.instance.installed_rails_version)
19+
cmd.tap { cmd[1,0] = gem_version if gem_version }
20+
end
21+
1322
describe 'Mongoid application tests' do
1423
before(:all) do
1524
unless SpecConfig.instance.app_tests?
@@ -91,7 +100,7 @@ def prepare_new_rails_app(name)
91100

92101
Dir.chdir(TMP_BASE) do
93102
FileUtils.rm_rf(name)
94-
check_call(%W(rails new #{name} --skip-spring --skip-active-record), env: clean_env)
103+
check_call(insert_rails_gem_version(%W(rails new #{name} --skip-spring --skip-active-record)), env: clean_env)
95104

96105
Dir.chdir(name) do
97106
adjust_rails_defaults
@@ -265,7 +274,7 @@ def build_mongodb_uri(parts)
265274
def write_mongoid_yml
266275
# HACK: the driver does not provide a MongoDB URI parser and assembler,
267276
# and the Ruby standard library URI module doesn't handle multiple hosts.
268-
parts = parse_mongodb_uri(SpecConfig.instance.safe_uri)
277+
parts = parse_mongodb_uri(SpecConfig.instance.uri_str)
269278
parts[:database] = 'mongoid_test'
270279
uri = build_mongodb_uri(parts)
271280
p uri

spec/support/spec_config.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,26 @@
55
class SpecConfig
66
include Singleton
77

8+
DEFAULT_MONGODB_URI = "mongodb://127.0.0.1:27017"
9+
810
def initialize
911
if ENV['MONGODB_URI']
1012
@uri_str = ENV['MONGODB_URI']
11-
@uri = Mongo::URI.new(@uri_str)
12-
end
13-
end
14-
15-
attr_reader :uri_str
16-
attr_reader :uri
17-
18-
def addresses
19-
if @uri
20-
@uri.servers
2113
else
2214
STDERR.puts "Environment variable 'MONGODB_URI' is not set, so the default url will be used."
2315
STDERR.puts "This may lead to unexpected test failures because service discovery will raise unexpected warnings."
2416
STDERR.puts "Please consider providing the correct uri via MONGODB_URI environment variable."
25-
['127.0.0.1:27017']
17+
@uri_str = DEFAULT_MONGODB_URI
2618
end
19+
20+
@uri = Mongo::URI.new(@uri_str)
2721
end
2822

29-
# returns the URI string, or constructs one from the defaults if no URI
30-
# string was given.
31-
def safe_uri
32-
@uri_str || "mongodb://#{addresses.first}"
23+
attr_reader :uri_str
24+
attr_reader :uri
25+
26+
def addresses
27+
@uri.servers
3328
end
3429

3530
def mri?
@@ -67,4 +62,19 @@ def rails_version
6762
end
6863
v || '6.1'
6964
end
65+
66+
# Scrapes the output of `gem list` to find which versions of Rails are
67+
# installed, and looks for first one that best matches whatever rails version
68+
# was requested (see `#rails_version`).
69+
#
70+
# @return [ String | nil ] the version of the requested Rails install, or
71+
# nil if nothing matches.
72+
def installed_rails_version
73+
output = `gem list --exact rails`
74+
if output =~ /^rails \((.*)\)/
75+
versions = $1.split(/,\s*/)
76+
rails_version_re = /^#{rails_version}(?:\..*)?$/
77+
versions.detect { |v| v =~ rails_version_re }
78+
end
79+
end
7080
end

0 commit comments

Comments
 (0)