diff --git a/Gemfile b/Gemfile index ff3e997..392ea17 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/lib/puppet/face/yardoc.rb b/lib/puppet/face/yardoc.rb index f5809b6..d394335 100644 --- a/lib/puppet/face/yardoc.rb +++ b/lib/puppet/face/yardoc.rb @@ -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 diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb deleted file mode 100644 index 5d4503c..0000000 --- a/spec/classes/init_spec.rb +++ /dev/null @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5fda588..311484f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 + diff --git a/spec/unit/puppet/examples/test/Modulefile b/spec/unit/puppet/examples/test/Modulefile new file mode 100644 index 0000000..5f73a2f --- /dev/null +++ b/spec/unit/puppet/examples/test/Modulefile @@ -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' diff --git a/spec/unit/puppet/examples/test/manifests/init.pp b/spec/unit/puppet/examples/test/manifests/init.pp new file mode 100644 index 0000000..487cf30 --- /dev/null +++ b/spec/unit/puppet/examples/test/manifests/init.pp @@ -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'] +} diff --git a/spec/unit/puppet/face_spec.rb b/spec/unit/puppet/face_spec.rb new file mode 100644 index 0000000..b60b6ef --- /dev/null +++ b/spec/unit/puppet/face_spec.rb @@ -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 diff --git a/spec/unit/puppetx/yardoc/pops_spec.rb b/spec/unit/puppetx/yardoc/pops_spec.rb new file mode 100644 index 0000000..7b14ee9 --- /dev/null +++ b/spec/unit/puppetx/yardoc/pops_spec.rb @@ -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 diff --git a/spec/unit/puppetx/yardoc/yard/examples/class.pp b/spec/unit/puppetx/yardoc/yard/examples/class.pp new file mode 100644 index 0000000..512b39f --- /dev/null +++ b/spec/unit/puppetx/yardoc/yard/examples/class.pp @@ -0,0 +1,5 @@ +class foo::bar { + file { '/test/file/path': + owner => 'baz', + } +} diff --git a/spec/unit/puppetx/yardoc/yard/examples/defined_type.pp b/spec/unit/puppetx/yardoc/yard/examples/defined_type.pp new file mode 100644 index 0000000..6021a41 --- /dev/null +++ b/spec/unit/puppetx/yardoc/yard/examples/defined_type.pp @@ -0,0 +1,7 @@ +define wibbly::wobbly ($wimey) { + Notify ($wimey) +} + +wibbly::wobbly{ + 'timey': wimey => stuff + } diff --git a/spec/unit/puppetx/yardoc/yard/examples/puppet3_function.rb b/spec/unit/puppetx/yardoc/yard/examples/puppet3_function.rb new file mode 100644 index 0000000..a9e352f --- /dev/null +++ b/spec/unit/puppetx/yardoc/yard/examples/puppet3_function.rb @@ -0,0 +1,7 @@ +require 'puppet' + +module Puppet::Parser::Functions + newfunction(:puppet3_function, :type => rvalue) do |args| + puts 'Hello World!' + end +end diff --git a/spec/unit/puppetx/yardoc/yard/examples/puppet4_function.rb b/spec/unit/puppetx/yardoc/yard/examples/puppet4_function.rb new file mode 100644 index 0000000..fc65f89 --- /dev/null +++ b/spec/unit/puppetx/yardoc/yard/examples/puppet4_function.rb @@ -0,0 +1,7 @@ +require 'puppet' + +Puppet::Functions.create_function(:puppet4_function) do + def puppet4_function(x,y) + x >= y ? x : y + end +end diff --git a/spec/unit/puppetx/yardoc/yard/handlers_spec.rb b/spec/unit/puppetx/yardoc/yard/handlers_spec.rb new file mode 100644 index 0000000..adbbeb6 --- /dev/null +++ b/spec/unit/puppetx/yardoc/yard/handlers_spec.rb @@ -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