Class: DuckTest::AutoloadConfig

Inherits:
Object
  • Object
show all
Includes:
LoggerHelper
Defined in:
lib/duck_test/autoload_config.rb

Overview

Helper methods for configuring autoload paths during startup. One of the requirements essential to running tests in the console is making all of the module and class files visible to the console environment. Typically, this is accomplished via config/application.rb by adding to the autoload_paths like the following:

config.autoload_paths += %W(#{Rails.root}/test)

During startup DuckTest will attempt to take this step for you prior to configuration using a before_configuration block inside the Railtie class for the gem. You can guide this behavior in a couple of ways using a config file in two locations.

  • In the home directory of your file system: ~/.ducktestrc

  • In the root directory of your Rails application: Rails.root/.ducktest

Notice the slight difference in the file names. One with rc and one without. The default configuration is to load each directory for each supported testing framework if the directory exists. Currently, testunit and rspec are the two testing frameworks. The startup process goes something like this:

  1. load configuration settings from disk

  2. determine the current Rails environment: test, development, production.

  3. loop thru each directory from the config. if the directory is configured to be added to the autoload_path and the directory actually exists, then, it is added, otherwise, it is ignored.

The following is a sample config file.

---
test:          # represents the Rails environment
  test: true   # represents an app directory directly off of the app root
  spec: true

development:
  test: true
  spec: false

The default configuration is to add test and spec directories to the autoload_path. You can easily prevent any of them by setting the boolean value to false. You can add additional directories by adding the directory name and true.

Load order: The default config for test and development is to add the test and spec directories if they exist. The config file ~/.ducktestrc in the home directory is loaded and merged with the default config and will override it's values. Next, the config file .ducktest in the app directory is loaded and merged with the default config and will override it's values. So, the rule is simple.

  • home directory overrides default

  • app directory overrides both

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from LoggerHelper

#ducklog

Constructor Details

- (AutoloadConfig) initialize

Do I really have to say it?



51
52
53
54
55
# File 'lib/duck_test/autoload_config.rb', line 51

def initialize
  super
  self.paths = []
  self.load_config
end

Instance Attribute Details

- (Object) paths

Returns the value of attribute paths



47
48
49
# File 'lib/duck_test/autoload_config.rb', line 47

def paths
  @paths
end

Instance Method Details

- (Object) load_config

Loads config settings for autoload paths



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/duck_test/autoload_config.rb', line 59

def load_config
  config = {test: {test: true, spec: true}, development: {test: false, spec: false}}

  if File.exists?("~/.ducktestrc")
    config = merge(config, YAML.load_file("~/.ducktestrc"))
  end

  if File.exists?("#{Rails.root}/.ducktest")
    config = merge(config, YAML.load_file("#{Rails.root}/.ducktest"))
  end

  config.each do |item|
    if Rails.env.eql?(item.first.to_s)
      item.last.each do |path|
        if path.last
          file_spec = "#{Rails.root}/#{path.first}"
          if File.exist?(file_spec)
            self.paths.push(file_spec)
            ducklog.system "Adding path to autoload_paths: #{file_spec}"
          end
        end
      end
      break
    end
  end

end

- (Object) merge(config, buffer)

Loads config settings for autoload paths



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/duck_test/autoload_config.rb', line 89

def merge(config, buffer)
  buffer = buffer.symbolize_keys
  config.each do |item|
    if item.last.kind_of?(Hash)
      if buffer[item.first]
        item.last.merge!(buffer[item.first].symbolize_keys)
      end
    end
  end
  return config
end