<commit_before>require 'serverspec'
require 'net/ssh'
require 'vagrant_helper'
require 'yaml'
require 'pathname'

include Serverspec::Helper::Ssh
include Serverspec::Helper::DetectOS

module RSpec
  module Core
    class ExampleGroup
      def get_file
        block = self.class.metadata[:example_group_block]
        if RUBY_VERSION.start_with?('1.8')
          file = block.to_s.match(/.*@(.*):[0-9]+>/)[1]
        else
          file = block.source_location.first
        end
        file
      end
    end
  end
end

RSpec.configure do |c|

  debug = ENV['debug']
  keep_box = ENV['keep_box']
  box = ENV['box'] || 'wheezy'
  c.add_setting :before_files
  c.before_files = []
  root_dir = Dir.getwd
  vagrant_helper = VagrantHelper.new(root_dir, box, true)

  c.before :all do
    file = self.get_file
    unless c.before_files.include? file
      c.before_files.push file
      c.ssh.close if c.ssh

      vagrant_helper.reset unless keep_box
      c.ssh = vagrant_helper.connect
      spec_dir = Dir.new File.dirname file

      if File.exists? spec_dir.to_path + '/facts.json'
        vagrant_facts_path = vagrant_helper.get_path spec_dir.to_path + '/facts.json'
        vagrant_helper.exec("sudo mkdir -p /etc/facter/facts.d && sudo ln -sf #{vagrant_facts_path.shellescape} /etc/facter/facts.d/")
      end

      hiera_config = {
          :backends => ['json'],
          :json => {
              :datadir => '/vagrant'
          },
          :hierarchy => [
              Pathname.new(File.join(spec_dir.to_path, 'hiera')).relative_path_from(Pathname.new(root_dir)).to_s,
              'spec/hiera'
          ]
      }
      hiera_command = "echo #{hiera_config.to_yaml.shellescape} > /etc/hiera.yaml"
      vagrant_helper.exec("sudo bash -c #{hiera_command.shellescape}")

      spec_dir.sort.each do |local_file|
        next unless File.extname(local_file) == '.pp'
        vagrant_manifest_path = vagrant_helper.get_path spec_dir.to_path + '/' + local_file
        command = "sudo puppet apply --verbose --modulepath '/etc/puppet/modules:/vagrant/modules' #{vagrant_manifest_path.shellescape} --hiera_config=/etc/hiera.yaml"
        command += ' --debug' if debug
        begin
          puts
          puts 'Running `' + vagrant_manifest_path + '`'
          output = vagrant_helper.exec command
          raise output if output.match(/Error: /)
        rescue Exception => e
          abort 'Puppet command failed'
        end
      end
    end
  end
end<commit_msg>Add --trace for puppet in debug-mode<commit_after>require 'serverspec'
require 'net/ssh'
require 'vagrant_helper'
require 'yaml'
require 'pathname'

include Serverspec::Helper::Ssh
include Serverspec::Helper::DetectOS

module RSpec
  module Core
    class ExampleGroup
      def get_file
        block = self.class.metadata[:example_group_block]
        if RUBY_VERSION.start_with?('1.8')
          file = block.to_s.match(/.*@(.*):[0-9]+>/)[1]
        else
          file = block.source_location.first
        end
        file
      end
    end
  end
end

RSpec.configure do |c|

  debug = ENV['debug']
  keep_box = ENV['keep_box']
  box = ENV['box'] || 'wheezy'
  c.add_setting :before_files
  c.before_files = []
  root_dir = Dir.getwd
  vagrant_helper = VagrantHelper.new(root_dir, box, true)

  c.before :all do
    file = self.get_file
    unless c.before_files.include? file
      c.before_files.push file
      c.ssh.close if c.ssh

      vagrant_helper.reset unless keep_box
      c.ssh = vagrant_helper.connect
      spec_dir = Dir.new File.dirname file

      if File.exists? spec_dir.to_path + '/facts.json'
        vagrant_facts_path = vagrant_helper.get_path spec_dir.to_path + '/facts.json'
        vagrant_helper.exec("sudo mkdir -p /etc/facter/facts.d && sudo ln -sf #{vagrant_facts_path.shellescape} /etc/facter/facts.d/")
      end

      hiera_config = {
          :backends => ['json'],
          :json => {
              :datadir => '/vagrant'
          },
          :hierarchy => [
              Pathname.new(File.join(spec_dir.to_path, 'hiera')).relative_path_from(Pathname.new(root_dir)).to_s,
              'spec/hiera'
          ]
      }
      hiera_command = "echo #{hiera_config.to_yaml.shellescape} > /etc/hiera.yaml"
      vagrant_helper.exec("sudo bash -c #{hiera_command.shellescape}")

      spec_dir.sort.each do |local_file|
        next unless File.extname(local_file) == '.pp'
        vagrant_manifest_path = vagrant_helper.get_path spec_dir.to_path + '/' + local_file
        command = "sudo puppet apply --verbose --modulepath '/etc/puppet/modules:/vagrant/modules' #{vagrant_manifest_path.shellescape} --hiera_config=/etc/hiera.yaml"
        command += ' --debug --trace' if debug
        begin
          puts
          puts 'Running `' + vagrant_manifest_path + '`'
          output = vagrant_helper.exec command
          raise output if output.match(/Error: /)
        rescue Exception => e
          abort 'Puppet command failed'
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11902380476095219, 'fraction_numerical': 0.002000400080016003, 'mean_word_length': 2.443526170798898, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6158487', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1676, 'n_words': 329}