require 'retrospec-puppet' require 'rspec' require 'puppet' def fixture_modules_path @fixture_module_path ||= File.expand_path(File.join(fixtures_path, 'modules')) end def fake_fixture_modules @fake_fixture_modules ||= File.expand_path(File.join(fixtures_path, 'fixture_modules')) end def gem_template_path @gem_template_path ||= File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), 'lib', 'retrospec', 'plugins', 'v1', 'plugin', 'templates')) end def sample_module_path File.join(fixtures_path, 'fixture_modules', 'sample_module') end def fixtures_path @fixtures_path ||= File.expand_path(File.join(File.dirname(__FILE__), 'fixtures')) end def fixtures_facts_path @fixtures_facts_path ||= File.expand_path(File.join(fixtures_path, 'facts')) end def fixtures_type_path @fixtures_type_path ||= File.expand_path(File.join(fixtures_path, 'types')) end def fixtures_provider_path @fixtures_provider_path ||= File.expand_path(File.join(fixtures_path, 'providers')) end def fixtures_functions_path @fixtures_functions_path ||= File.expand_path(File.join(fixtures_path, 'functions')) end def clean_up_module_dir(dir) #puts "removing directory #{dir}" pre_commit = File.join(dir, '.git', 'hooks', 'pre-commit') FileUtils.rm_rf(File.join(dir, 'spec')) FileUtils.rm_f(pre_commit) if File.exists?(pre_commit) FileUtils.rm_f(File.join(dir, 'Gemfile')) FileUtils.rm_f(File.join(dir, '.fixtures.yml')) FileUtils.rm_f(File.join(dir, 'Rakefile')) FileUtils.rm_f(File.join(dir, '.git', 'hooks','pre-commit')) end def initialize_templates Retrospec::Plugins::V1::Puppet.new(File.join(fixture_modules_path, 'tomcat')).run_pre_hook end def retrospec_templates_path # I like to develop the templates at the same time as this gem # but I keep the templates in another repo # as a side effect, puppet retrospec will pick up this environment variable as well if File.exists?(File.join(ENV['HOME'], 'github', 'retrospec-templates')) File.join(ENV['HOME'], 'github', 'retrospec-templates') elsif ENV['RETROSPEC_TEMPLATES_DIR'] ENV['RETROSPEC_TEMPLATES_DIR'] else dir = File.expand_path(File.join(ENV['HOME'], '.retrospec', 'repos', 'retrospec-puppet-templates')) FileUtils.mkdir_p(dir) unless File.exists?(dir) dir end end def install_module(module_name) FileUtils.mkdir_p(fixture_modules_path) puts `bundle exec puppet module install -i #{fixture_modules_path} #{module_name}` Dir.glob(File.join(fixture_modules_path, '**', 'spec')).each do |dir| clean_up_module_dir(File.dirname(dir)) end end def ast(opts = {:file => nil, :content => nil}) if opts[:file] result = Utilities::PuppetModule.parser.parse_file(opts[:file]) else result = Utilities::PuppetModule.parser.parse_string(opts[:content]) end result.current end # return a manifest body object def manifest_body(ast) ast.body.body || ast.body end def find_all_resources(manifest_body) res = manifest_body.eAllContents.find_all do |p| p.class.to_s == 'Puppet::Pops::Model::ResourceExpression' end end def dumper @dumper ||= Retrospec::Puppet::RspecDumper.new end