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

Adds the ability to spec test an arbitrary string #619

wants to merge 1 commit into from

Conversation

binford2k
Copy link
Contributor

We are building a web service that will enable rspec-puppet testing of
POSTed Puppet code. The use case is a tutorial that will allow users to
type in a snippet of Puppet code and get immediate validation of whether
they'd completed the exercise correctly.

Example test:

require 'rspec-puppet'

RSpec::configure do |c|
  c.string = "package { 'httpd':
    ensure => present,
  }"
end

describe 'apache', :type => :string do
  let(:node) { 'testhost.example.com' }

  describe 'when called with no parameters on redhat' do
    let (:facts) { { ... } }

    it {
      should contain_package('httpd').with({
        'ensure' => 'present',
      })
    }
  end
end

We are building a web service that will enable rspec-puppet testing of
POSTed Puppet code. The use case is a tutorial that will allow users to
type in a snippet of Puppet code and get immediate validation of whether
they'd completed the exercise correctly.

Example test:

```Ruby
require 'rspec-puppet'

RSpec::configure do |c|
  c.string = "package { 'httpd':
    ensure => present,
  }"
end

describe 'apache', :type => :string do
  let(:node) { 'testhost.example.com' }

  describe 'when called with no parameters on redhat' do
    let (:facts) { { ... } }

    it {
      should contain_package('httpd').with({
        'ensure' => 'present',
      })
    }
  end
end
```
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.04%) to 91.346% when pulling 4409ed2 on binford2k:validate_arbitrary_string_of_puppet_code into 3fb3d9b on rodjek:master.

@rodjek
Copy link
Owner

rodjek commented Nov 8, 2017

Rather than making the input string an RSpec setting, would you mind changing it to be a helper on the example instead? e.g. let(:code), let(:manifest) or similar?

@binford2k
Copy link
Contributor Author

Hmm. I'm not opposed to that idea, but how would we be able to invoke a spec programmatically then? We're using this now like so:

  def run_rspec(spec_path, str)
    require 'rspec/core'

    # rspec needs an IO object to write to. We just want it as a string...
    data = StringIO.new
    RSpec::configure do |c|
        c.output_stream = data
        c.formatter     = 'json'
    end

    # require *after* setting the output stream or it screams at us
    # String input depends on https://github.com/rodjek/rspec-puppet/pull/619
    require 'rspec-puppet'
    RSpec::configure do |c|
        c.string        = str
    end

    begin
        RSpec::Core::Runner.run([spec_path])
        return data.string
    rescue StandardError, LoadError => e
        return "Error running spec test: #{e.message}",
end

@rodjek rodjek added this to the 2.7.x milestone Mar 6, 2018
@scotje scotje removed this from the 2.8.x milestone Oct 15, 2020
@DavidS
Copy link
Collaborator

DavidS commented Nov 9, 2020

@binford2k @scotje is this still relevant? If yes, please link to a internal ticket to capture that.

@DavidS DavidS self-assigned this Nov 9, 2020
@sanfrancrisko
Copy link
Contributor

@binford2k Do you intend to return to this? If not, would you be OK closing the PR?

@binford2k
Copy link
Contributor Author

@sanfrancrisko I would like to get it merged, but I don't know how to accomplish what @rodjek asked for and retain the ability to run a spec test from an arbitrary file against an arbitrary string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants