test_config 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +19 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +16 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +123 -0
- data/Rakefile +19 -0
- data/config/custom_source_dir/custom_default.yml +2 -0
- data/config/custom_source_dir/default.yml +2 -0
- data/config/custom_source_dir/env.yml +2 -0
- data/config/custom_source_dir/env2.yml +2 -0
- data/config/environments/custom_default.yml +2 -0
- data/config/environments/default.yml +2 -0
- data/config/environments/empty.yml +0 -0
- data/config/environments/env.yml +2 -0
- data/config/environments/env2.yml +2 -0
- data/config/environments/has_erb.yml +3 -0
- data/config/environments/load_me.yml +2 -0
- data/features/support/env.rb +5 -0
- data/lib/test_config.rb +62 -0
- data/lib/test_config/config_data.rb +23 -0
- data/lib/test_config/config_file.rb +26 -0
- data/lib/test_config/hash_methods.rb +27 -0
- data/lib/test_config/version.rb +3 -0
- data/spec/config_data_spec.rb +66 -0
- data/spec/config_file_spec.rb +52 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/test_config_spec.rb +178 -0
- data/test_config.gemspec +26 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2d855d62c606102b542a274bb8e7f058906ab66f
|
4
|
+
data.tar.gz: b1b0681b7bbea475b2b321f3c202b7257320b4c8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: afb60e8ecd312b451536ca108c98b4b5713c4e4003757c473dc6df4f43eaefa036e98a7041165d532f3c579971fef8f8c7b890dfe5def0dd4fbb466a58fbc601
|
7
|
+
data.tar.gz: 41aa9c4f1011b951293aac642685f8ea28caaae2374ef06c96e80318464b61dd58fa4afe0b4574e0630902f30761fa63f09b1b558b126fc1fd1f8fa9e37b4e9a
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
TestConfig
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Scott Clark
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# TestConfig
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/saclark/test_config.svg?branch=master)](https://travis-ci.org/saclark/test_config) [![Coverage Status](https://coveralls.io/repos/saclark/test_config/badge.png)](https://coveralls.io/r/saclark/test_config)
|
4
|
+
|
5
|
+
TestConfig provides flexible cross-environment configuration management for your test suites by allowing you to store configuration data in YAML files and accessing that data through methods on the TestConfig module matching the desired key.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'test_config'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install test_config
|
22
|
+
|
23
|
+
Then be sure to `require 'test_config'` in your project.
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
By default, TestConfig looks in a `config/environments` directory to load a `default.yml` file containing configuration data. It also loads and _merges in_ any additional configuration data from files listed in a `TEST_ENV` environment variable, if present.
|
27
|
+
|
28
|
+
Each of these names and locations are options that can be [configured](#configuration-options).
|
29
|
+
|
30
|
+
### Example
|
31
|
+
Let's say you are testing a web application and you want to keep certain test data in configuration files that you can access from your tests, thus allowing your tests to run more seamlessly in different environments. If we are using TestConfig's default settings, we would first create a file named `default.yml` with the following data and place it in `config/environments`:
|
32
|
+
|
33
|
+
```yaml
|
34
|
+
base_url: http://www.dev.example.com
|
35
|
+
username: testuser
|
36
|
+
password: Pa55w0rd
|
37
|
+
```
|
38
|
+
|
39
|
+
At this point, you can access this data in your tests by calling methods on the TestConfig module that match your keys*.
|
40
|
+
|
41
|
+
> \*__Note__: For this reason, you should name your keys according to the same syntactic rules as those for method names in Ruby. (although if you _really_ need to, you can use the `#send` method to get around this: `TestConfig.send('key-name-with-dashes')`.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
TestConfig.base_url # => "http://www.dev.example.com"
|
45
|
+
TestConfig.username # => "testuser"
|
46
|
+
TestConfig.password # => "Pa55w0rd"
|
47
|
+
```
|
48
|
+
|
49
|
+
If you aren't sure a key exists, you can check if it exists or even provide default values like so:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
TestConfig.has_key?('nonexistent_key') # => false
|
53
|
+
TestConfig.nonexistent_key(:or => "my default value") # => "my default value"
|
54
|
+
```
|
55
|
+
|
56
|
+
Now, in order to run tests on your local machine with an updated `base_url`, you can create another file (let's call it `local.yml`) in your `config/environments` directory with the following:
|
57
|
+
|
58
|
+
```yaml
|
59
|
+
base_url: http://localhost:1234
|
60
|
+
```
|
61
|
+
|
62
|
+
You'd then run tests against your local machine with a `TEST_ENV` environment variable set to `local.yml`. The data from that file will get merged in with the data from `default.yml`, giving you the same configuration data as before, but with the `base_url` updated for your local machine:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
TestConfig.base_url # => "http://localhost:1234"
|
66
|
+
TestConfig.username # => "testuser"
|
67
|
+
TestConfig.password # => "Pa55w0rd"
|
68
|
+
```
|
69
|
+
|
70
|
+
This works particularly well with Cucumber since you can set this environment variable in your `cucumber.yml` profiles for each environment:
|
71
|
+
|
72
|
+
```yaml
|
73
|
+
default: --format pretty
|
74
|
+
local: TEST_ENV=local.yml --format pretty
|
75
|
+
joes_machine: TEST_ENV=joes_machine.yml --format pretty
|
76
|
+
ci: TEST_ENV=ci.yml --format pretty
|
77
|
+
```
|
78
|
+
|
79
|
+
## Configuration Options
|
80
|
+
TestConfig is itself, configurable by calling the `configure!` method with any of the optional parameters found in the example below:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
TestConfig.configure!(
|
84
|
+
:source_directory => 'my/custom/location',
|
85
|
+
:base_config => 'my_base_config.yml',
|
86
|
+
:env_variable => 'MY_TEST_ENV_VAR'
|
87
|
+
)
|
88
|
+
```
|
89
|
+
|
90
|
+
Alternatively, you can set these options by placing a `test_config_options.yml` file in the root of your project. It might look like this:
|
91
|
+
|
92
|
+
```yaml
|
93
|
+
source_directory: my/custom/location
|
94
|
+
base_config: my_base_config.yml
|
95
|
+
env_variable: MY_TEST_ENV_VAR
|
96
|
+
```
|
97
|
+
|
98
|
+
Here is a breakdown of each option:
|
99
|
+
|
100
|
+
#### `:source_directory`
|
101
|
+
- Tells TestConfig where to find your configuration files (starting from the root of your project).
|
102
|
+
- Set to `config/environments` by default.
|
103
|
+
|
104
|
+
#### `:base_config`
|
105
|
+
- Tells TestConfig which file to use for "base configuration" (i.e. configuration data common to all test environments). This should be the filename or filepath to a
|
106
|
+
- Set to `default.yml` by default.
|
107
|
+
- Can be set to `nil` to prevent TestConfig from looking for a base configuration file.
|
108
|
+
|
109
|
+
#### `:env_variable`
|
110
|
+
- Tells TestConfig which environment variable (if present) to use for determining which additional configuration files to load. The environment variable should contain a comma separated list of filenames/filepaths from the `:source_directory` to be loaded and _merged in_ with previously loaded data (data from a previously loaded file whose key matches one from a freshly loaded file will be overwritten).
|
111
|
+
- Set to `TEST_ENV` by default.
|
112
|
+
- Can be set to `nil` to prevent TestConfig from looking for additional configuration files.
|
113
|
+
|
114
|
+
## TODO
|
115
|
+
- Add support for automatically loading files according to hostname
|
116
|
+
|
117
|
+
## Contributing
|
118
|
+
|
119
|
+
1. Fork it ( https://github.com/saclark/test_config/fork )
|
120
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
121
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
122
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
123
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'coveralls/rake/task'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'cucumber'
|
4
|
+
require 'cucumber/rake/task'
|
5
|
+
require 'bundler/gem_tasks'
|
6
|
+
|
7
|
+
# Run with `rake spec`
|
8
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
9
|
+
task.rspec_opts = ['--color', '--format', 'documentation']
|
10
|
+
end
|
11
|
+
|
12
|
+
# Run with `rake cucumber`
|
13
|
+
Cucumber::Rake::Task.new(:features)
|
14
|
+
|
15
|
+
# For Coveralls
|
16
|
+
Coveralls::RakeTask.new
|
17
|
+
task :test_with_coveralls => [:spec, :features, 'coveralls:push']
|
18
|
+
|
19
|
+
task :default => [:spec, :features]
|
File without changes
|
data/lib/test_config.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
require_relative 'test_config/version'
|
4
|
+
require_relative 'test_config/hash_methods'
|
5
|
+
require_relative 'test_config/config_file'
|
6
|
+
require_relative 'test_config/config_data'
|
7
|
+
|
8
|
+
module TestConfig
|
9
|
+
@_configuration = ConfigData.new
|
10
|
+
|
11
|
+
def self.data
|
12
|
+
@_configuration.data
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configure!(options = {})
|
16
|
+
opts = {
|
17
|
+
:base_config => 'default.yml',
|
18
|
+
:source_directory => 'config/environments',
|
19
|
+
:env_variable => 'TEST_ENV'
|
20
|
+
}.merge(options)
|
21
|
+
|
22
|
+
@_configuration.clear!
|
23
|
+
|
24
|
+
if opts[:base_config]
|
25
|
+
@_configuration.load!("#{opts[:source_directory]}/#{opts[:base_config]}")
|
26
|
+
end
|
27
|
+
|
28
|
+
if opts[:env_variable] && ENV[opts[:env_variable]]
|
29
|
+
ENV[opts[:env_variable]].split(/, */).each do |file|
|
30
|
+
@_configuration.load!("#{opts[:source_directory]}/#{file}")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.load!(file_path)
|
36
|
+
@_configuration.load!(file_path)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.read(file_path)
|
40
|
+
ConfigFile.new(file_path).data
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.has_key?(key)
|
44
|
+
data.has_key?(key)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.method_missing(name, default = nil, *args, &block)
|
48
|
+
if has_key?(name.to_sym)
|
49
|
+
return data[name.to_sym]
|
50
|
+
elsif default && default.is_a?(Hash) && default.has_key?(:or)
|
51
|
+
return default[:or]
|
52
|
+
else
|
53
|
+
fail(NoMethodError, "Unknown configuration root: #{name}", caller)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
if File.file?('test_config_options.yml')
|
58
|
+
configure!(read('test_config_options.yml'))
|
59
|
+
else
|
60
|
+
configure!
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module TestConfig
|
2
|
+
class ConfigData
|
3
|
+
attr_accessor :data
|
4
|
+
|
5
|
+
def initialize(data = {})
|
6
|
+
@data = HashMethods.deep_symbolize(data)
|
7
|
+
end
|
8
|
+
|
9
|
+
def clear!
|
10
|
+
@data = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def load!(*files)
|
14
|
+
@data = self.load(*files)
|
15
|
+
end
|
16
|
+
|
17
|
+
def load(*files)
|
18
|
+
files.inject(@data) do |data, file|
|
19
|
+
HashMethods.deep_merge(data, ConfigFile.new(file).data)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module TestConfig
|
2
|
+
class ConfigFile
|
3
|
+
attr_accessor :file, :data
|
4
|
+
|
5
|
+
def initialize(file)
|
6
|
+
@file = file
|
7
|
+
@data = HashMethods.deep_symbolize(self.to_h)
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
begin
|
12
|
+
File.read(@file)
|
13
|
+
rescue SystemCallError
|
14
|
+
raise "Could not locate configuration file: #{@file}."
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def evaluate
|
19
|
+
ERB.new(self.to_s).result
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_h
|
23
|
+
YAML.load(self.evaluate) || {}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module TestConfig
|
2
|
+
module HashMethods
|
3
|
+
def self.deep_symbolize(data)
|
4
|
+
if data.is_a?(Hash)
|
5
|
+
return data.inject({}) do |memo, (k, v)|
|
6
|
+
memo.tap { |m| m[k.to_sym] = deep_symbolize(v) }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
if data.is_a?(Array)
|
11
|
+
return data.inject([]) do |memo, v|
|
12
|
+
memo << deep_symbolize(v); memo
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
data
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.deep_merge(orig_data, new_data)
|
20
|
+
merger = proc do |key, v1, v2|
|
21
|
+
Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
|
22
|
+
end
|
23
|
+
|
24
|
+
orig_data.merge(new_data, &merger)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TestConfig::ConfigData do
|
4
|
+
before :each do
|
5
|
+
@config_data = TestConfig::ConfigData.new('existing_truthy' => 'key value')
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#initialize' do
|
9
|
+
it 'takes 0 or 1 parameters and returns a TestConfig::ConfigData object' do
|
10
|
+
TestConfig::ConfigData.new.should be_an_instance_of TestConfig::ConfigData
|
11
|
+
TestConfig::ConfigData.new({}).should be_an_instance_of TestConfig::ConfigData
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#data' do
|
16
|
+
it 'should return symbolized configuration data' do
|
17
|
+
@config_data.data.should == { :existing_truthy => 'key value' }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#clear!' do
|
22
|
+
it 'should clear all configuration data' do
|
23
|
+
@config_data.clear!
|
24
|
+
@config_data.data.should == {}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#load!' do
|
29
|
+
it 'should take a file and mutate @data by merging in the files contents' do
|
30
|
+
@config_data.load!('config/environments/load_me.yml')
|
31
|
+
@config_data.data.should == {
|
32
|
+
:existing_truthy => 'key value',
|
33
|
+
:config_environments_load_me => 'from config/environments/load_me.yml'
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should take a list of files and mutate @data by merging in the files' contents" do
|
38
|
+
@config_data.load!('config/environments/load_me.yml', 'config/environments/env.yml')
|
39
|
+
@config_data.data.should == {
|
40
|
+
:existing_truthy => 'key value',
|
41
|
+
:config_environments_load_me => 'from config/environments/load_me.yml',
|
42
|
+
:config_environments_env => 'from config/environments/env.yml',
|
43
|
+
:common_key => 'value from config/environments/env.yml'
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#load' do
|
49
|
+
it 'should take a file and return a new hash containing @data merged with the file contents' do
|
50
|
+
@config_data.load('config/environments/load_me.yml').should == {
|
51
|
+
:existing_truthy => 'key value',
|
52
|
+
:config_environments_load_me => 'from config/environments/load_me.yml'
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should take a list of files and return a new hash containing @data merged with the files' contents" do
|
57
|
+
@config_data.load!('config/environments/load_me.yml', 'config/environments/env.yml')
|
58
|
+
@config_data.data.should == {
|
59
|
+
:existing_truthy => 'key value',
|
60
|
+
:config_environments_load_me => 'from config/environments/load_me.yml',
|
61
|
+
:config_environments_env => 'from config/environments/env.yml',
|
62
|
+
:common_key => 'value from config/environments/env.yml'
|
63
|
+
}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TestConfig::ConfigFile do
|
4
|
+
before :each do
|
5
|
+
@config_file = TestConfig::ConfigFile.new('config/environments/default.yml')
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#file' do
|
9
|
+
it 'should return the input string passed to the contructor' do
|
10
|
+
@config_file.file.should == 'config/environments/default.yml'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#data' do
|
15
|
+
it 'should return the file data as a hash with keys symbolized' do
|
16
|
+
@config_file.data.should == {
|
17
|
+
:config_environments_default => 'from config/environments/default.yml',
|
18
|
+
:common_key => 'value from config/environments/default.yml'
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#to_s' do
|
24
|
+
it 'should return the file contents as a string' do
|
25
|
+
@config_file.to_s.should == "config_environments_default: 'from config/environments/default.yml'\ncommon_key: 'value from config/environments/default.yml'\n"
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should raise an erorr if the file is not found' do
|
29
|
+
expect { TestConfig::ConfigFile.new('blah').to_s }.to raise_error(RuntimeError)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#evaluate' do
|
34
|
+
it 'should return the file contents as a string with erb evaluated' do
|
35
|
+
file_with_erb = TestConfig::ConfigFile.new('config/environments/has_erb.yml')
|
36
|
+
file_with_erb.evaluate.should include("value_from_erb: value from erb in config/environments/custom_default.yml")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#to_h' do
|
41
|
+
it 'should return an empty hash if the file is empty' do
|
42
|
+
TestConfig::ConfigFile.new('config/environments/empty.yml').to_h.should == {}
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should return a hash of file contents with keys not symbolized' do
|
46
|
+
@config_file.to_h.should == {
|
47
|
+
'config_environments_default' => 'from config/environments/default.yml',
|
48
|
+
'common_key' => 'value from config/environments/default.yml'
|
49
|
+
}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TestConfig do
|
4
|
+
before :each do
|
5
|
+
module TestConfig
|
6
|
+
@_configuration = ConfigData.new({
|
7
|
+
:existing_truthy => 'key value',
|
8
|
+
:existing_falsey => false,
|
9
|
+
:data_hash => {
|
10
|
+
:array_data => ['first item', 'second item']
|
11
|
+
}
|
12
|
+
})
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
after :each do
|
17
|
+
module TestConfig
|
18
|
+
@_configuration = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
ENV.delete('TEST_ENV')
|
22
|
+
ENV.delete('MY_TEST_ENV')
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#data' do
|
26
|
+
it 'should return a hash of all configuration data' do
|
27
|
+
TestConfig.data.should == {
|
28
|
+
:existing_truthy => 'key value',
|
29
|
+
:existing_falsey => false,
|
30
|
+
:data_hash => {
|
31
|
+
:array_data => ['first item', 'second item']
|
32
|
+
}
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#configure!' do
|
38
|
+
it 'should load files according to defaults when no options passed' do
|
39
|
+
ENV['TEST_ENV'] = 'env.yml,env2.yml'
|
40
|
+
TestConfig.configure!
|
41
|
+
|
42
|
+
TestConfig.data.should == {
|
43
|
+
:config_environments_default => 'from config/environments/default.yml',
|
44
|
+
:config_environments_env => 'from config/environments/env.yml',
|
45
|
+
:config_environments_env2 => 'from config/environments/env2.yml',
|
46
|
+
:common_key => 'value from config/environments/env2.yml'
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should load files according to custom options' do
|
51
|
+
ENV['MY_TEST_ENV'] = 'env.yml,env2.yml'
|
52
|
+
TestConfig.configure!(
|
53
|
+
:source_directory => 'config/custom_source_dir',
|
54
|
+
:base_config => 'custom_default.yml',
|
55
|
+
:env_variable => 'MY_TEST_ENV'
|
56
|
+
)
|
57
|
+
|
58
|
+
TestConfig.data.should == {
|
59
|
+
:config_custom_source_dir_custom_default => 'from config/custom_source_dir/custom_default.yml',
|
60
|
+
:config_custom_source_dir_env => 'from config/custom_source_dir/env.yml',
|
61
|
+
:config_custom_source_dir_env2 => 'from config/custom_source_dir/env2.yml',
|
62
|
+
:common_key => 'value from config/custom_source_dir/env2.yml'
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should not load anything if base_config and env_variable set to nil' do
|
67
|
+
TestConfig.configure!(
|
68
|
+
:base_config => nil,
|
69
|
+
:env_variable => nil
|
70
|
+
)
|
71
|
+
|
72
|
+
TestConfig.data.should == {}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '#load!' do
|
77
|
+
it "should merge a given file's data into @_configuration" do
|
78
|
+
TestConfig.load!('config/environments/load_me.yml')
|
79
|
+
TestConfig.data.should == {
|
80
|
+
:existing_truthy => 'key value',
|
81
|
+
:existing_falsey => false,
|
82
|
+
:data_hash => {
|
83
|
+
:array_data => ['first item', 'second item']
|
84
|
+
},
|
85
|
+
:config_environments_load_me => 'from config/environments/load_me.yml',
|
86
|
+
}
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#read' do
|
91
|
+
it 'should return a symblized hash of file data' do
|
92
|
+
TestConfig.read('config/environments/custom_default.yml').should == {
|
93
|
+
:config_environments_custom_default => 'from config/environments/custom_default.yml',
|
94
|
+
:common_key => 'value from config/environments/custom_default.yml'
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'should evaluate files containing erb' do
|
99
|
+
TestConfig.read('config/environments/has_erb.yml').should == {
|
100
|
+
:value_from_erb => 'value from erb in config/environments/custom_default.yml'
|
101
|
+
}
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should return an empty hash if the file is empty' do
|
105
|
+
TestConfig.read('config/environments/empty.yml').should == {}
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'should raise an error if the file does not exist' do
|
109
|
+
expect { TestConfig.read('this_does_not_exist.yml') }.to raise_error
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe '#has_key?' do
|
114
|
+
it 'should return true for existing keys' do
|
115
|
+
TestConfig.has_key?(:existing_truthy).should == true
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should return false for nonexistent keys' do
|
119
|
+
TestConfig.has_key?(:nonexistent_key).should == false
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe '#method_missing' do
|
124
|
+
context 'existing key with truthy value' do
|
125
|
+
it 'should return the key value when no default is given' do
|
126
|
+
TestConfig.existing_truthy.should == 'key value'
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'should return the key value when a default is given' do
|
130
|
+
TestConfig.existing_truthy(:or => 'default value').should == 'key value'
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'existing key with falsey value' do
|
135
|
+
it 'should return the key value when no default is given' do
|
136
|
+
TestConfig.existing_falsey.should == false
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'should return the key value when a default is given' do
|
140
|
+
TestConfig.existing_falsey(:or => 'default value').should == false
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'nonexistent key' do
|
145
|
+
it 'should return the default value when a truthy default value is given' do
|
146
|
+
TestConfig.nonexistent_key(:or => 'default value').should == 'default value'
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'should return the default value when a falsey default value is given' do
|
150
|
+
TestConfig.nonexistent_key(:or => false).should == false
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should return the default value assigned to the ':or' key" do
|
154
|
+
TestConfig.nonexistent_key(:blah => 'blah', :or => 'default value').should == 'default value'
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should throw a 'NoMethodError' when no default is given" do
|
158
|
+
expect { TestConfig.nonexistent_key }.to raise_error(NoMethodError)
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should throw a 'NoMethodError' when the first parameter is not a hash" do
|
162
|
+
expect { TestConfig.nonexistent_key('blah', :or => 'default value') }.to raise_error(NoMethodError)
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should throw a 'NoMethodError' when the first parameter is not a hash with the key ':or'" do
|
166
|
+
expect { TestConfig.nonexistent_key(:blah => 'blah') }.to raise_error(NoMethodError)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context 'nested complex data' do
|
171
|
+
it 'should be accessible' do
|
172
|
+
TestConfig.data_hash.should == {
|
173
|
+
:array_data => ['first item', 'second item']
|
174
|
+
}
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
data/test_config.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'test_config/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'test_config'
|
8
|
+
spec.version = TestConfig::VERSION
|
9
|
+
spec.authors = ['Scott Clark']
|
10
|
+
spec.email = ['sclarkdev@gmail.com']
|
11
|
+
spec.summary = 'Flexible cucumber test configuration.'
|
12
|
+
spec.description = 'A flexible means of configuration for cucumber tests.'
|
13
|
+
spec.homepage = 'https://github.com/saclark/test_config'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.1'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
24
|
+
spec.add_development_dependency 'cucumber', '~> 1.3'
|
25
|
+
spec.add_development_dependency 'coveralls', '~> 0.7'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: test_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scott Clark
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.14'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: cucumber
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.7'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.7'
|
83
|
+
description: A flexible means of configuration for cucumber tests.
|
84
|
+
email:
|
85
|
+
- sclarkdev@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".coveralls.yml"
|
91
|
+
- ".gitignore"
|
92
|
+
- ".ruby-gemset"
|
93
|
+
- ".ruby-version"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- config/custom_source_dir/custom_default.yml
|
100
|
+
- config/custom_source_dir/default.yml
|
101
|
+
- config/custom_source_dir/env.yml
|
102
|
+
- config/custom_source_dir/env2.yml
|
103
|
+
- config/environments/custom_default.yml
|
104
|
+
- config/environments/default.yml
|
105
|
+
- config/environments/empty.yml
|
106
|
+
- config/environments/env.yml
|
107
|
+
- config/environments/env2.yml
|
108
|
+
- config/environments/has_erb.yml
|
109
|
+
- config/environments/load_me.yml
|
110
|
+
- features/support/env.rb
|
111
|
+
- lib/test_config.rb
|
112
|
+
- lib/test_config/config_data.rb
|
113
|
+
- lib/test_config/config_file.rb
|
114
|
+
- lib/test_config/hash_methods.rb
|
115
|
+
- lib/test_config/version.rb
|
116
|
+
- spec/config_data_spec.rb
|
117
|
+
- spec/config_file_spec.rb
|
118
|
+
- spec/spec_helper.rb
|
119
|
+
- spec/test_config_spec.rb
|
120
|
+
- test_config.gemspec
|
121
|
+
homepage: https://github.com/saclark/test_config
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.2.2
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: Flexible cucumber test configuration.
|
145
|
+
test_files:
|
146
|
+
- features/support/env.rb
|
147
|
+
- spec/config_data_spec.rb
|
148
|
+
- spec/config_file_spec.rb
|
149
|
+
- spec/spec_helper.rb
|
150
|
+
- spec/test_config_spec.rb
|