A platform for community discussion. Free, open, simple.
FIX: GitUtils preventing boot (#37599)
David Taylor04d28fd34a2c
diff --git a/lib/git_utils.rb b/lib/git_utils.rbindex d357eb23e38..87dd3cbd0b9 100644--- a/lib/git_utils.rb+++ b/lib/git_utils.rb@@ -40,12 +40,17 @@ class GitUtils# The `config/git-utils-override.json` file can be used by hosting providers# to override the git information that Discourse reports in the UI.- def self.filesystem_overrides+ private_class_method def self.filesystem_overrides@filesystem_overrides ||=begin- JSON.parse(File.read("#{Rails.root}/config/git-utils-overrides.json"))+ JSON.parse(File.read("#{rails_root}/config/git-utils-overrides.json"))rescue Errno::ENOENT{}endend++ private_class_method def self.rails_root+ # Can't use `Rails.root` here because GitUtils is `require`'d before Rails is initialized+ Pathname.new(File.expand_path("..", __dir__))+ endenddiff --git a/spec/lib/git_utils_spec.rb b/spec/lib/git_utils_spec.rbindex 0fe05a895ba..390abd06c64 100644--- a/spec/lib/git_utils_spec.rb+++ b/spec/lib/git_utils_spec.rb@@ -17,6 +17,7 @@ RSpec.describe GitUtils dorun("git config user.email '[email protected]'")run("git config user.name 'Test User'")run("git config commit.gpgsign false")+ run("git checkout --quiet -b main")yieldendend@@ -40,15 +41,11 @@ RSpec.describe GitUtils doendend- it "returns fallback values when not in a git repo or missing data" do+ it "returns fallback values when missing data" dowithin_temp_git_repo docreate_commit- expect(GitUtils.full_version).to eq("unknown")- end-- Dir.chdir(temp_dir) do- FileUtils.rm_rf(".git")- expect(GitUtils.git_version).to eq("unknown")+ run("git checkout --quiet --detach HEAD")+ expect(GitUtils.git_branch).to eq("unknown")endend@@ -87,15 +84,7 @@ RSpec.describe GitUtils do"full_version" => "override-full",}.to_json,)- Rails.stubs(:root).returns(Pathname.new(temp_dir))-- expect(GitUtils.filesystem_overrides).to eq(- {- "git_version" => "override-sha",- "git_branch" => "override-branch",- "full_version" => "override-full",- },- )+ GitUtils.stubs(:rails_root).returns(Pathname.new(temp_dir))within_temp_git_repo docreate_commit@@ -105,9 +94,24 @@ RSpec.describe GitUtils doendend- it "returns empty hash when file does not exist" do- Rails.stubs(:root).returns(Pathname.new(temp_dir))- expect(GitUtils.filesystem_overrides).to eq({})+ it "returns regular info when file does not exist" do+ GitUtils.stubs(:rails_root).returns(Pathname.new(temp_dir))+ within_temp_git_repo do+ create_commit+ expect(GitUtils.git_version).to eq(`git rev-parse HEAD`.strip)+ expect(GitUtils.git_branch).to eq("main")+ expect(GitUtils.full_version).to eq("unknown")+ endendend++ it "calculates rails root correctly" do+ expected = Rails.root+ Rails.stubs(:root).raises "Rails.root might not be available during GitUtils initialization"+ expect(GitUtils.send(:rails_root)).to eq(expected)+ ensure+ # This will get unstubbed automatically by rails_helper, but we gotta do it+ # even earlier, since some other test cleanup code calls Rails.root+ Rails.unstub(:root)+ endend