require "bundler/gem_tasks" def __current__ Pathname( File.expand_path('..', __FILE__) ) end def git_specs(command, options={}) sh "git --git-dir=#{__current__.join('../tmp/elasticsearch/.git')} --work-tree=#{__current__.join('../tmp/elasticsearch')} #{command}", options end task(:default) { system "rake --tasks" } task :test => 'test:unit' # ----- Test tasks ------------------------------------------------------------ require 'rake/testtask' require 'rspec/core/rake_task' namespace :test do RSpec::Core::RakeTask.new(:spec) desc "Update the repository with YAML tests" task :update do git_specs "fetch origin --verbose", :verbose => true end desc "Wait for Elasticsearch to be in a green state" task :wait_for_green do sh '../scripts/wait-cluster.sh' end Rake::TestTask.new(:unit) do |test| test.libs << 'lib' << 'test' test.test_files = FileList["test/unit/**/*_test.rb"] test.deps = [ :spec ] # test.verbose = true # test.warning = true end desc "Run integration tests" task :integration => :wait_for_green do require 'elasticsearch' branches = `git --git-dir=#{__current__.join('../tmp/elasticsearch/.git')} --work-tree=#{__current__.join('../tmp/elasticsearch')} branch --no-color` current_branch = branches. split("\n"). select { |b| b =~ /^\*/ }. reject { |b| b =~ /no branch|detached/ }. map { |b| b.gsub(/^\*\s*/, '') }. first unless current_branch STDERR.puts "[!] Unable to determine current branch, defaulting to 'master'" current_branch = 'master' end # Define the task t = Rake::TestTask.new(:integration) do |test| test.deps = [ :wait_for_green ] test.libs << 'lib' << 'test' test.test_files = FileList["test/integration/yaml_test_runner.rb", "test/integration/**/*_test.rb"] test.verbose = false test.warning = false end # Check if a test cluster is running begin url = ENV['TEST_CLUSTER_URL'] || ENV['TEST_ES_SERVER'] url = "http://localhost:#{ENV['TEST_CLUSTER_PORT'] || 9250}" unless url client = Elasticsearch::Client.new :url => url es_version_info = client.info['version'] build_hash = es_version_info['build_hash'] cluster_running = true rescue Faraday::ConnectionFailed STDERR.puts "[!] Test cluster not running?" cluster_running = false end checkout_specs_version = ENV['TEST_NO_CHECKOUT'].nil? ? true : false checkout_build_hash = ENV['TEST_BUILD_REF'] || build_hash ENV['TEST_BUILD_REF'] = checkout_build_hash begin unless checkout_specs_version STDERR.puts '-'*80, "YAML tests: Not switching, TEST_NO_CHECKOUT=y", '-'*80 end if checkout_specs_version && !checkout_build_hash STDERR.puts "[!] Cannot determine checkout build hash -- server not running or TEST_BUILD_REF not specified" exit(1) end if checkout_specs_version && checkout_build_hash # Checkout the commit corresponding to the running server build, or passed TEST_BUILD_REF name = ENV['CI'] ? checkout_build_hash : "[\e[1m#{checkout_build_hash}\e[0m]" STDERR.puts '-'*80, "YAML tests: Switching to #{name} from #{current_branch}", '-'*80 git_specs "checkout #{checkout_build_hash} --force --quiet" end # Run the task args = [t.ruby_opts_string, t.run_code, t.file_list_string, t.option_list].join(' ') ruby args do |ok, status| if !ok && status.respond_to?(:signaled?) && status.signaled? raise SignalException.new(status.termsig) elsif !ok fail "Command failed with status (#{status.exitstatus}): " + "[ruby #{args}]" end end ensure git_specs "checkout #{current_branch} --force --quiet" if checkout_specs_version && current_branch end end desc "Run unit and integration tests" task :all do Rake::Task['test:unit'].invoke Rake::Task['test:integration'].invoke end namespace :cluster do desc "Start Elasticsearch nodes for tests" task :start do $LOAD_PATH << File.expand_path('../../elasticsearch-transport/lib', __FILE__) << File.expand_path('../test', __FILE__) require 'elasticsearch/extensions/test/cluster' Elasticsearch::Extensions::Test::Cluster.start end desc "Stop Elasticsearch nodes for tests" task :stop do $LOAD_PATH << File.expand_path('../../elasticsearch-transport/lib', __FILE__) << File.expand_path('../test', __FILE__) require 'elasticsearch/extensions/test/cluster' Elasticsearch::Extensions::Test::Cluster.stop end end end # ----- Documentation tasks --------------------------------------------------- require 'yard' YARD::Rake::YardocTask.new(:doc) do |t| t.options = %w| --embed-mixins --markup=markdown | end # ----- Code analysis tasks --------------------------------------------------- if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9' require 'cane/rake_task' Cane::RakeTask.new(:quality) do |cane| cane.abc_max = 15 cane.no_style = true end end