Class: TConsole::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/tconsole/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Config

Returns a new instance of Config.



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

def initialize(argv = [])
  self.trace_execution = false
  self.test_dir = "test"
  self.include_paths = ["./test", "./lib"]
  self.preload_paths = []
  self.fail_fast = false
  self.file_sets = {
    "all" => ["#{test_dir}/**/*_test.rb"]
  }

  # load any args into this config that were passed
  load_args(argv)

  @after_load = nil
  @before_load = nil
  @before_test_run = nil

  @cached_suite_counts = {}
  @cached_elements = {}
end

Instance Attribute Details

#cached_elementsObject

Element names we know



32
33
34
# File 'lib/tconsole/config.rb', line 32

def cached_elements
  @cached_elements
end

#cached_suite_countsObject

Counts of tests in suites



29
30
31
# File 'lib/tconsole/config.rb', line 29

def cached_suite_counts
  @cached_suite_counts
end

#fail_fastObject

Whether or not our test runs should stop when the first test fails. Defaults to false.



23
24
25
# File 'lib/tconsole/config.rb', line 23

def fail_fast
  @fail_fast
end

#file_setsObject

Defines the file set commands that are available



26
27
28
# File 'lib/tconsole/config.rb', line 26

def file_sets
  @file_sets
end

#include_pathsObject

Paths to add to the ruby include path. Defaults to ./test, ./lib



16
17
18
# File 'lib/tconsole/config.rb', line 16

def include_paths
  @include_paths
end

#onceObject

Only runs the command passed on the command line, and then exits



38
39
40
# File 'lib/tconsole/config.rb', line 38

def once
  @once
end

#preload_pathsObject

Paths we want to preload. Defaults to nil.



19
20
21
# File 'lib/tconsole/config.rb', line 19

def preload_paths
  @preload_paths
end

#run_commandObject

First command to run when tconsole loads



35
36
37
# File 'lib/tconsole/config.rb', line 35

def run_command
  @run_command
end

#test_dirObject

Test directory for the app we’re testing. Defaults to ./test.



12
13
14
# File 'lib/tconsole/config.rb', line 12

def test_dir
  @test_dir
end

#traceObject

Lets us know if we should include trace output. Defaults to false.



8
9
10
# File 'lib/tconsole/config.rb', line 8

def trace
  @trace
end

#trace_executionObject

Lets us know if we should include trace output



4
5
6
# File 'lib/tconsole/config.rb', line 4

def trace_execution
  @trace_execution
end

Class Method Details

.clear_loaded_configsObject



154
155
156
# File 'lib/tconsole/config.rb', line 154

def self.clear_loaded_configs
  @loaded_configs = nil
end

.configure(argv = []) ⇒ Object

Returns an appropriate tconsole config based on the environment



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/tconsole/config.rb', line 159

def self.configure(argv = [])
  config = Config.new(argv)

  if is_rails?
    config.preload_paths = ["./config/application"]
    config.include_paths = ["./test"]
    config.file_sets = {
      "all" => ["#{config.test_dir}/unit/**/*_test.rb", "#{config.test_dir}/functional/**/*_test.rb",
        "#{config.test_dir}/integration/**/*_test.rb"],
      "units" => ["#{config.test_dir}/unit/**/*_test.rb"],
      "unit" => ["#{config.test_dir}/unit/**/*_test.rb"],
      "functionals" => ["#{config.test_dir}/functional/**/*_test.rb"],
      "functional" => ["#{config.test_dir}/functional/**/*_test.rb"],
      "integration" => ["#{config.test_dir}/integration/**/*_test.rb"]
    }

    config.before_load do
      ENV["RAILS_ENV"] ||= "test"
    end

    config.after_load do
      ::Rails.application
      ::Rails::Engine.class_eval do
        def eager_load!
          # turn off eager_loading
        end
      end
    end

    config.before_test_run do
      if defined? ::ActiveRecord
        ::ActiveRecord::Base.clear_active_connections!
        ::ActiveRecord::Base.establish_connection
      end
    end
  end

  @loaded_configs ||= []
  @loaded_configs.each do |block|
    block.call(config)
  end

  config
end

.is_rails?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/tconsole/config.rb', line 204

def self.is_rails?
  @rails ||= !!File.exist?("./config/application.rb")
end

.load_config(path) ⇒ Object

Loads up a config file



141
142
143
144
145
# File 'lib/tconsole/config.rb', line 141

def self.load_config(path)
  if File.exist?(path)
    load path
  end
end

.run(&block) ⇒ Object

Saves a configuration block that we can apply to the configuration once it’s loaded



149
150
151
152
# File 'lib/tconsole/config.rb', line 149

def self.run(&block)
  @loaded_configs ||= []
  @loaded_configs << block
end

Instance Method Details

#after_load(&block) ⇒ Object

Code to run after loading the environment



102
103
104
# File 'lib/tconsole/config.rb', line 102

def after_load(&block)
  @after_load = block
end

#after_load!Object

Calls the after load callback



107
108
109
# File 'lib/tconsole/config.rb', line 107

def after_load!
  @after_load.call unless @after_load.nil?
end

#before_load(&block) ⇒ Object

Code to run before loading the environment



92
93
94
# File 'lib/tconsole/config.rb', line 92

def before_load(&block)
  @before_load = block
end

#before_load!Object

Calls the before load callback



97
98
99
# File 'lib/tconsole/config.rb', line 97

def before_load!
  @before_load.call unless @before_load.nil?
end

#before_test_run(&block) ⇒ Object

Calls before each test execution



112
113
114
# File 'lib/tconsole/config.rb', line 112

def before_test_run(&block)
  @before_test_run = block
end

#before_test_run!Object



116
117
118
# File 'lib/tconsole/config.rb', line 116

def before_test_run!
  @before_test_run.call unless @before_test_run.nil?
end

#cache_test_ids(result) ⇒ Object



120
121
122
123
# File 'lib/tconsole/config.rb', line 120

def cache_test_ids(result)
  self.cached_suite_counts = result.suite_counts
  self.cached_elements = result.elements
end

#fail_fast?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/tconsole/config.rb', line 87

def fail_fast?
  self.fail_fast
end

#load_args(argv) ⇒ Object

Public: Loads any passed command line arguments into the config.

argv - The array of command line arguments we’re loading



76
77
78
79
80
81
# File 'lib/tconsole/config.rb', line 76

def load_args(argv)
  args = argv.clone

  option_parser.parse!(args)
  self.run_command = args.join(" ")
end

#option_parserObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tconsole/config.rb', line 61

def option_parser
  @option_parser ||= OptionParser.new do |opts|
    opts.on("-t", "--trace", "Enable verbose output.") do
      self.trace_execution = true
    end

    opts.on("-o", "--once", "Run whatever command is passed and then exit.") do
      self.once = true
    end
  end
end

#trace?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/tconsole/config.rb', line 83

def trace?
  self.trace_execution
end

#validation_errorsObject

Returns true if this config is valid or false otherwise



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/tconsole/config.rb', line 126

def validation_errors
  errors = []

  unless Dir.exists?(test_dir)
    errors << "Couldn't find test directory `#{test_dir}`. Exiting."
  end

  unless file_sets.is_a?(Hash) && !file_sets["all"].nil?
    errors << "No `all` file set is defined in your configuration. Exiting."
  end

  errors
end