Merge pull request #2 from hkenney/PUP-3086_add-basic-spec-tests

(PDOC-5) Add basic unit testing
This commit is contained in:
Andrew Parker 2014-09-04 10:20:00 -07:00
commit 39f4ca1d09
13 changed files with 226 additions and 16 deletions

View File

@ -1,11 +1,16 @@
source 'https://rubygems.org'
gem 'yard'
gem 'puppet', '~> 3.6.2'
gem 'rgen'
group :test do
gem 'rspec'
gem 'mocha'
gem 'puppet'
gem 'puppetlabs_spec_helper'
end
group :development do
gem 'pry'
gem 'pry-debugger'
end

View File

@ -12,8 +12,8 @@ Puppet::Face.define(:yardoc, '0.0.1') do
raise RuntimeError, "The 'rgen' gem must be installed in order to use this face."
end
if RUBY_VERSION < '1.9' && !Puppet.features.require_relative?
raise RuntimeError, "The 'backports' gem must be installed in order to use this face under Ruby 1.8.7."
if RUBY_VERSION.match(/^1\.8/)
raise RuntimeError, "This face requires Ruby 1.9 or greater."
end
end

View File

@ -1,7 +0,0 @@
require 'spec_helper'
describe 'puppet_yardoc' do
context 'with defaults for all parameters' do
it { should contain_class('puppet_yardoc') }
end
end

View File

@ -4,14 +4,19 @@ $LOAD_PATH.unshift File.join(dir, 'lib')
require 'mocha'
require 'puppet'
require 'rspec'
require 'spec/autorun'
Spec::Runner.configure do |config|
# This is neeeded so we can access a Registry if YARD creates one
require 'puppetx/yardoc/yard/plugin'
include YARD
RSpec.configure do |config|
config.mock_with :mocha
end
# We need this because the RAL uses 'should' as a method. This
# allows us the same behaviour but with a different method name.
class Object
alias :must :should
# Borrowed from YARD spec helper
def parse_file(file, thisfile = __FILE__, log_level = log.level, ext = '.pp')
Registry.clear
path = File.join(File.dirname(thisfile), 'examples', file.to_s + ext)
YARD::Parser::SourceParser.parse(path, [], log_level)
end

View File

@ -0,0 +1,10 @@
name 'username-test'
version '0.1.0'
source ''
author 'username'
license 'Apache 2.0'
summary ''
description ''
project_page ''
dependency 'puppetlabs/stdlib'

View File

@ -0,0 +1,12 @@
class test (
$package_name = $test::params::package_name,
$service_name = $test::params::service_name,
) inherits test::params {
# validate parameters here
class { 'test::install': } ->
class { 'test::config': } ~>
class { 'test::service': } ->
Class['test']
}

View File

@ -0,0 +1,63 @@
require 'spec_helper'
require 'puppet/face/yardoc'
describe Puppet::Face do
describe "YARDoc action" do
it "should raise an error if yard is absent" do
Puppet.features.stubs(:yard?).returns(false)
expect{Puppet::Face[:yardoc, :current].yardoc}.to raise_error(RuntimeError, "The 'yard' gem must be installed in order to use this face.")
end
it "should raise an error if rgen is absent" do
Puppet.features.stubs(:rgen?).returns(false)
expect{Puppet::Face[:yardoc, :current].yardoc}.to raise_error(RuntimeError, "The 'rgen' gem must be installed in order to use this face.")
end
it "should raise an error if the Ruby verion is less than 1.9", :if => RUBY_VERSION.match(/^1\.8/) do
expect{Puppet::Face[:yardoc, :current].yardoc}.to raise_error(RuntimeError, "This face requires Ruby 1.9 or greater.")
end
it "should invoke Yardoc with MODULE_SOURCEFILES if no arguments are provided" do
YARD::CLI::Yardoc.expects(:run).with('manifests/**/*.pp', 'lib/**/*.rb')
Puppet::Face[:yardoc, :current].yardoc
end
it "should invoke Yardoc with provided arguments" do
YARD::CLI::Yardoc.expects(:run).with('--debug', 'some_file.rb')
Puppet::Face[:yardoc, :current].yardoc('--debug', 'some_file.rb')
end
end
describe "modules action" do
it "should raise an error if yard is absent" do
Puppet.features.stubs(:yard?).returns(false)
expect{Puppet::Face[:yardoc, :current].modules}.to raise_error(RuntimeError, "The 'yard' gem must be installed in order to use this face.")
end
it "should raise an error if rgen is absent" do
Puppet.features.stubs(:rgen?).returns(false)
expect{Puppet::Face[:yardoc, :current].modules}.to raise_error(RuntimeError, "The 'rgen' gem must be installed in order to use this face.")
end
it "should raise an error if the Ruby version is less than 1.9", :if => RUBY_VERSION.match(/^1\.8/) do
expect{Puppet::Face[:yardoc, :current].modules}.to raise_error(RuntimeError, "This face requires Ruby 1.9 or greater.")
end
end
describe "server action" do
it "should raise an error if yard is absent" do
Puppet.features.stubs(:yard?).returns(false)
expect{Puppet::Face[:yardoc, :current].server}.to raise_error(RuntimeError, "The 'yard' gem must be installed in order to use this face.")
end
it "should raise an error if rgen is absent" do
Puppet.features.stubs(:rgen?).returns(false)
expect{Puppet::Face[:yardoc, :current].server}.to raise_error(RuntimeError, "The 'rgen' gem must be installed in order to use this face.")
end
it "should raise an error if the Ruby version is less than 1.9", :if => RUBY_VERSION.match(/^1\.8/) do
expect{Puppet::Face[:yardoc, :current].server}.to raise_error(RuntimeError, "This face requires Ruby 1.9 or greater.")
end
end
end

