FIX: Test output related to `Discourse::VERSION`

It's a little awkward to test constants by re-assigning them so
I've added a new parameter to `Discourse.find_compatible_resource`
which can be used by tests.
This commit is contained in:
Robin Ward 2020-07-09 14:57:27 -04:00
parent c2ce7f2673
commit b1c6ff9e1c
2 changed files with 10 additions and 20 deletions

View File

@ -1,9 +1,8 @@
# frozen_string_literal: true
module Discourse
VERSION_REGEXP = /\A\d+\.\d+\.\d+(\.beta\d+)?\z/ unless defined? ::Discourse::VERSION_REGEXP
VERSION_COMPATIBILITY_FILENAME = ".discourse-compatibility"
VERSION_REGEXP ||= /\A\d+\.\d+\.\d+(\.beta\d+)?\z/
VERSION_COMPATIBILITY_FILENAME ||= ".discourse-compatibility"
# work around reloader
unless defined? ::Discourse::VERSION
@ -29,19 +28,19 @@ module Discourse
# 2.5.0.beta2: bbffee
# 2.4.4.beta6: some-other-branch-ref
# 2.4.2.beta1: v1-tag
def self.find_compatible_resource(version_list)
def self.find_compatible_resource(version_list, version = ::Discourse::VERSION::STRING)
return unless version_list
version_list = YAML.load(version_list).sort_by { |version, pin| Gem::Version.new(version) }.reverse
version_list = YAML.load(version_list).sort_by { |v, pin| Gem::Version.new(v) }.reverse
# If plugin compat version is listed as less than current Discourse version, take the version/hash listed before.
checkout_version = nil
version_list.each do |core_compat, target|
if Gem::Version.new(core_compat) == Gem::Version.new(::Discourse::VERSION::STRING) # Exact version match - return it
if Gem::Version.new(core_compat) == Gem::Version.new(version) # Exact version match - return it
checkout_version = target
break
elsif Gem::Version.new(core_compat) < Gem::Version.new(::Discourse::VERSION::STRING) # Core is on a higher version than listed, use a later version
elsif Gem::Version.new(core_compat) < Gem::Version.new(version) # Core is on a higher version than listed, use a later version
break
end
checkout_version = target

View File

@ -48,30 +48,21 @@ describe Discourse::VERSION do
end
context "compatible_resource" do
after do
# Cleanup versions
::Discourse::VERSION::STRING = [::Discourse::VERSION::MAJOR, ::Discourse::VERSION::MINOR, ::Discourse::VERSION::TINY, ::Discourse::VERSION::PRE].compact.join('.')
end
shared_examples "test compatible resource" do
it "returns nil when the current version is above all pinned versions" do
::Discourse::VERSION::STRING = "2.6.0"
expect(Discourse.find_compatible_resource(version_list)).to be_nil
expect(Discourse.find_compatible_resource(version_list, "2.6.0")).to be_nil
end
it "returns the correct version if matches exactly" do
::Discourse::VERSION::STRING = "2.5.0.beta4"
expect(Discourse.find_compatible_resource(version_list)).to eq("twofivebetafour")
expect(Discourse.find_compatible_resource(version_list, "2.5.0.beta4")).to eq("twofivebetafour")
end
it "returns the closest matching version" do
::Discourse::VERSION::STRING = "2.4.6.beta12"
expect(Discourse.find_compatible_resource(version_list)).to eq("twofivebetatwo")
expect(Discourse.find_compatible_resource(version_list, "2.4.6.beta12")).to eq("twofivebetatwo")
end
it "returns the lowest version possible when using an older version" do
::Discourse::VERSION::STRING = "1.4.6.beta12"
expect(Discourse.find_compatible_resource(version_list)).to eq("twofourtwobetaone")
expect(Discourse.find_compatible_resource(version_list, "1.4.6.beta12")).to eq("twofourtwobetaone")
end
end