Skip to content

Commit

Permalink
Expose version and git revision
Browse files Browse the repository at this point in the history
  • Loading branch information
jefmathiot committed Jan 16, 2015
1 parent 1f7995f commit f02a3c9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions electric_sheep.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions lib/electric_sheep.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'electric_sheep/version'
require 'git_rev'
require 'active_support'
require 'active_support/core_ext'

Expand All @@ -23,15 +24,43 @@
require 'electric_sheep/notifiers'

module ElectricSheep

class << self

def template_path
@_template_path ||= File.join(gem_path, 'templates')
end

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
42 changes: 42 additions & 0 deletions spec/electric_sheep_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f02a3c9

Please sign in to comment.