Module: RSpecConfigurationHelper

Included in:
DBUtils, Rspec2db
Defined in:
lib/rspec2db/utils/rspec_configuration_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_rspec_optionsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rspec2db/utils/rspec_configuration_helper.rb', line 80

def self.check_rspec_options
  rspec_options_file = '.rspec'
  rspec2db_config_file = './rspec2db.yaml'

  return nil unless File.exist?(rspec_options_file)
  config = RSpecConfigurationHelper.load_config(false)
  if config.nil?
    puts 'Creating rspec2db config file (' + rspec2db_config_file + ')'
    RSpecConfigurationHelper.generate_local_config rspec2db_config_file
    RSpecConfigurationHelper.insert_rspec2db_formatter rspec_options_file, rspec2db_config_file
    return RSpecConfigurationHelper.load_config
  end
  puts 'rspec2db config file already exists (' + config['file_path'] + ')'
  config['dbconnection']
end

.generate_local_config(rspec2db_config_file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rspec2db/utils/rspec_configuration_helper.rb', line 26

def self.generate_local_config(rspec2db_config_file)
  if File.exist?(rspec2db_config_file)
    puts 'Default config file exists, override with default? (Y/N)'
    override = STDIN.gets.chomp
    return if override == 'N' || override == 'n'
  end

  puts 'Creating default rspec2db config file (' + rspec2db_config_file + ')'
  rspec2db_bundler_path = Bundler.rubygems.find_name('rspec2db').first.full_gem_path
  rspec2db_config_src = rspec2db_bundler_path + '/config/rspec2db.yml'
  FileUtils.cp rspec2db_config_src, rspec2db_config_file
end

.insert_rspec2db_formatter(rspec_file, rspec2db_config_file) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/rspec2db/utils/rspec_configuration_helper.rb', line 60

def self.insert_rspec2db_formatter(rspec_file, rspec2db_config_file)
  rspec2db_formatter = "--format Rspec2db\n--options #{rspec2db_config_file}"
  puts 'Adding rspec2db options and formatter to ' + rspec_file
  File.open(rspec_file, 'a') do |f|
    f.puts rspec2db_formatter
    f.flush
  end
end

.load_config(failsafe = true) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rspec2db/utils/rspec_configuration_helper.rb', line 2

def self.load_config(failsafe = true)
  rspec_file = '.rspec'
  file_path = nil
  File.open(rspec_file).each do |line|
    file_path = line.split('--options ').last.strip if line.match '--options\s?.+ya?ml'
  end

  if file_path.nil?
    puts 'No rspec2db config specified in .rspec'
    return nil
  elsif !File.exists?(file_path)
    puts 'could not find the config file at the following location: ' + file_path
    abort 'exiting... please check your config file' if failsafe
    nil
  else
    config = YAML::load(File.open(file_path))
    config['file_path'] = file_path
    config
  end
  # Bakir: disabled override by environment variables because it can cause unwanted side-effects 
  #(for example: env variable defined by specific name like BUILD can override what user defined in rspec2db.yaml)
  #override_default_config config
end

.load_local_configObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rspec2db/utils/rspec_configuration_helper.rb', line 39

def self.load_local_config
  rspec2db_default_config_file = './rspec2db.yaml'
  rspec2db_project_config = RSpecConfigurationHelper.load_config
  rspec_options_file = '.rspec'
  unless rspec2db_project_config.nil?
    puts 'Using project config file'
  else
    puts 'No project file specified in .rspec. Looking for default rspec2db.yaml file'
    if File.exists? rspec2db_default_config_file
      rspec2db_project_config = YAML::load(File.open(rspec2db_default_config_file))
    else
      puts 'File not found, generating a default rpsec2db.yaml'
      RSpecConfigurationHelper.generate_local_config(rspec2db_default_config_file)
      rspec2db_project_config = YAML::load(File.open(rspec2db_default_config_file))
    end
    RSpecConfigurationHelper.insert_rspec2db_formatter rspec_options_file, rspec2db_default_config_file
  end

  Hash[rspec2db_project_config['dbconnection'].map { |k, v| [k.to_sym, v] }]
end

.override_default_config(config) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/rspec2db/utils/rspec_configuration_helper.rb', line 69

def self.override_default_config(config)
  config["options"].each do |key, value|
    config["options"][key] = ENV[key.upcase] unless ENV[key.upcase].nil?
  end

  config["dbconnection"].each do |key, value|
    config["dbconnection"][key] = ENV[key.upcase] unless ENV[key.upcase].nil?
  end
  config
end

Instance Method Details

#extract_rspec_core_versionObject



97
98
99
# File 'lib/rspec2db/utils/rspec_configuration_helper.rb', line 97

def extract_rspec_core_version
  Gem.loaded_specs['rspec-core'].version.to_s.split('.').map { |v| v.to_i }
end

#load_snippet_extractorObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rspec2db/utils/rspec_configuration_helper.rb', line 101

def load_snippet_extractor
  major_version, minor_version = @rspec_core_version

  if major_version == 3 && minor_version < 4
    require 'rspec/core/formatters/snippet_extractor'
    RSpec::Core::Formatters::SnippetExtractor.new
  elsif major_version == 3 && minor_version >= 4
    require 'rspec/core/formatters/html_snippet_extractor'
    RSpec::Core::Formatters::HtmlSnippetExtractor.new
  end
end


113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rspec2db/utils/rspec_configuration_helper.rb', line 113

def print_example_failed_content(example)
  print_content = ''
  exception = example.execution_result.exception
  return print_content if exception.backtrace.nil?

  backtrace_content = exception.backtrace.map { |line| RSpec::Core::BacktraceFormatter.new.backtrace_line(line) }
  backtrace_content.compact!
  snippet_extractor ||= load_snippet_extractor

  snippet_content = snippet_extractor.snippet(backtrace_content)
  snippet_content = snippet_content.sub( "class=\"offending\"", "class=\"offending\" style=\"background-color: red;\"" )
  print_content = "    <pre class=\"ruby\" style=\"background-color: #E6E6E6; border: 1px solid;\"><code>#{snippet_content}</code></pre>"
  print_content
end