Skip to content

Commit

Permalink
Add default package version and guard against nil/empty versions (ber…
Browse files Browse the repository at this point in the history
…nd#176)

* Add default package version ('1.0') and ensure that this is the value provided to FPM if the recipe version is nil or the empty string
* Removed unnecessary `attr_reader` for version
  • Loading branch information
tomeon authored and bernd committed Oct 19, 2016
1 parent 739ec6c commit fded99b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/fpm/cookery/package/version.rb
Expand Up @@ -16,7 +16,11 @@ class Version
:default => '-'
}

attr_reader :version, :epoch, :revision
# fpm sets the default version in FPM::Command; since we bypass the
# command line interface, we need to set our own value.
DEFAULT_VERSION = '1.0'

attr_reader :epoch, :revision

def initialize(recipe, target, config)
@recipe = recipe
Expand All @@ -30,6 +34,10 @@ def vendor
@config[:vendor] || @recipe.vendor
end

def version
@version ||= DEFAULT_VERSION
end

def revision_delimiter
REVISION_DELIMITER[:default]
end
Expand All @@ -50,9 +58,7 @@ def to_s
private

def split_version(version)
epoch, version = version.split(':', 2)

version.nil? ? [epoch, nil] : [version, epoch]
(version || '').split(':', 2).reject(&:empty?).reverse
end
end
end
Expand Down
50 changes: 50 additions & 0 deletions spec/package_version_spec.rb
Expand Up @@ -140,4 +140,54 @@
expect(version.to_str).to eq('1.3-1.foo')
end
end

describe '#version' do
context 'given a colon-delimited string in recipe.version' do
context 'where version and epoch are defined' do
before(:each) { recipe.version = '8675309:3.1.1'}

it 'returns the version and epoch' do
expect(version.version).to eq('3.1.1')
expect(version.epoch).to eq('8675309')
end
end

context 'where only version is defined' do
before(:each) { recipe.version = ':2.1' }

it 'returns the version and sets epoch to nil' do
expect(version.version).to eq('2.1')
expect(version.epoch).to be nil
end
end

context 'where only epoch is defined' do
before(:each) { recipe.version = '12345:' }

it 'returns the epoch as version and sets epoch to nil' do
expect(version.version).to eq('12345')
expect(version.epoch).to be nil
end
end

end

context 'given a nil recipe.version' do
before(:each) { recipe.version = nil }

it 'returns the default version' do
expect(version.version).to eq(klass::DEFAULT_VERSION)
expect(version.epoch).to be nil
end
end

context 'given a recipe.version containing the empty string' do
before(:each) { recipe.version = '' }

it 'returns the default version' do
expect(version.version).to eq(klass::DEFAULT_VERSION)
expect(version.epoch).to be nil
end
end
end
end

0 comments on commit fded99b

Please sign in to comment.