#-------------------------------------------------------------------------
# # Copyright (c) Microsoft and contributors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#--------------------------------------------------------------------------
require 'rake/testtask'
require 'rubygems/package_task'
require 'dotenv/tasks'

gem_spec = eval(File.read('./azure.gemspec'))
Gem::PackageTask.new(gem_spec) do |pkg|
  pkg.need_zip = false
  pkg.need_tar = false
end

namespace :test do
  task :require_environment => :dotenv do
    unset_environment = [
      ENV.fetch('AZURE_STORAGE_ACCOUNT',  nil),
      ENV.fetch('AZURE_STORAGE_ACCESS_KEY',    nil),
      ENV.fetch('AZURE_SERVICEBUS_NAMESPACE', nil),
      ENV.fetch('AZURE_SERVICEBUS_ACCESS_KEY', nil),
      ENV.fetch('AZURE_MANAGEMENT_CERTIFICATE', nil),
      ENV.fetch('AZURE_SUBSCRIPTION_ID', nil)
    ].include?(nil)

    abort '[ABORTING] Configure your environment to run the integration tests' if unset_environment
  end

  Rake::TestTask.new :unit do |t|
    t.pattern = 'test/unit/**/*_test.rb'
    t.verbose = true
    t.libs = %w(lib test)
  end

  namespace :unit do
    def component_task(component)
      Rake::TestTask.new component do |t|
        t.pattern = "test/unit/#{component}/**/*_test.rb"
        t.verbose = true
        t.libs = %w(lib test)
      end
    end

    component_task :affinity_group
    component_task :base_management
    component_task :blob
    component_task :cloud_service_management
    component_task :core
    component_task :database
    component_task :service
    component_task :storage_management
    component_task :table
    component_task :virtual_machine_image_management
    component_task :virtual_machine_management
    component_task :vnet
  end

  Rake::TestTask.new :integration do |t|
    t.test_files = Dir['test/integration/**/*_test.rb'].reject do |path|
      path.include?('database')
    end
    t.verbose = true
    t.libs = %w(lib test)
  end

  task :integration => :require_environment

  namespace :integration do
    def component_task(component)
      Rake::TestTask.new component do |t|
        t.pattern = "test/integration/#{component}/**/*_test.rb"
        t.verbose = true
        t.libs = %w(lib test)
      end

      task component => 'test:require_environment'
    end

    component_task :affinity_group
    component_task :blob
    component_task :cloud_service
    component_task :database
    component_task :location
    component_task :queue
    component_task :service_bus
    component_task :storage_management
    component_task :table
    component_task :vm
    component_task :vm_image
    component_task :vnet
  end

  Rake::TestTask.new :recorded do |t|
    t.test_files = Dir['test/integration/**/*_test.rb'].reject do |path|
      # Reject the test paths those are not yet VCR recorded
      # Following three are Azure-Storage gem features and need to be removed when we take dependency on azure-storage gem
      path.include?('/blob/') || path.include?('/queue/') || path.include?('/table/')
    end
    t.verbose = true
    t.libs = %w(lib test)
  end

  namespace :recorded do
    def component_task(component)
      Rake::TestTask.new component do |t|
        t.pattern = "test/integration/#{component}/**/*_test.rb"
        t.verbose = true
        t.libs = %w(lib test)
      end
    end

    component_task :affinity_group
    component_task :cloud_service
    component_task :database
    component_task :location
    component_task :service_bus
    component_task :storage_management
    component_task :vm
    component_task :vm_image
    component_task :vnet
  end

  task :cleanup => :require_environment do
    $:.unshift 'lib'
    require 'azure'

    Azure.configure do |config|
      config.access_key     = ENV.fetch('AZURE_STORAGE_ACCESS_KEY')
      config.account_name   = ENV.fetch('AZURE_STORAGE_ACCOUNT')
      config.acs_namespace  = ENV.fetch('AZURE_SERVICEBUS_NAMESPACE')
      config.sb_access_key  = ENV.fetch('AZURE_SERVICEBUS_ACCESS_KEY')
      config.management_certificate  = ENV.fetch('AZURE_MANAGEMENT_CERTIFICATE')
      config.management_endpoint  = ENV.fetch('AZURE_MANAGEMENT_ENDPOINT')
      config.sql_database_management_endpoint = ENV.fetch('AZURE_SQL_DATABASE_MANAGEMENT_ENDPOINT')
      config.subscription_id  = ENV.fetch('AZURE_SUBSCRIPTION_ID')
    end
  end
end

task :test => %w(test:unit test:integration)

task :default => :test