View File

@ -0,0 +1,38 @@
require 'spec_helper'
require 'puppetx/yardoc/pops/yard_statement'
describe Puppetx::Yardoc::Pops do
let(:parser) {Puppet::Pops::Parser::Parser.new()}
describe "YARDstatement class" do
let(:manifest) {"#hello world\nclass foo { }"}
let(:model) {parser.parse_string(manifest).current.definitions.first}
let(:test_statement) {Puppetx::Yardoc::Pops::YARDStatement.new(model)}
describe "when creating a new instance of YARDStatement" do
it "should extract comments from the source code" do
expect(test_statement.comments).to match(/^#hello world/)
end
end
end
describe "YARDTransfomer class" do
let(:manifest) {"#hello world\nclass foo($bar) { }"}
let(:manifest_default) {"#hello world\nclass foo($bar = 3) { }"}
let(:transformer) {Puppetx::Yardoc::Pops::YARDTransformer.new}
describe "transform method" do
it "should perform the correct transformation with parameter defaults" do
model = parser.parse_string(manifest_default).current.definitions.first
statements = transformer.transform(model)
expect(statements.parameters[0][0].class).to be(Puppetx::Yardoc::Pops::YARDStatement)
end
it "should perform the correct transofmration without parameter defaults" do
model = parser.parse_string(manifest).current.definitions.first
statements = transformer.transform(model)
expect(statements.parameters[0][1].class).to be(NilClass)
end
end
end
end

View File

@ -0,0 +1,5 @@
class foo::bar {
file { '/test/file/path':
owner => 'baz',
}
}

View File

@ -0,0 +1,7 @@
define wibbly::wobbly ($wimey) {
Notify ($wimey)
}
wibbly::wobbly{
'timey': wimey => stuff
}

View File

@ -0,0 +1,7 @@
require 'puppet'
module Puppet::Parser::Functions
newfunction(:puppet3_function, :type => rvalue) do |args|
puts 'Hello World!'
end
end

View File

@ -0,0 +1,7 @@
require 'puppet'
Puppet::Functions.create_function(:puppet4_function) do
def puppet4_function(x,y)
x >= y ? x : y
end
end

View File

@ -0,0 +1,58 @@
require 'spec_helper'
require 'puppetx/yardoc/yard/handlers'
describe Puppetx::Yardoc::YARD::Handlers do
describe "DefinedTypeHanlder" do
it "should add a defined type object in the Registry" do
parse_file :defined_type, __FILE__, log.level, '.pp'
obj = Registry.at("wibbly::wobbly")
expect(obj.type).to be(:definedtype)
end
end
describe "FutureParserDispatchHandler" do
before(:each) {parse_file :puppet4_function, __FILE__, log.level, '.rb'}
it "should add a puppet namespace object to the Registry" do
namespace = Registry.at("FutureParserFunctions")
expect(namespace.type).to be(:puppetnamespace)
end
it "should add a future parser function object to the Registry" do
function = Registry.at("FutureParserFunctions#puppet4_function")
expect(function.type).to be(:method)
end
it "should add a method object to the Registry" do
method = Registry.at("#puppet4_function")
expect(method.type).to be(:method)
end
end
describe "ParserFunctionHanlder" do
before(:each) {parse_file :puppet3_function, __FILE__, log.level, '.rb'}
it "should add a module object to the Registry" do
puppet_module = Registry.at("Puppet::Parser::Functions")
expect(puppet_module.type).to be(:module)
end
it "should add a puppet namespace object to the Registry" do
namespace = Registry.at("ParserFunctions")
expect(namespace.type).to be(:puppetnamespace)
end
it "should add a method object to the Registry" do
method = Registry.at("ParserFunctions#puppet3_function")
expect(method.type).to be(:method)
end
end
describe "HostClassDefintion" do
before(:each) {parse_file :class, __FILE__, log.level, '.pp'}
it "should add a host class object to the Registry" do
hostclass = Registry.at("foo::bar")
expect(hostclass.type).to be(:hostclass)
end
end
end