-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out FakePuppetfile so it can be tested in rspec
- Loading branch information
Showing
3 changed files
with
82 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'spec_helper' | ||
|
||
require_relative '../../util/fake_puppet_file' | ||
|
||
describe FakePuppetfile do | ||
let(:fake) { FakePuppetfile.new } | ||
|
||
specify 'without a version' do | ||
expect(PuppetForge::Module).not_to receive(:find) | ||
|
||
fake.instance_eval { mod 'theforeman/motd' } | ||
|
||
expect { |b| fake.content(&b) }.to yield_with_args("mod 'theforeman/motd'") | ||
end | ||
|
||
specify 'with a minimum' do | ||
expect(PuppetForge::Module).not_to receive(:find) | ||
|
||
fake.instance_eval { mod 'theforeman/motd', '>= 1.2' } | ||
|
||
expect { |b| fake.content(&b) }.to yield_with_args("mod 'theforeman/motd', '>= 1.2'") | ||
end | ||
|
||
specify 'with a git url' do | ||
expect(PuppetForge::Module).to receive(:find) | ||
.with('theforeman-motd') | ||
.and_return(double(PuppetForge::V3::Module, current_release: double(PuppetForge::V3::Release, version: '1.2.3'))) | ||
|
||
fake.instance_eval { mod 'theforeman/motd', :git => 'https://github.com/theforeman/puppet-motd' } | ||
|
||
expect { |b| fake.content(&b) }.to yield_with_args("mod 'theforeman/motd', '~> 1.2.3'") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'puppet_forge' | ||
require 'semverse' | ||
|
||
class FakePuppetfile | ||
def initialize | ||
@new_content = [] | ||
end | ||
|
||
def forge(url) | ||
@new_content << ['forge', url, nil] | ||
PuppetForge.host = url | ||
end | ||
|
||
def mod(name, options = nil) | ||
if options.is_a?(Hash) && !options.include?(:ref) | ||
release = PuppetForge::Module.find(name.tr('/', '-')).current_release | ||
@new_content << ['mod', name, "~> #{release.version}"] | ||
else | ||
@new_content << ['mod', name, options] | ||
end | ||
end | ||
|
||
def content | ||
max_length = @new_content.select { |type, _value| type == 'mod' }.map { |_type, value| value.length }.max | ||
|
||
@new_content.each do |type, value, options| | ||
if type == 'forge' | ||
yield "forge '#{value}'" | ||
yield "" | ||
elsif type == 'mod' | ||
if options.nil? | ||
yield "mod '#{value}'" | ||
elsif options.is_a?(String) | ||
padding = ' ' * (max_length - value.length) | ||
yield "mod '#{value}', #{padding}'#{options}'" | ||
else | ||
padding = ' ' * (max_length - value.length) | ||
yield "mod '#{value}', #{padding}#{options.map { |k, v| ":#{k} => '#{v}'" }.join(', ')}" | ||
end | ||
end | ||
end | ||
end | ||
end |