From 2fe01f13df2692c6fe83855616c79c6ff427b290 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 5 Feb 2023 20:31:14 +0100 Subject: [PATCH] Commit hash version but not in git Fixes #11 --- lib/mrsk/commander.rb | 10 +++++++++- test/commander_test.rb | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/mrsk/commander.rb b/lib/mrsk/commander.rb index 661f8a15..2807162a 100644 --- a/lib/mrsk/commander.rb +++ b/lib/mrsk/commander.rb @@ -91,7 +91,15 @@ class Mrsk::Commander private def cascading_version - version.presence || ENV["VERSION"] || `git rev-parse HEAD`.strip + version.presence || ENV["VERSION"] || current_commit_hash + end + + def current_commit_hash + if system("git rev-parse") + `git rev-parse HEAD`.strip + else + raise "Can't use commit hash as version, no git repository found in #{Dir.pwd}" + end end # Lazy setup of SSHKit diff --git a/test/commander_test.rb b/test/commander_test.rb index 89ac4627..31bee04a 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -9,6 +9,16 @@ class CommanderTest < ActiveSupport::TestCase assert_equal Mrsk::Configuration, @mrsk.config.class end + test "commit hash as version" do + assert_equal `git rev-parse HEAD`.strip, @mrsk.config.version + end + + test "commit hash as version but not in git" do + @mrsk.expects(:system).with("git rev-parse").returns(nil) + error = assert_raises(RuntimeError) { @mrsk.config } + assert_match /no git repository found/, error.message + end + test "overwriting hosts" do assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @mrsk.hosts