<commit_before>require_relative '../shared/system.rb'
require_relative '../shared/presence.rb'
require_relative '../shared/debug_output.rb'
require 'colorize'
require 'pathname'
require 'rake'
require 'shellwords'
require 'fileutils'
require 'diffy'

class DabCompareError < RuntimeError
end

require_relative '../shared/args.rb'

raise 'no input' unless $settings[:input]

def base_read_test_file(fname)
  ret = {}
  mode = nil
  open(fname).read.split("\n").each do |line|
    if line.start_with? '## '
      mode = line[2..-1].strip
      ret[mode] ||= []
    else
      ret[mode] << line
    end
  end
  ret.map do |k, v|
    k = k.downcase.gsub(/[^a-z]+/, '_').to_sym
    v = v.join("\n").strip
    [k, v]
  end.to_h
end

def describe_action(input, output, action)
  info = " * #{action}: #{input.to_s.blue.bold} -> #{output.blue.bold}..."
  puts info.white
  yield
  puts "#{info.white} #{'[OK]'.green}"
end

def run_ruby_part(input, output, action, tool, options = '', input_as_arg = false)
  describe_action(input, output, action) do
    if input.is_a? Array
      raise 'must input as arg' unless input_as_arg
      input = input.map(&:to_s).map(&:shellescape).join(' ')
    else
      input = input.to_s.shellescape
    end
    output = output.to_s.shellescape
    options = options.presence
    options = options.to_s if options
    input_part = input_as_arg ? ' ' : '<'
    cmd = "timeout 10 ruby src/#{tool}/#{tool}.rb #{options} #{input_part} #{input} > #{output}"
    begin
      psystem_noecho cmd
    rescue SystemCommandError => e
      STDERR.puts
      STDERR.puts e.stderr
      STDERR.puts
      raise
    end
  end
end

def compare_output(info, actual, expected, soft_match = false)
  match = if soft_match
            actual.uncolorize.include? expected.uncolorize
          else
            actual.uncolorize == expected.uncolorize
          end
  if match
    puts "#{info}... OK!".green
  else
    puts 'Received:'.bold
    puts actual
    puts 'Expected:'.bold
    puts expected
    puts 'Diff:'.bold
    puts Diffy::Diff.new(expected, actual).to_s(:color)
    puts "#{info}... ERROR!".red.bold
    raise DabCompareError.new('test error')
  end
end
<commit_msg>frontend: copy received data to clipboard on failed compare<commit_after>require_relative '../shared/system.rb'
require_relative '../shared/presence.rb'
require_relative '../shared/debug_output.rb'
require 'colorize'
require 'pathname'
require 'rake'
require 'shellwords'
require 'fileutils'
require 'diffy'
require 'clipboard'

class DabCompareError < RuntimeError
end

require_relative '../shared/args.rb'

raise 'no input' unless $settings[:input]

def base_read_test_file(fname)
  ret = {}
  mode = nil
  open(fname).read.split("\n").each do |line|
    if line.start_with? '## '
      mode = line[2..-1].strip
      ret[mode] ||= []
    else
      ret[mode] << line
    end
  end
  ret.map do |k, v|
    k = k.downcase.gsub(/[^a-z]+/, '_').to_sym
    v = v.join("\n").strip
    [k, v]
  end.to_h
end

def describe_action(input, output, action)
  info = " * #{action}: #{input.to_s.blue.bold} -> #{output.blue.bold}..."
  puts info.white
  yield
  puts "#{info.white} #{'[OK]'.green}"
end

def run_ruby_part(input, output, action, tool, options = '', input_as_arg = false)
  describe_action(input, output, action) do
    if input.is_a? Array
      raise 'must input as arg' unless input_as_arg
      input = input.map(&:to_s).map(&:shellescape).join(' ')
    else
      input = input.to_s.shellescape
    end
    output = output.to_s.shellescape
    options = options.presence
    options = options.to_s if options
    input_part = input_as_arg ? ' ' : '<'
    cmd = "timeout 10 ruby src/#{tool}/#{tool}.rb #{options} #{input_part} #{input} > #{output}"
    begin
      psystem_noecho cmd
    rescue SystemCommandError => e
      STDERR.puts
      STDERR.puts e.stderr
      STDERR.puts
      raise
    end
  end
end

def compare_output(info, actual, expected, soft_match = false)
  match = if soft_match
            actual.uncolorize.include? expected.uncolorize
          else
            actual.uncolorize == expected.uncolorize
          end
  if match
    puts "#{info}... OK!".green
  else
    puts 'Received:'.bold
    puts actual
    Clipboard.copy(actual)
    puts 'Expected:'.bold
    puts expected
    puts 'Diff:'.bold
    puts Diffy::Diff.new(expected, actual).to_s(:color)
    puts "#{info}... ERROR!".red.bold
    raise DabCompareError.new('test error')
  end
end

{'fraction_non_alphanumeric': 0.14180272869604116, 'fraction_numerical': 0.0017893088794453142, 'mean_word_length': 2.954022988505747, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '734033', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1536, 'n_words': 367}