<commit_before>#!/opt/puppet/bin/ruby
#  $stderr = File.new('/dev/null', 'w')

Dir.chdir "/root/.testing"
require 'rubygems'
require 'serverspec'
require 'pathname'
require 'yaml'
require 'trollop'

opts = Trollop::options do
  opt :progress, "Display details of tasks completed", :default => true
  opt :brief, "Display number of tasks completed"
  opt :name, "Name of the quest to track", :type => :string
  opt :completed, "Display completed quests"
  opt :showall, "Show all available quests"
  opt :start, "Provide name of the quest to start tracking", :type => :string
  opt :current, "Name of the quest in progress"
end

if opts[:showall] then
  puts "The following quests are available: "
  Dir.glob('/root/.testing/spec/localhost/*_spec.rb').each do |f|
    puts File.basename(f).gsub('_spec.rb','').capitalize
  end
  exit
end

if opts[:completed] then
  puts "The following quests have been completed! :"
  questlog = YAML::load_file('/root/.testing/log.yml')
  questlog.each do |key, value|
    if value.is_a?(Hash) then
        puts value['name'].capitalize
    end
  end
  exit
end
  
if opts[:current] then
  questlog = YAML::load_file('/root/.testing/log.yml')
  puts questlog['current'].capitalize
  exit
end

if opts[:start] then
  questlog = YAML::load_file('/root/.testing/log.yml')
  if File.exist?("/root/.testing/spec/localhost/#{opts[:start]}_spec.rb") then
    questlog['current'] = opts[:start].downcase
    File.open('/root/.testing/log.yml', 'w') { |f| f.write questlog.to_yaml }
    puts "You are starting the #{opts[:start].capitalize} quest."
  else
    puts "Please select another quest. The Quest you specified does not exist."
    puts "The command: 'quests --showall' will list all available quests."
  end
end

if opts[:name].nil? then
  questlog = YAML::load_file('/root/.testing/log.yml')
  name = questlog['current']
else
  name = opts[:name]
end
  
include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS

RSpec.configure do |c|
  c.tty = true
  c.color_enabled = true
  c.add_formatter(:json)
  if ENV['ASK_SUDO_PASSWORD']
    require 'highline/import'
    c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
  else
    c.sudo_password = ENV['SU<PASSWORD>PASSWORD']
  end

  #c.before(:all, &:silence_output)
  #c.after(:all, &:enable_output)
end

# Redirects stderr and stdout to /dev/null.
def silence_output
  #@orig_stderr = $stderr
  #@orig_stdout = $stdout

  # redirect stderr and stdout to /dev/null
  #$stderr = File.new('/dev/null', 'w')
  #$stdout = File.new('/dev/null', 'w')
end

# Replace stdout and stderr so anything else is output correctly.
def enable_output
  $stderr = @orig_stderr
  $stdout = @orig_stdout
  @orig_stderr = nil
  @orig_stdout = nil
end

config = RSpec.configuration
json_formatter = RSpec::Core::Formatters::JsonFormatter.new(config.out)
reporter  = RSpec::Core::Reporter.new(json_formatter)
config.instance_variable_set(:@reporter, reporter)

## Run RSpec on the policies directory
RSpec::Core::Runner.run(["/root/.testing/spec/localhost/#{name}_spec.rb"])

total = 0
failures = []
successes = []
json_formatter.output_hash[:examples].each do |example|
  total += 1
  if example[:status] == 'failed' then
    failures.push example[:full_description]
  else
    successes.push example[:full_description]
  end
end
total = failures.length + successes.length

if opts[:progress] && !opts[:brief] && !opts[:start] then
  if failures.length != 0 then 
    STDOUT.puts "The following tasks were not completed:"
    failures.each { |x| puts " - #{x}" }
  end
  if successes.length != 0 then 
    STDOUT.puts "The following tasks were completed successfully! :"
    successes.each { |x| puts x }
  end
  STDOUT.puts "You successfully completed #{successes.length} tasks, out of a total of #{total} tasks!"
end

if successes.length == total then
  quests_complete = YAML::load_file('/root/.testing/log.yml')
  if !quests_complete.has_key?(name) then
    quests_complete["#{name}"] = Hash.new
    quests_complete["#{name}"]['name'] = "#{name}"
    quests_complete["#{name}"]['tasks'] = total
    quests_complete["#{name}"]['tasks_done'] = successes.length
    quests_complete["#{name}"]['time'] = Time.now.getutc
    File.open('/root/.testing/log.yml', 'w') {|f| f.write quests_complete.to_yaml }
  end
end

if opts[:brief]  then
  STDOUT.puts "#{successes.length}/#{total}"
end
<commit_msg> No is better than 0/0<commit_after>#!/opt/puppet/bin/ruby
#  $stderr = File.new('/dev/null', 'w')

