# You need Ruby (Rake, RWebSpec, ci_reporter gems installed)
#   Simplest way on Windows is to install RubyShell (http://agileway.com.au/downloads)

require 'rubygems'
gem 'ci_reporter'
gem 'rspec'
require 'rspec/core/rake_task'
require 'ci/reporter/rake/rspec' # use this if you're using RSpec

load File.join(File.dirname(__FILE__), "buildwise.rake")

## Settings: Customize here...
# 
BUILDWISE_URL = ENV["BUILDWISE_MASTER"] || "http://buildwise.dev"
 
FULL_BUILD_MAX_TIME = 60 * 60   # 1 hour
FULL_BUILD_CHECK_INTERVAL = 60  # 1 minute

$test_dir =  File.expand_path( File.join( File.dirname(__FILE__), "spec" ) ) # change to aboslution path if invoktion is not this directory
# rspec will be created 'spec/reports' under checkout dir

# List tests you want to exclude
#
def excluded_spec_files
  # NOTE, testing only for faster develping agent, remove a couple of test later
  ["debugging_spec.rb", "passenger_spec.rb"]
end

def all_specs
  Dir.glob("#{$test_dir}/*_spec.rb")
end

def specs_for_quick_build
  # list test files to be run in a quick build, leave the caller to set full path
  [
    "login_spec.rb", 
    "flight_spec.rb",
    "payment_spec.rb",
    "passenger_spec.rb",
    "not_exists_spec.rb" # will exclude non-exists test quietly
  ]
end

desc "run tests in this spec/ folder, option to use INTELLIGENT_ORDERING or/and DYNAMIC_FEEDBACK"
RSpec::Core::RakeTask.new("ui_tests:quick") do |t|
  specs_to_be_executed = buildwise_determine_specs_for_quick_build(specs_for_quick_build, excluded_spec_files, $test_dir);
  buildwise_formatter =  File.join(File.dirname(__FILE__), "buildwise_rspec_formatter.rb")
  t.rspec_opts = "--pattern my_own_custom_order --require #{buildwise_formatter} #{specs_to_be_executed.join(' ')} --order defined"
end



desc "run quick tests from BuildWise"
task "ci:ui_tests:quick" => ["ci:setup:rspec"] do
  build_id = buildwise_start_build(:working_dir => File.expand_path(File.dirname(__FILE__)))
  buildwise_run_sequential_build_target(build_id, "ui_tests:quick")
end


## Full Build
#
#  TODO - how to determin useing RSpec or Cucumber
#
desc "Running tests distributedly"
task "ci:ui_tests:full" => ["ci:setup:rspec"] do
  build_id = buildwise_start_build(:working_dir => File.expand_path(File.dirname(__FILE__)),
                                   :ui_test_dir => ["spec"],
                                   :excluded => excluded_spec_files || [],
                                   :parallel => true
  )
  buildwise_montior_parallel_execution(build_id, :max_wait_time => FULL_BUILD_MAX_TIME, :check_interval => FULL_BUILD_CHECK_INTERVAL)
end


desc "run all tests in this folder"
RSpec::Core::RakeTask.new("go") do |t|
  test_files = Dir.glob("*_spec.rb") + Dir.glob("*_test.rb") - excluded_test_files
  t.pattern = FileList[test_files]
  t.rspec_opts = "" # to enable warning: "-w"
end
