<commit_before># -------------------------------------------------------------
# Cucumber Automation Test Framework
# Created by <NAME>
# <EMAIL>
# Updated Mar 9, 2013
# Verion 0.2.1 
# -------------------------------------------------------------

# -------------------------------------------------------------
# Notes
# - I use tag prefixes so I can run individual scenarios that have numbered tags
# ex. @test.01, @test.02 will both be caught by @test
# -------------------------------------------------------------

## Fill in to skip questions
@project_directory
@log_folder_name
@tag_prefixes_to_run
@tags_to_skip

END {
  setup
  run_tags
}

# Setup 
def setup
  @question_repeat = 3
  @os_windows = (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
  @os_mac = (/darwin/ =~ RUBY_PLATFORM) != nil
  @os_linux = !@os_windows && !@os_mac

  #Ask questions if necessary
  ask_project_directory if @project_directory.nil?
  verify_project_directory
  ask_log_folder_name if @log_folder_name.nil?
  
  #Create log folder/files
  create_log_folder
  name_log_files

  ask_tag_questions if @tag_prefixes_to_run.nil? unless re_run_non_passed?
  collect_tags_from_directory
end
def ask_project_directory
  print "What is the directory of your cucumber project: "
  @project_directory = question_not_blank
  #puts @project_directory
end
def verify_project_directory
  @question_repeat.times do
    dir = @project_directory
    @project_directory = dir.gsub('~/',"/Users/#{`whoami`.chomp}/") if dir.start_with?('~/')  if @os_mac
    @project_directory = dir.gsub('~/',"/home/#{`whoami`.chomp}/") if dir.start_with?('~/')  if @os_linux 
    break if File.directory?(@project_directory)
    puts "The cucumber directory listed below does not exists, please try again"
    puts "  '#{@project_directory}'"
    directory_help
    ask_project_directory
  end
end
def directory_help
  unless @windows
    array = @project_directory.split('/')
    array.each.with_index do |item,index|
      next if index == 0
      dir = array[0,index + 1].join('/')
      
      unless File.directory?(dir)
        dir = array[0,index].join('/')
        puts "Valid folders in path #{dir}:"
        puts `cd #{dir}; ls`        
        break
      end
    end
  end
  
end
def ask_log_folder_name
  print "Name a log folder(default 'log'): "
  @log_folder_name = STDIN.gets.chomp.strip
  @log_folder_name = 'log' if @log_folder_name == ''
  #puts @log_folder_name
end 
def ask_tag_questions
  print "List important starts of tags, separated by commas (Starting with @): "
  @tag_prefixes_to_run = question_separated_by_commas
  #print "#{@tag_prefixes_to_run}\n"
  if @tags_to_skip.nil?
    print "Are there any tags you would like to skip?(y/n): "
    if question_yes_no
      print 'List tags to skip separated by commas: '
      @tags_to_skip = question_separated_by_commas
      #print "#{@tags_to_skip}\n"
    end
  end
end
def create_log_folder
  @log_directory = File.join(@project_directory,@log_folder_name)
  return if File.directory?(@log_directory)
  Dir::mkdir(@log_directory)
  #puts @log_directory
end
  summary = "atf-summary.txt"
  failure = "atf-fail-#{time}.txt"
  @summary_log_path = File.join(@log_directory,summary)
  @failure_log_path = File.join(@log_directory,failure)
end
def re_run_non_passed?
  history = get_tags_from_last_summary
  unless history.any? #If never run or all tests were previously passed
    archive_last_summary
    return false 
  end
  print "Would you like to rerun the #{history.length} unpassed tags?(y/n): "
  response = question_yes_no
  
  if response
    @tags = history
  else
    archive_last_summary
  end
  
  return response
end

#Previous Summary
def get_tags_from_last_summary
  return [] unless File.exists? @summary_log_path 
  File.open(@summary_log_path, 'r') {|f| @old_summary = f.read }
  parsed = parse_summary_lines
  non_passed = parsed[0] - parsed [1]
  failed = parsed[2] - parsed[1]
  non_passed_failed_last = non_passed - failed + failed
  non_passed_failed_last
end
def parse_summary_lines
  array, line_array = [[],[],[]] , ['','','']
  
  @old_summary.each_line do |line|
    line_array[0] += line if line.start_with? 'Scheduled'
    line_array[1] += line if line.start_with? 'Passed'
    line_array[2] += line if line.start_with? 'Failed'
  end
  
  line_array.each_with_index do |item,index|
    item.each_line do |line|
      item = line.strip.gsub(']','').gsub('"','').split('[')[1]
      item = item.split(',')if line.include? ','      
      array[index] << item 
      array[index] = array[index].flatten.uniq  
    end
  end
  array  
end
def archive_last_summary
  archive_name = "atf-summary_before-#{time}.txt"
  if File.exists? @summary_log_path
    File.open(File.join(@log_directory,archive_name), 'w') {|f| f.write(@old_summary)} 
    File.delete(@summary_log_path)
  end
end

#Gather tags from feature files
def collect_tags_from_directory
  return if @tags.any? if !!@tags #skip if collected from summary
  @tags = []
  features = find_feature_files(File.join(@project_directory,'features',''))
  features.each do |feature|
    @tags << scan_features_for_tags(feature)
    @tags.flatten!.uniq!<commit_msg>Bug-Fix<commit_after># -------------------------------------------------------------
# Cucumber Automation Test Framework
# Created by <NAME>
# <EMAIL>
# Updated Mar 9, 2013
# Verion 0.2.1 
# -------------------------------------------------------------

# -------------------------------------------------------------
# Notes
# - I use tag prefixes so I can run individual scenarios that have numbered tags
# ex. @test.01, @test.02 will both be caught by @test
# -------------------------------------------------------------

## Fill in to skip questions
@project_directory
@log_folder_name
@tag_prefixes_to_run
@tags_to_skip

END {
  setup
  run_tags
}

# Setup 
def setup
  @question_repeat = 3
  @os_windows = (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
  @os_mac = (/darwin/ =~ RUBY_PLATFORM) != nil
  @os_linux = !@os_windows && !@os_mac

  #Ask questions if necessary
  ask_project_directory if @project_directory.nil?
  verify_project_directory
  ask_log_folder_name if @log_folder_name.nil?
  
  #Create log folder/files
  create_log_folder
  name_log_files

  ask_tag_questions if @tag_prefixes_to_run.nil? unless re_run_non_passed?
  collect_tags_from_directory
end
def ask_project_directory
  print "What is the directory of your cucumber project: "
  @project_directory = question_not_blank
  #puts @project_directory
end
def verify_project_directory
  @question_repeat.times do
    dir = @project_directory
    @project_directory = dir.gsub('~/',"/Users/#{`whoami`.chomp}/") if dir.start_with?('~/')  if @os_mac
    @project_directory = dir.gsub('~/',"/home/#{`whoami`.chomp}/") if dir.start_with?('~/')  if @os_linux 
    break if File.directory?(@project_directory)
    puts "The cucumber directory listed below does not exists, please try again"
    puts "  '#{@project_directory}'"
    directory_help
    ask_project_directory
  end
end
def directory_help
  unless @windows
    array = @project_directory.split('/')
    array.each.with_index do |item,index|
      next if index == 0
      dir = array[0,index + 1].join('/')
      
      unless File.directory?(dir)
        dir = array[0,index].join('/')
        puts "Valid folders in path #{dir}:"
        puts `cd #{dir}; ls`        
        break
      end
    end
  end
  
end
def ask_log_folder_name
  print "Name a log folder(default 'log'): "
  @log_folder_name = STDIN.gets.chomp.strip
  @log_folder_name = 'log' if @log_folder_name == ''
  #puts @log_folder_name
end 
def ask_tag_questions
  print "List important starts of tags, separated by commas (Starting with @): "
  @tag_prefixes_to_run = question_separated_by_commas
  #print "#{@tag_prefixes_to_run}\n"
  if @tags_to_skip.nil?
    print "Are there any tags you would like to skip?(y/n): "
    if question_yes_no
      print 'List tags to skip separated by commas: '
      @tags_to_skip = question_separated_by_commas
      #print "#{@tags_to_skip}\n"
    end
  end
end
def create_log_folder
  @log_directory = File.join(@project_directory,@log_folder_name)
  return if File.directory?(@log_directory)
  Dir::mkdir(@log_directory)
  #puts @log_directory
end
  summary = "atf-summary.txt"
  failure = "atf-fail-#{time}.txt"
  @summary_log_path = File.join(@log_directory,summary)
  @failure_log_path = File.join(@log_directory,failure)
end
def re_run_non_passed?
  history = get_tags_from_last_summary
  unless history.any? #If never run or all tests were previously passed
    archive_last_summary
    return false 
  end
  print "Would you like to rerun the #{history.length} unpassed tags?(y/n): "
  response = question_yes_no
  
  if response
    @tags = history
  else
    archive_last_summary
  end
  
  return response
end

#Previous Summary
def get_tags_from_last_summary
  return [] unless File.exists? @summary_log_path 
  File.open(@summary_log_path, 'r') {|f| @old_summary = f.read }
  parsed = parse_summary_lines
  non_passed = parsed[0] - parsed [1]
  failed = parsed[2] - parsed[1]
  non_passed_failed_last = non_passed - failed + failed
  non_passed_failed_last
end
def parse_summary_lines
  array, line_array = [[],[],[]] , ['','','']
  
  @old_summary.each_line do |line|
    line_array[0] += line if line.start_with? 'Scheduled'
    line_array[1] += line if line.start_with? 'Passed'
    line_array[2] += line if line.start_with? 'Failed'
  end
  
  line_array.each_with_index do |item,index|
    item.each_line do |line|
      item = line.strip.gsub(']','').gsub('"','').gsub(' ','').split('[')[1]
      item = item.split(',')if line.include? ','      
      array[index] << item 
      array[index] = array[index].flatten.uniq  
    end
  end
  array  
end
def archive_last_summary
  archive_name = "atf-summary_before-#{time}.txt"
  if File.exists? @summary_log_path
    File.open(File.join(@log_directory,archive_name), 'w') {|f| f.write(@old_summary)} 
    File.delete(@summary_log_path)
  end
end

#Gather tags from feature files
def collect_tags_from_directory
  return if @tags.any? if !!@tags #skip if collected from summary
  @tags = []
  features = find_feature_files(File.join(@project_directory,'features',''))
  features.each do |feature|
    @tags << scan_features_for_tags(feature)
    @tags.flatten!.uniq!
{'fraction_non_alphanumeric': 0.15859412614347618, 'fraction_numerical': 0.004814636494944632, 'mean_word_length': 3.17443729903537, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, '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': '4068848', 'n_tokens_mistral': 3530, 'n_tokens_neox': 3460, 'n_words': 828}