Skip to content

Commit

Permalink
Factor out FakePuppetfile so it can be tested in rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
ekohl committed May 1, 2024
1 parent 753dc84 commit 24a81c7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 52 deletions.
58 changes: 6 additions & 52 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,6 @@ rescue LoadError
puts 'Rubocop not loaded'
end

begin
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

pin_task = true
rescue LoadError
pin_task = false
end

BUILD_KATELLO = !ENV.key?('EXCLUDE_KATELLO')

BUILDDIR = File.expand_path(ENV['BUILDDIR'] || '_build')
Expand Down Expand Up @@ -363,13 +313,17 @@ namespace :pkg do
end
end

if pin_task
begin
require_relative 'util/fake_puppet_file'
rescue LoadError
# Some dependency missing
else
desc 'Pin all the modules in Puppetfile to released versions instead of git branches'
task :pin_modules do
filename = 'Puppetfile'

fake = FakePuppetfile.new
fake.instance_eval { instance_eval(File.read(filename), filename, 1) }
fake.instance_eval { eval(File.read(filename), filename, 1) }

File.open(filename, 'w') do |file|
fake.content do |line|
Expand Down
33 changes: 33 additions & 0 deletions spec/util/fake_puppet_file_spec.rb
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
43 changes: 43 additions & 0 deletions util/fake_puppet_file.rb
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

0 comments on commit 24a81c7

Please sign in to comment.