Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to spec test an arbitrary string #619

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/documentation/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,12 @@ facts with values that it derives from the node name (specified with
In some circumstances (e.g. where your nodename/certname is not the same as
your FQDN), this behaviour is undesirable and can be disabled by changing this
setting to `false`.

### string
**Type:** String<br />
**Default:** `nil`<br />
**Puppet Version(s):** 2.x, 3.x, 4.x, 5.x

Set this to an arbitrary string of Puppet code and define an example of type
`:string`. Rather than reading files off disk, `rspec-puppet` will run the tests
against this string of code.
1 change: 1 addition & 0 deletions lib/rspec-puppet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def self.rspec_puppet_example?
c.add_setting :derive_node_facts_from_nodename, :default => true
c.add_setting :adapter
c.add_setting :platform, :default => Puppet::Util::Platform.actual_platform
c.add_setting :string, :default => nil

c.before(:all) do
if RSpec.configuration.setup_fixtures?
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec-puppet/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'rspec-puppet/example/type_alias_example_group'
require 'rspec-puppet/example/provider_example_group'
require 'rspec-puppet/example/application_example_group'
require 'rspec-puppet/example/string_example_group'

RSpec::configure do |c|

Expand All @@ -27,6 +28,7 @@ def c.rspec_puppet_include(group, type, file_path)
c.rspec_puppet_include RSpec::Puppet::TypeAliasExampleGroup, :type_alias, %w[spec type_aliases]
c.rspec_puppet_include RSpec::Puppet::ProviderExampleGroup, :provider, %w[spec providers]
c.rspec_puppet_include RSpec::Puppet::ApplicationExampleGroup, :application, %w[spec applications]
c.rspec_puppet_include RSpec::Puppet::StringExampleGroup, :string, %w[spec strings]

# Hook for each example group type to remove any caches or instance variables, since they will remain
# and cause a memory leak. Can't be assigned per type by :file_path, so check for its presence.
Expand Down
18 changes: 18 additions & 0 deletions lib/rspec-puppet/example/string_example_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module RSpec::Puppet
module StringExampleGroup
include RSpec::Puppet::ManifestMatchers
include RSpec::Puppet::Support

def catalogue
@catalogue ||= load_catalogue(:string)
end

def exported_resources
lambda { load_catalogue(:string, true) }
end

def rspec_puppet_cleanup
@catalogue = nil
end
end
end
2 changes: 2 additions & 0 deletions lib/rspec-puppet/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def test_manifest(type, opts = {})
nil
elsif type == :type_alias
"$test = #{str_from_value(opts[:test_value])}\nassert_type(#{self.class.top_level_description}, $test)"
elsif type == :string
RSpec.configuration.string
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,13 @@ def post_condition
expect(subject.build_code(:class, {})).to eq "\ninclude class_name\npost_condition"
end
end

context "with code passed in as a string" do
it "builds a test manifest" do
allow(RSpec.configuration).to receive(:string).and_return("example code")
expect(subject.build_code(:string, {})).to eq "\nexample code"
end
end

end
end