Skip to content

Commit

Permalink
(PDK-1047) Add --generate-missing-tests to pdk convert
Browse files Browse the repository at this point in the history
  • Loading branch information
rodjek committed Sep 10, 2019
1 parent 9158517 commit eb5622a
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/pdk/cli/convert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module PDK::CLI
PDK::CLI.full_interview_option(self)
flag nil, :noop, _('Do not convert the module, just output what would be done.')
flag nil, :force, _('Convert the module automatically, with no prompts.')
flag nil, :'add-tests', _('Add any missing tests while converting the module.')

run do |opts, _args, _cmd|
require 'pdk/module/convert'
Expand Down
7 changes: 7 additions & 0 deletions lib/pdk/generate/puppet_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ def check_preconditions
end
end

def can_run?
check_preconditions
true
rescue PDK::CLI::ExitWithError
false
end

# Check that the templates can be rendered. Find an appropriate template
# and create the target files from the template. This is the main entry
# point for the class.
Expand Down
35 changes: 34 additions & 1 deletion lib/pdk/module/convert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def run
stage_changes!

unless update_manager.changes?
PDK::Report.default_target.puts(_('No changes required.'))
if generating_tests?
generate_missing_tests!
else
PDK::Report.default_target.puts(_('No changes required.'))
end

return
end

Expand Down Expand Up @@ -54,6 +59,8 @@ def run

PDK::Util::Bundler.ensure_bundle! if needs_bundle_update?

generate_missing_tests! if generating_tests?

print_result 'Convert completed'
end

Expand All @@ -65,6 +72,32 @@ def force?
options[:force]
end

def generate_missing_tests?
options[:'add-tests']
end

def generating_tests?
generate_missing_tests? && missing_tests?
end

def missing_tests?
test_generators.flatten.any? { |gen| gen.can_run? }
end

def test_generators
@test_generators ||= PDK::Util::PuppetStrings.all_objects.map do |generator, objects|
(objects || []).map do |obj|
generator.new(Dir.pwd, obj['name'], spec_only: true)
end
end
end

def generate_missing_tests!
test_generators.flatten.each do |gen|
gen.run if gen.can_run?
end
end

def needs_bundle_update?
update_manager.changed?('Gemfile')
end
Expand Down
18 changes: 18 additions & 0 deletions lib/pdk/util/puppet_strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ def self.find_object(name)
[generator, known_objects[object_type].find { |obj| object_names.include?(obj['name']) }]
end

# Generate a list of all objects that PDK has a generator for.
#
# @returns [Array[Array[PDK::Generate::PuppetObject,
# Array[Hash{String=>Object}]]]] an associative array where the first
# element of each pair is the generator class and the second element of
# each pair is an array of description hashes from puppet-strings.
def self.all_objects
generators = PDK::Generate::GENERATORS.select do |gen|
gen.respond_to?(:puppet_strings_type) && !gen.puppet_strings_type.nil?
end

known_objects = generate_hash

generators.map { |gen| [gen, known_objects[gen.puppet_strings_type]] }.reject do |_, obj|
obj.nil? || obj.empty?
end
end

# Evaluates the mapping of puppet-strings object type to PDK generator
# class.
#
Expand Down
52 changes: 52 additions & 0 deletions spec/acceptance/convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,56 @@
end
end
end

context 'when adding missing tests' do
# A real bare bones module here. Testing to ensure that the functionality
# works even when the module didn't have puppet-strings installed before
# the convert.
before(:all) do
FileUtils.mkdir_p(File.join('missing_tests', 'manifests', 'baz'))
FileUtils.mkdir_p(File.join('missing_tests', 'spec', 'classes'))

File.open(File.join('missing_tests', 'manifests', 'foo.pp'), 'wb') do |f|
f.puts 'class missing_tests::foo { }'
end
File.open(File.join('missing_tests', 'spec', 'classes', 'foo_spec.rb'), 'wb') do |f|
f.puts "require 'spec_helper'"
f.puts "describe 'missing_tests::foo' do"
f.puts 'end'
end

File.open(File.join('missing_tests', 'manifests', 'bar.pp'), 'wb') do |f|
f.puts 'class missing_tests::bar { }'
end
File.open(File.join('missing_tests', 'manifests', 'baz', 'gronk.pp'), 'wb') do |f|
f.puts 'define missing_tests::baz::gronk() { }'
end

Dir.chdir('missing_tests')
end

after(:all) do
Dir.chdir('..')
FileUtils.rm_rf('missing_tests')
end

class_path = File.join('spec', 'classes', 'bar_spec.rb')
define_path = File.join('spec', 'defines', 'baz', 'gronk_spec.rb')

describe command("#{pdk_convert_base} --force --skip-interview --add-tests") do
its(:exit_status) { is_expected.to eq(0) }
its(:stderr) { is_expected.to match(%r{#{Regexp.escape(class_path)}}m) }
its(:stderr) { is_expected.to match(%r{#{Regexp.escape(define_path)}}m) }

describe file(class_path) do
it { is_expected.to be_file }
its(:content) { is_expected.to match(%r{describe 'missing_tests::bar'}m) }
end

describe file(define_path) do
it { is_expected.to be_file }
its(:content) { is_expected.to match(%r{describe 'missing_tests::baz::gronk'}m) }
end
end
end
end
52 changes: 52 additions & 0 deletions spec/unit/pdk/util/puppet_strings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,58 @@
end
end

describe '.all_objects' do
subject(:result) { described_class.all_objects }

before(:each) do
allow(described_class).to receive(:generate_hash).and_return(puppet_strings_data)
end

context 'when there are no results from puppet-strings' do
let(:puppet_strings_data) { {} }

it 'returns an empty array' do
expect(result).to eq([])
end
end

context 'when the puppet-strings result includes objects that PDK has no generator for' do
let(:puppet_strings_data) do
{
'data_types' => [
{ 'name' => 'mymodule::my_object' },
],
}
end

it 'filters the generatorless objects out of the result' do
expect(result).to eq([])
end
end

context 'when the puppet-strings result includes objects with generators' do
let(:puppet_strings_data) do
{
'puppet_classes' => [
{ 'name' => 'mymodule::my_class' },
],
'defined_types' => [
{ 'name' => 'mymodule::my_define' },
],
}
end

it 'returns an associative array' do
expect(result).to eq(
[
[PDK::Generate::DefinedType, [{ 'name' => 'mymodule::my_define' }]],
[PDK::Generate::PuppetClass, [{ 'name' => 'mymodule::my_class' }]],
],
)
end
end
end

describe '.find_generator' do
subject { described_class.find_generator(type) }

Expand Down

0 comments on commit eb5622a

Please sign in to comment.