FIX: Handle .discourse-compatibility syntax errors (#10891)

Previously, any errors in those files would e.g. blow up the update process in docker_manager.
Now it prints out an error and proceeds as if there was no compatibility file.

Includes:

* DEV: Extract setup_git_repo
* DEV: Use `Dir.mktmpdir`
* DEV: Default to `main` branch (The latest versions of git already do this, so to avoid problems do this by default)
This commit is contained in:
Jarek Radosz
2020-10-12 18:25:06 +02:00
committed by GitHub
parent a47c8f0585
commit 6932a373a3
5 changed files with 76 additions and 44 deletions

View File

@ -4,9 +4,7 @@ require 'rails_helper'
require 'version'
describe Discourse::VERSION do
context "has_needed_version?" do
it "works for major comparisons" do
expect(Discourse.has_needed_version?('1.0.0', '1.0.0')).to eq(true)
expect(Discourse.has_needed_version?('2.0.0', '1.0.0')).to eq(true)
@ -44,10 +42,9 @@ describe Discourse::VERSION do
expect(Discourse.has_needed_version?('1.3.0.beta4', '1.3.0.beta3')).to eq(true)
expect(Discourse.has_needed_version?('1.3.0', '1.3.0.beta3')).to eq(true)
end
end
context "compatible_resource" do
context "find_compatible_resource" do
shared_examples "test compatible resource" do
it "returns nil when the current version is above all pinned versions" do
expect(Discourse.find_compatible_resource(version_list, "2.6.0")).to be_nil
@ -70,6 +67,10 @@ describe Discourse::VERSION do
expect(Discourse.find_compatible_resource(nil)).to be_nil
end
it "raises an error on invalid input" do
expect { Discourse.find_compatible_resource("1.0.0.beta1 12f82d5") }.to raise_error(Discourse::InvalidVersionListError)
end
context "with a regular compatible list" do
let(:version_list) { <<~VERSION_LIST
2.5.0.beta6: twofivebetasix
@ -94,4 +95,33 @@ describe Discourse::VERSION do
include_examples "test compatible resource"
end
end
context "find_compatible_git_resource" do
let!(:git_directory) do
path = nil
capture_stdout do
# Note the lack of colon between version and hash
path = setup_git_repo(".discourse-compatibility" => "1.0.0.beta1 12f82d5")
# Simulate a remote upstream
`cd #{path} && git remote add origin #{path}/.git && git fetch -q`
`cd #{path} && git branch -u origin/main`
end
path
end
after do
FileUtils.remove_entry(git_directory)
end
it "gracefully handles invalid input" do
output = capture_stderr do
expect(Discourse.find_compatible_git_resource(git_directory)).to be_nil
end
expect(output).to include("Invalid version list")
end
end
end