Skip to content

Commit 57be856

Browse files
BuonOmorafiss
authored andcommitted
refactor(Gemfile): Simplify rails loading
The `RAILS_SOURCE` env was not documented, and the methods were not up-to-date (and crashing). I think this is not necessary as we can manually change the `gem` specification for testing. I added a related comment in the Gemfile. `RAILS_VERSION` has basically the same problem: we can manually change the tag if we need to make a specific test. As the code to fetch the latest version of Rails wasn't working anymore, I changed it to something more robust, taking advantage of the `Gem::Version` and `Gem::Requirement` classes. And without relying on `eval`
1 parent 6b969d0 commit 57be856

File tree

3 files changed

+36
-54
lines changed

3 files changed

+36
-54
lines changed

CONTRIBUTING.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ To run a specific test case, use minitest's `-n` option to run tests that match
9090
TEST_FILES="test/cases/adapter_test.rb" TESTOPTS=`-n=/test_indexes/` bundle exec rake test
9191
```
9292

93-
By default, tests will be run from the bundled version of Rails. To run against a local copy, set environemnt variable `RAILS_SOURCE`. Running against a local copy of Rails can be helpful when try to debug issues.
94-
95-
```bash
96-
RAILS_SOURCE="path/to/local_copy" bundle exec rake test
97-
```
98-
9993
`test/config.yml` assumes CockroachDB will be running at localhost:26257 with a root user. Make changes to `test/config.yml` as needed.
10094

10195
### Run Tests from a Backup

Gemfile

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,49 @@
1-
require 'openssl'
2-
source 'https://rubygems.org'
3-
gemspec
1+
# frozen_string_literal: true
42

5-
if ENV['RAILS_SOURCE']
6-
gemspec path: ENV['RAILS_SOURCE']
7-
else
8-
def get_version_from_gemspec
9-
gemspec = eval(File.read('activerecord-cockroachdb-adapter.gemspec'))
3+
source "https://rubygems.org"
104

11-
gem_version = gemspec.dependencies.
12-
find { |dep| dep.name == 'activerecord' }.
13-
requirement.
14-
requirements.
15-
first.
16-
last
5+
gemspec
176

18-
major, minor, tiny, pre = gem_version.segments
197

20-
if pre
21-
gem_version.to_s
22-
else
23-
find_latest_matching_version(major, minor)
8+
module RailsTag
9+
class << self
10+
def call
11+
req = gemspec_requirement
12+
"v" + all_activerecord_versions.find { req.satisfied_by?(_1) }.version
2413
end
25-
end
2614

27-
def find_latest_matching_version(gemspec_major, gemspec_minor)
28-
all_activerecord_versions.
29-
reject { |version| version["prerelease"] }.
30-
map { |version| version["number"].split(".").map(&:to_i) }.
31-
find { |major, minor|
32-
major == gemspec_major && (minor == gemspec_minor || gemspec_minor.nil?)
33-
}.join(".")
34-
end
15+
def gemspec_requirement
16+
File
17+
.foreach("activerecord-cockroachdb-adapter.gemspec", chomp: true)
18+
.find { _1[/add_dependency\s.activerecord.,\s.(.*)./] }
19+
20+
Gem::Requirement.new(Regexp.last_match(1))
21+
end
3522

36-
def all_activerecord_versions
37-
require 'net/http'
38-
require 'yaml'
23+
def all_activerecord_versions
24+
require 'net/http'
25+
require 'yaml'
3926

40-
uri = URI.parse "https://rubygems.org/api/v1/versions/activerecord.yaml"
41-
http = Net::HTTP.new(uri.host, uri.port)
42-
http.use_ssl = true
43-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
27+
uri = URI.parse "https://rubygems.org/api/v1/versions/activerecord.yaml"
28+
http = Net::HTTP.new(uri.host, uri.port)
29+
http.use_ssl = true
30+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
4431

45-
YAML.load(
46-
http.request(Net::HTTP::Get.new(uri.request_uri)).body
47-
)
32+
YAML.load(
33+
http.request(Net::HTTP::Get.new(uri.request_uri)).body
34+
).map { Gem::Version.new(_1["number"]) }
35+
end
4836
end
49-
50-
# Get Rails from source because the gem doesn't include tests
51-
version = ENV['RAILS_VERSION'] || get_version_from_gemspec
52-
gem 'rails', git: "https://github.com/rails/rails.git", tag: "v#{version}"
5337
end
5438

55-
group :development do
39+
40+
group :development, :test do
41+
# We need to load the gem from git to have access to activerecord's test files.
42+
# You can use `path: "some/local/rails"` if you want to test the gem against
43+
# a specific rails codebase.
44+
gem "rails", github: "rails/rails", tag: RailsTag.call
45+
46+
gem "rake"
5647
gem "byebug"
5748
gem "minitest-excludes", "~> 2.0.1"
5849

activerecord-cockroachdb-adapter.gemspec

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
# coding: utf-8
1+
# frozen_string_literal: true
22

3-
lib = File.expand_path('lib', __dir__)
4-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5-
6-
require './lib/version.rb'
3+
require_relative 'lib/version'
74
version = ActiveRecord::COCKROACH_DB_ADAPTER_VERSION
85

96
Gem::Specification.new do |spec|

0 commit comments

Comments
 (0)