#!/usr/bin/env ruby


require 'thor'
require 'cuke_cataloger'


class CLI < Thor

  desc 'catalog_test_cases', 'Catalog the test cases in LOCATION with an id based on PREFIX'
  option :location, :default => '.'
  option :prefix, :default => '@test_case_'
  option :row_id, :type => :boolean, :default => true
  option :id_column_name, :default => 'test_case_id'

  def catalog_test_cases
    puts "Tagging tests in '#{options[:location]}' with tag '#{options[:prefix]}'\n"
    puts "Including outline rows\n" if options[:row_id]

    tagger = CukeCataloger::UniqueTestCaseTagger.new
    tagger.tag_tests(options[:location], options[:prefix], {}, options[:row_id], options[:id_column_name])
  end

  desc 'validate_test_cases', 'Validate the test cases in LOCATION with an id based on PREFIX. Will output the report to FILE, if provided.'
  option :location, :default => '.'
  option :prefix, :default => '@test_case_'
  option :row_id, :type => :boolean, :default => true
  option :id_column_name, :default => 'test_case_id'
  option :file

  def validate_test_cases
    puts "Validating tests in '#{options[:location]}' with tag '#{options[:prefix]}'\n"
    puts "Including outline rows\n" if options[:row_id]

    results = CukeCataloger::UniqueTestCaseTagger.new.validate_test_ids(options[:location], options[:prefix], options[:row_id], options[:id_column_name])
    report_text = CukeCataloger::TextReportFormatter.new.format_data(results)

    if options[:file]
      puts "Problems found: #{results.count}"
      File.open(options[:file], 'w') { |file| file.write(report_text) }
    else
      puts report_text
    end
  end

end


CLI.start(ARGV)