Dir.chdir "/root/.testing"
require 'rubygems'
require 'serverspec'
require 'pathname'
require 'yaml'
require 'trollop'

opts = Trollop::options do
  opt :progress, "Display details of tasks completed", :default => true
  opt :brief, "Display number of tasks completed"
  opt :name, "Name of the quest to track", :type => :string
  opt :completed, "Display completed quests"
  opt :showall, "Show all available quests"
  opt :start, "Provide name of the quest to start tracking", :type => :string
  opt :current, "Name of the quest in progress"
end

if opts[:showall] then
  puts "The following quests are available: "
  Dir.glob('/root/.testing/spec/localhost/*_spec.rb').each do |f|
    puts File.basename(f).gsub('_spec.rb','').capitalize
  end
  exit
end

if opts[:completed] then
  puts "The following quests have been completed! :"
  questlog = YAML::load_file('/root/.testing/log.yml')
  questlog.each do |key, value|
    if value.is_a?(Hash) then
        puts value['name'].capitalize
    end
  end
  exit
end
  
if opts[:current] then
  questlog = YAML::load_file('/root/.testing/log.yml')
  puts questlog['current'].capitalize
  exit
end

if opts[:start] then
  questlog = YAML::load_file('/root/.testing/log.yml')
  if File.exist?("/root/.testing/spec/localhost/#{opts[:start]}_spec.rb") then
    questlog['current'] = opts[:start].downcase
    File.open('/root/.testing/log.yml', 'w') { |f| f.write questlog.to_yaml }
    puts "You are starting the #{opts[:start].capitalize} quest."
  else
    puts "Please select another quest. The Quest you specified does not exist."
    puts "The command: 'quests --showall' will list all available quests."
  end
end

if opts[:name].nil? then
  questlog = YAML::load_file('/root/.testing/log.yml')
  name = questlog['current']
else
  name = opts[:name]
end
  
include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS

RSpec.configure do |c|
  c.tty = true
  c.color_enabled = true
  c.add_formatter(:json)
  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, &:silence_output)
  #c.after(:all, &:enable_output)
end

# Redirects stderr and stdout to /dev/null.
def silence_output
  #@orig_stderr = $stderr
  #@orig_stdout = $stdout

  # redirect stderr and stdout to /dev/null
  #$stderr = File.new('/dev/null', 'w')
  #$stdout = File.new('/dev/null', 'w')
end

# Replace stdout and stderr so anything else is output correctly.
def enable_output
  $stderr = @orig_stderr
  $stdout = @orig_stdout
  @orig_stderr = nil
  @orig_stdout = nil
end

config = RSpec.configuration
json_formatter = RSpec::Core::Formatters::JsonFormatter.new(config.out)
reporter  = RSpec::Core::Reporter.new(json_formatter)
config.instance_variable_set(:@reporter, reporter)

## Run RSpec on the policies directory
RSpec::Core::Runner.run(["/root/.testing/spec/localhost/#{name}_spec.rb"])

total = 0
failures = []
successes = []
json_formatter.output_hash[:examples].each do |example|
  total += 1
  if example[:status] == 'failed' then
    failures.push example[:full_description]
  else
    successes.push example[:full_description]
  end
end
total = failures.length + successes.length

if opts[:progress] && !opts[:brief] && !opts[:start] then
  if failures.length != 0 then 
    STDOUT.puts "The following tasks were not completed:"
    failures.each { |x| puts " - #{x}" }
  end
  if successes.length != 0 then 
    STDOUT.puts "The following tasks were completed successfully! :"
    successes.each { |x| puts x }
  end
  STDOUT.puts "You successfully completed #{successes.length} tasks, out of a total of #{total} tasks!"
end

if successes.length == total then
  quests_complete = YAML::load_file('/root/.testing/log.yml')
  if !quests_complete.has_key?(name) then
    quests_complete["#{name}"] = Hash.new
    quests_complete["#{name}"]['name'] = "#{name}"
    quests_complete["#{name}"]['tasks'] = total
    quests_complete["#{name}"]['tasks_done'] = successes.length
    quests_complete["#{name}"]['time'] = Time.now.getutc
    File.open('/root/.testing/log.yml', 'w') {|f| f.write quests_complete.to_yaml }
  end
end

if opts[:brief]  then
  if total == 0 then
    STDOUT.puts "No"
  else
    STDOUT.puts "#{successes.length}/#{total}"
  end
end

{'fraction_non_alphanumeric': 0.1493212669683258, 'fraction_numerical': 0.0012443438914027148, 'mean_word_length': 4.18533724340176, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5287443', 'n_tokens_mistral': 3050, 'n_tokens_neox': 2992, 'n_words': 783}