lib/rspec/terraform/helpers/destroy.rb in rspec-terraform-0.1.0.pre.43 vs lib/rspec/terraform/helpers/destroy.rb in rspec-terraform-0.1.0.pre.44

- old
+ new

@@ -32,19 +32,28 @@ def execute(overrides = {}, &block) parameters = with_configuration_provider_parameters(overrides) parameters = with_resolved_vars(parameters, &block) - ensure_required_parameters(parameters) - - clean(parameters) - init(parameters) - destroy(parameters) + execute_if_required(parameters) do + validate(parameters) + clean(parameters) + init(parameters) + destroy(parameters) + end end private + def execute_if_required(parameters, &block) + only_if = parameters[:only_if] + only_if_args = only_if ? [parameters].slice(0, only_if.arity) : [] + should_execute = only_if ? only_if.call(*only_if_args) : true + + block.call if should_execute + end + def with_configuration_provider_parameters(parameters) configuration_provider.resolve(parameters) end def with_resolved_vars(parameters, &block) @@ -60,29 +69,29 @@ in_place: [:configuration_directory], isolated: %i[source_directory configuration_directory] }[execution_mode] || [] end - def ensure_required_parameters(parameters) - missing_parameters = - required_parameters(execution_mode) - .filter { |parameter| parameters[parameter].nil? } - - return if missing_parameters.empty? - - raise_missing_parameters(missing_parameters) - end - def raise_missing_parameters(parameters) parameters = parameters.collect { |parameter| "`:#{parameter}`" } if parameters.count == 1 raise StandardError, "Required parameter: #{parameters[0]} missing." else parameters = "#{parameters[..-2].join(', ')} and #{parameters[-1]}" raise StandardError, "Required parameters: #{parameters} missing." end + end + + def validate(parameters) + missing_parameters = + required_parameters(execution_mode) + .filter { |parameter| parameters[parameter].nil? } + + return if missing_parameters.empty? + + raise_missing_parameters(missing_parameters) end def clean(parameters) return unless execution_mode == :isolated