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

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

RSpec.configure do |c|
  if ENV['ASK_SUDO_PASSWORD']
    require 'highline/import'
    c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
  else
    c.sudo_password = ENV['<PASSWORD>']
  end
  c.before :all do
    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
    pwd = Pathname.new(file).dirname
    c.add_setting :pwd
    if c.pwd != pwd
      c.pwd = <PASSWORD>
      c.ssh.close if c.ssh

      vagrantIsRunning = `vagrant status`.match(/running/)
      vagrantHasSnapshot = system('vagrant snapshot list 2>/dev/null | grep -q "Name: default "')

      actions = []
      unless vagrantHasSnapshot
        actions.push('destroy -f')
        actions.push('up')
        actions.push('snapshot take default')
      end
      unless vagrantIsRunning
        actions.push('up')
      end
      actions.push('snapshot go default')
      actions.each do |action|
        `vagrant #{action}`
      end

      user = Etc.getlogin
      options = {}
      config = `vagrant ssh-config`
      config.each_line do |line|
        if match = /HostName (.*)/.match(line)
          c.host = match[1]
          options = Net::SSH::Config.for(c.host)
        elsif  match = /User (.*)/.match(line)
          user = match[1]
        elsif match = /IdentityFile (.*)/.match(line)
          options[:keys] = [match[1].gsub(/"/, '')]
        elsif match = /Port (.*)/.match(line)
          options[:port] = match[1]
        end
      end
      c.ssh = Net::SSH.start(c.host, user, options)

      manifest = File.join(Pathname.new(file).dirname, 'manifest.pp')
      manifest = manifest.sub(Dir.getwd, '/vagrant')
      provisioner = "sudo puppet apply --detailed-exitcodes --verbose --modulepath '/vagrant/modules' #{manifest.shellescape} || [ $? -eq 2 ]"
      c.ssh.exec!(provisioner)
    end
  end
end
<commit_msg>Allow multiple manifests to be run in single spec, refactor to ruby standards<commit_after>require 'serverspec'
require 'pathname'
require 'net/ssh'

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

RSpec.configure do |c|
  if ENV['ASK_SUDO_PASSWORD']
    require 'highline/import'
    c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
  else
    c.sudo_password = ENV['<PASSWORD>']
  end
  c.before :all do
    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
    pwd = Pathname.new(file).dirname
    c.add_setting :pwd
    if c.pwd != pwd
      c.pwd = <PASSWORD>
      c.ssh.close if c.ssh

      vagrant_is_running = `vagrant status`.match(/running/)
      vagrant_has_snapshot = system('vagrant snapshot list 2>/dev/null | grep -q "Name: default "')

      actions = []
      unless vagrant_has_snapshot
        actions.push('destroy -f')
        actions.push('up')
        actions.push('snapshot take default')
      end
      unless vagrant_is_running
        actions.push('up')
      end
      actions.push('snapshot go default')
      actions.each do |action|
        `vagrant #{action}`
      end

      user = Etc.getlogin
      options = {}
      config = `vagrant ssh-config`
      config.each_line do |line|
        if match = /HostName (.*)/.match(line)
          c.host = match[1]
          options = Net::SSH::Config.for(c.host)
        elsif  match = /User (.*)/.match(line)
          user = match[1]
        elsif match = /IdentityFile (.*)/.match(line)
          options[:keys] = [match[1].gsub(/"/, '')]
        elsif match = /Port (.*)/.match(line)
          options[:port] = match[1]
        end
      end
      c.ssh = Net::SSH.start(c.host, user, options)


      manifests_dir = Dir.new Pathname.new(file).dirname
      vagrant_manifests_path = manifests_dir.to_path.sub(Dir.getwd, '/vagrant')
      manifests_dir.sort.select do |manifest_path|
        puts manifest_path
        next unless File.extname(manifest_path) == '.pp'
        manifest = vagrant_manifests_path + '/' + manifest_path
        command = "sudo puppet apply --detailed-exitcodes --verbose --modulepath '/vagrant/modules' #{manifest.shellescape} || [ $? -eq 2 ]"
        puts command
        c.ssh.exec! command
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.14172535211267606, 'fraction_numerical': 0.0048415492957746475, 'mean_word_length': 2.4536474164133737, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6231828', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1510, 'n_words': 328}