diff --git a/electric_sheep.gemspec b/electric_sheep.gemspec index 63f8c09..98c8044 100644 --- a/electric_sheep.gemspec +++ b/electric_sheep.gemspec @@ -43,5 +43,6 @@ Gem::Specification.new do |spec| spec.add_dependency 'mail', '~> 2.6', '>= 2.6.3' spec.add_dependency 'premailer', '~> 1.8', '>= 1.8.2' spec.add_dependency 'posix-spawn', '~> 0.3', '>= 0.3.9' + spec.add_dependency 'git_rev', '~> 0.1', '>= 0.1.0' end diff --git a/lib/electric_sheep.rb b/lib/electric_sheep.rb index bb8cba3..900e948 100644 --- a/lib/electric_sheep.rb +++ b/lib/electric_sheep.rb @@ -1,4 +1,5 @@ require 'electric_sheep/version' +require 'git_rev' require 'active_support' require 'active_support/core_ext' @@ -23,7 +24,9 @@ require 'electric_sheep/notifiers' module ElectricSheep + class << self + def template_path @_template_path ||= File.join(gem_path, 'templates') end @@ -31,7 +34,33 @@ def template_path def gem_path @_gem_path ||= File.expand_path(File.join(File.dirname(__FILE__), '..')) end + + def version + VERSION + end + + def git_revision + if @sha.nil? + if ENV['ELECTRIC_SHEEP_REVISION'] + @sha = ENV['ELECTRIC_SHEEP_REVISION'][0, 7] + else + begin + @sha = GitRev::Sha.new.short + # Not a git repository + rescue + @sha = "-" * 7 + end + end + end + @sha + end + + def revision + [version, git_revision].join(" ") + end + end + end I18n.enforce_available_locales=false diff --git a/spec/electric_sheep_spec.rb b/spec/electric_sheep_spec.rb index 3e2c6a2..d45d527 100644 --- a/spec/electric_sheep_spec.rb +++ b/spec/electric_sheep_spec.rb @@ -10,4 +10,46 @@ subject.template_path.must_equal "#{subject.gem_path}/templates" end + it 'provides the version' do + subject.version.must_equal ElectricSheep::VERSION + end + + describe 'providing the revision hash' do + + before do + subject.instance_variable_set(:@sha, nil) + end + + after do + ENV['ELECTRIC_SHEEP_REVISION'] = nil + end + + it 'uses the revision in cache' do + subject.instance_variable_set(:@sha, 'xxxxxxx') + subject.git_revision.must_equal 'xxxxxxx' + end + + it 'gets the hash from an environment variable' do + ENV['ELECTRIC_SHEEP_REVISION'] = '0123456' + 'x' * 33 + subject.git_revision.must_equal '0123456' + end + + it 'get the hash from the git repository' do + GitRev::Sha.any_instance.expects(:short).returns 'fedcba9' + subject.git_revision.must_equal 'fedcba9' + end + + it 'returns an unknown revision when unable to get it' do + GitRev::Sha.any_instance.expects(:short).raises 'An exception' + subject.git_revision.must_equal '-------' + end + + end + + it 'merges the version and git revision' do + subject.expects(:version).returns('0.0.0') + subject.expects(:git_revision).returns('0000000') + subject.revision.must_equal '0.0.0 0000000' + end + end