From fe6b50e0383af01023f010b26cd1cf2fa2f3c9c4 Mon Sep 17 00:00:00 2001 From: Dan Bode Date: Mon, 21 Jun 2010 09:30:30 -0700 Subject: just getting started with unit tests for sudo. --- spec/lib/helpers.rb | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 spec/lib/helpers.rb (limited to 'spec/lib/helpers.rb') diff --git a/spec/lib/helpers.rb b/spec/lib/helpers.rb new file mode 100644 index 0000000..55a18ae --- /dev/null +++ b/spec/lib/helpers.rb @@ -0,0 +1,64 @@ +module Helpers + + TEST_DIR = Pathname.new(__FILE__).parent + '..' + + TYPES = { + :ec2 => :ec2 + } + + def self.included(obj) + obj.instance_eval { attr_reader :valid_params } + end + + # Creates a new resource of +type+ + def with(opts = {}, &block) + resource = @type.new(opts) + block ? (yield resource) : resource + end + + # what is the difference? + # Returns a lambda creating a resource (ready for use with +should+) + def specifying(opts = {}, &block) + specification = lambda { with(opts) } + block ? (yield specification) : specification + end + + # Sets up an expection that a resource for +type+ is not created + def should_not_create(type) + raise "Invalid type #{type}" unless TYPES[type] + Puppet::Type.type(TYPES[type]).expects(:new).never + end + + # Sets up an expection that a resource for +type+ is created + def should_create(type) + raise "Invalid type #{type}" unless TYPES[type] + Puppet::Type.type(TYPES[type]).expects(:new).with { |args| yield(args) } + end + + # Return the +@valid_params+ without one or more keys + # Note: Useful since resource types don't like it when +nil+ is + # passed as a parameter value + def valid_params_without(*keys) + valid_params.reject { |k, v| keys.include?(k) } + end + + # yeah! I added this one! + def valid_params_with(opts = {}) + opts.each { |k, v| valid_params[k] = v} + valid_params + end + + # Stub the default provider to get around confines for testing + def stub_default_provider! + unless defined?(@type) + raise ArgumentError, "@type must be set" + end + provider = @type.provider(:ec2) + @type.stubs(:defaultprovider => provider) + end + + def fixture(name, ext = '.txt') + (TEST_DIR + 'fixtures' + "#{name}#{ext}").read + end + +end -- cgit v1.2.3