CGC_Codes

RomSpec helper

Jun 5th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.14 KB | None | 0 0
  1. require 'bundler'
  2. Bundler.setup
  3.  
  4. if RUBY_ENGINE == 'ruby' && ENV['COVERAGE'] == 'true'
  5.   require 'yaml'
  6.   rubies = YAML.load(File.read(File.join(__dir__, '..', '.travis.yml')))['rvm']
  7.   latest_mri = rubies.select { |v| v =~ /\A\d+\.\d+.\d+\z/ }.max
  8.  
  9.   if RUBY_VERSION == latest_mri
  10.     require 'simplecov'
  11.     SimpleCov.start do
  12.       add_filter '/spec/'
  13.     end
  14.   end
  15. end
  16.  
  17. require 'rom-sql'
  18. require 'rom/sql/rake_task'
  19.  
  20. require 'logger'
  21. require 'tempfile'
  22.  
  23. begin
  24.   require 'byebug'
  25. rescue LoadError
  26.   require 'pry'
  27. end
  28.  
  29. LOGGER = Logger.new(File.open('./log/test.log', 'a'))
  30. ENV['TZ'] ||= 'UTC'
  31.  
  32. oracle_settings = {
  33.   db_name: ENV.fetch('ROM_ORACLE_DATABASE', 'xe'),
  34.   host: ENV.fetch('ROM_ORACLE_HOST', 'localhost'),
  35.   port: Integer(ENV.fetch('ROM_ORACLE_PORT', '1521'))
  36. }
  37.  
  38. if defined? JRUBY_VERSION
  39.   DB_URIS = {
  40.     sqlite: 'jdbc:sqlite:::memory',
  41.     postgres: 'jdbc:postgresql://localhost/rom_sql',
  42.     mysql: 'jdbc:mysql://localhost/rom_sql?user=root&sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION',
  43.     oracle: ENV['ROM_USE_ORACLE'] ? fail('Setup Oracle for JRuby!') : nil
  44.   }
  45. else
  46.   DB_URIS = {
  47.     sqlite: 'sqlite::memory',
  48.     postgres: 'postgres://localhost/rom_sql',
  49.     mysql: 'mysql2://root@localhost/rom_sql?sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION',
  50.     oracle: "oracle://#{ oracle_settings[:host] }:#{ oracle_settings[:port] }/" \
  51.             "#{ oracle_settings[:db_name] }?username=rom_sql&password=rom_sql&autosequence=true"
  52.   }
  53. end
  54.  
  55. ADAPTERS = ENV['ROM_USE_ORACLE'] ? DB_URIS.keys : DB_URIS.keys - %i(oracle)
  56. PG_LTE_95 = ENV.fetch('PG_LTE_95', 'true') == 'true'
  57.  
  58. SPEC_ROOT = root = Pathname(__FILE__).dirname
  59.  
  60. TMP_PATH = root.join('../tmp')
  61.  
  62. class ROM::SQL::Schema::Inferrer
  63.   def self.on_error(*)
  64.     # quiet in specs
  65.   end
  66. end
  67.  
  68. require 'dry/core/deprecations'
  69. Dry::Core::Deprecations.set_logger!(root.join('../log/deprecations.log'))
  70.  
  71. ROM::SQL.load_extensions(:postgres, :sqlite)
  72.  
  73. require 'dry-types'
  74. module Types
  75.   include Dry::Types.module
  76. end
  77.  
  78. def with_adapters(*args, &block)
  79.   reset_adapter = Hash[*ADAPTERS.flat_map { |a| [a, false] }]
  80.   adapters = args.empty? || args[0] == :all ? ADAPTERS : (args & ADAPTERS)
  81.  
  82.   adapters.each do |adapter|
  83.     context("with #{adapter}", **reset_adapter, adapter => true, &block)
  84.   end
  85. end
  86.  
  87. warning_api_available = RUBY_VERSION >= '2.4.0'
  88.  
  89. module SileneceWarnings
  90.   def warn(str)
  91.     if str['/sequel/'] || str['/rspec-core']
  92.       nil
  93.     else
  94.       super
  95.     end
  96.   end
  97. end
  98.  
  99. Warning.extend(SileneceWarnings) if warning_api_available
  100.  
  101. RSpec.configure do |config|
  102.   config.disable_monkey_patching!
  103.   config.warnings = warning_api_available
  104.  
  105.   config.before(:suite) do
  106.     tmp_test_dir = TMP_PATH.join('test')
  107.     FileUtils.rm_r(tmp_test_dir) if File.exist?(tmp_test_dir)
  108.     FileUtils.mkdir_p(tmp_test_dir)
  109.   end
  110.  
  111.   config.before do
  112.     module Test
  113.     end
  114.   end
  115.  
  116.   config.after do
  117.     Object.send(:remove_const, :Test)
  118.   end
  119.  
  120.   Dir[root.join('shared/**/*.rb')].each { |f| require f }
  121.   Dir[root.join('support/**/*.rb')].each { |f| require f }
  122.  
  123.   config.include(Helpers, helpers: true)
  124.   config.include ENVHelper
  125. end
Advertisement
Add Comment
Please, Sign In to add comment