Skip to content

Commit

Permalink
(PDK-846) add Resource API type unit test template
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Mar 16, 2018
1 parent b798efa commit e0847e1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
20 changes: 11 additions & 9 deletions lib/pdk/generate/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,27 @@ def check_preconditions
end
end

# @return [String] the path where the new type will be written.
# @return [String] the path where the new provider will be written.
def target_object_path
@target_object_path ||= File.join(module_dir, 'lib', 'puppet', 'type', object_name) + '.rb'
@target_object_path ||= File.join(module_dir, 'lib', 'puppet', 'provider', object_name, object_name) + '.rb'
end

# @return [String] the path where the new provider will be written.
def target_addon_path
@target_addon_path ||= File.join(module_dir, 'lib', 'puppet', 'provider', object_name, object_name) + '.rb'
# @return [String] the path where the new type will be written.
def target_type_path
@target_type_path ||= File.join(module_dir, 'lib', 'puppet', 'type', object_name) + '.rb'
end

# Calculates the path to the file where the tests for the new defined
# type will be written.
#
# @return [String] the path where the tests for the new defined type
# @return [String] the path where the tests for the new provider
# will be written.
def target_spec_path
@target_spec_path ||= File.join(module_dir, 'spec', 'unit', 'puppet', 'provider', object_name, object_name) + '_spec.rb'
end

# @return [String] the path where the tests for the new type will be written.
def target_type_spec_path
@target_type_spec_path ||= File.join(module_dir, 'spec', 'unit', 'puppet', 'type', object_name) + '_spec.rb'
end

# transform a object name into a ruby class name
def self.class_name_from_object_name(object_name)
object_name.to_s.split('_').map(&:capitalize).join
Expand Down
16 changes: 12 additions & 4 deletions lib/pdk/generate/puppet_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ def target_object_path
raise NotImplementedError
end

# @abstract Subclass and implement {#target_addon_path}. Implementations
# @abstract Subclass and implement {#target_type_path}. Implementations
# of this method should return a String containing the destination path
# of the additional object file being generated.
# @return [String] returns nil if there is no additional object file
def target_addon_path
def target_type_path
nil
end

Expand All @@ -73,6 +73,13 @@ def target_spec_path
raise NotImplementedError
end

# @abstract Subclass and implement {#target_type_spec_path}. Implementations
# of this method should return a String containing the destination path
# of the tests for the object being generated.
def target_type_spec_path
nil
end

# Retrieves the type of the object being generated, e.g. :class,
# :defined_type, etc. This is specified in the subclass' OBJECT_TYPE
# constant.
Expand All @@ -91,7 +98,7 @@ def object_type
#
# @api public
def check_preconditions
[target_object_path, target_addon_path, target_spec_path].compact.each do |target_file|
[target_object_path, target_type_path, target_spec_path, target_type_spec_path].compact.each do |target_file|
next unless File.exist?(target_file)

raise PDK::CLI::ExitWithError, _("Unable to generate %{object_type}; '%{file}' already exists.") % {
Expand All @@ -116,8 +123,9 @@ def run
data = template_data.merge(configs: config_hash)

render_file(target_object_path, template_path[:object], data)
render_file(target_addon_path, template_path[:addon], data) if template_path[:addon]
render_file(target_type_path, template_path[:type], data) if template_path[:type]
render_file(target_spec_path, template_path[:spec], data) if template_path[:spec]
render_file(target_type_spec_path, template_path[:type_spec], data) if template_path[:type_spec]
end
end

Expand Down
6 changes: 4 additions & 2 deletions lib/pdk/module/templatedir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ def render
# @api public
def object_template_for(object_type)
object_path = File.join(@object_dir, "#{object_type}.erb")
addon_path = File.join(@object_dir, "#{object_type}_addon.erb")
type_path = File.join(@object_dir, "#{object_type}_type.erb")
spec_path = File.join(@object_dir, "#{object_type}_spec.erb")
type_spec_path = File.join(@object_dir, "#{object_type}_type_spec.erb")

if File.file?(object_path) && File.readable?(object_path)
result = { object: object_path }
result[:addon] = addon_path if File.file?(addon_path) && File.readable?(addon_path)
result[:type] = type_path if File.file?(type_path) && File.readable?(type_path)
result[:spec] = spec_path if File.file?(spec_path) && File.readable?(spec_path)
result[:type_spec] = type_spec_path if File.file?(type_spec_path) && File.readable?(type_spec_path)
result
else
nil
Expand Down
9 changes: 9 additions & 0 deletions spec/acceptance/new_provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@
its(:content) { is_expected.to match(%r{RSpec.describe Puppet::Provider::TestProvider::TestProvider do}) }
end

describe file('spec/unit/puppet/type') do
it { is_expected.to be_directory }
end

describe file('spec/unit/puppet/type/test_provider_spec.rb') do
it { is_expected.to be_file }
its(:content) { is_expected.to match(%r{RSpec.describe 'the test_provider type' do}) }
end

context 'when validating the generated code' do
describe command('pdk validate ruby') do
its(:stdout) { is_expected.to be_empty }
Expand Down

0 comments on commit e0847e1

Please sign in to comment.