travis-config 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db1b438a15fc56e6225dfb5483d279d42dedf2a4
4
- data.tar.gz: d67b6b5f139cc394e1a7588dea711c20371b5752
3
+ metadata.gz: d28c9bf4b9fe2cad9b9320fc1c0f75a79f78bfc1
4
+ data.tar.gz: 8f80aa3063b9c09846c277bb8616349f2b273e84
5
5
  SHA512:
6
- metadata.gz: 85c63849ba32c848183596720d9b206b8a2d7c23be61a82dddabbee79025bd4b8ee0b5762b0b69706dfe4606b988868a6f7ff1af333c9af23b0723b7878a866a
7
- data.tar.gz: f7886416ae772b319e0d9c1c765b3e3ddae8a4ceb07131ea15546225491e73f55daf99edca0b20e56d6921428f767c3f875550c71c57a182058c491fd9a2e414
6
+ metadata.gz: a80aeac2d95ff766b3e5446ce0a23ad7ee49523e34a6b6b7b96602985c9fa70ee502d9ce5efea94386756975455a8b701682a0d4e52ba96edc6e1e2f8e1af4e0
7
+ data.tar.gz: ebb04bd02c4348d37d9bb26b32a4af29630b430ac0815e91c29a46c8886a5db83b706892a68cd43373a21ffd4e666ad98814a414844560a2e5b29c64fe6a67e4
data/Gemfile CHANGED
@@ -1,3 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ group :test do
6
+ gem 'rspec', '~> 3.0'
7
+ gem 'mocha', '~> 1.1'
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ travis-config (0.1.0)
5
+ hashr (~> 0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ hashr (0.0.22)
12
+ metaclass (0.0.4)
13
+ mocha (1.1.0)
14
+ metaclass (~> 0.0.1)
15
+ rspec (3.1.0)
16
+ rspec-core (~> 3.1.0)
17
+ rspec-expectations (~> 3.1.0)
18
+ rspec-mocks (~> 3.1.0)
19
+ rspec-core (3.1.7)
20
+ rspec-support (~> 3.1.0)
21
+ rspec-expectations (3.1.2)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.1.0)
24
+ rspec-mocks (3.1.3)
25
+ rspec-support (~> 3.1.0)
26
+ rspec-support (3.1.2)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ mocha (~> 1.1)
33
+ rspec (~> 3.0)
34
+ travis-config!
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Travis Config
2
+
3
+ Shared way of loading and reading configuration in Travis CI.
4
+
5
+ Users can either set defaults and include logic directly to the class
6
+ `Travis::Config` or extend the class to, e.g. `Travis::Logs::Config`.
7
+
8
+ E.g.
9
+
10
+ ```ruby
11
+ require 'travis/config'
12
+
13
+ module Travis
14
+ class Config < Hashr
15
+ define host: 'travis-ci.org'
16
+
17
+ def foo
18
+ :foo
19
+ end
20
+ end
21
+ end
22
+ ```
23
+
24
+ Or:
25
+
26
+ ```ruby
27
+ require 'travis/config'
28
+
29
+ module Travis::Logs
30
+ class Config < Travis::Config
31
+ define host: 'logs.travis-ci.org'
32
+
33
+ def foo
34
+ :foo
35
+ end
36
+ end
37
+ end
38
+ ```
39
+
40
+ ### Config sources
41
+
42
+ Serveral config sources are supported:
43
+
44
+ * Files: All files `config/travis.yml` and `config/travis/*.yml` are read and
45
+ merged (alpabetically sorted).
46
+ * Env: `ENV['travis_config']` is parsed as YAML and merged, if present.
47
+ * Heroku: The env vars `DATABASE_URL` or `SHARED_DATABASE_URL`, and `DB_POOL`
48
+ or `DATABASE_POOL_SIZE` are merged into the database config.
49
+ * Docker: The env vars `POSTGRESQL_PORT`, `RABBITMQ_PORT`, and `REDIS_PORT` are
50
+ interpreted as resource URLs, and merged into the database config.
51
+
52
+ Configuration from all sources is merged before it is passed to the `Hashr`
53
+ instance. All merging is deep merging of Hashes on any level.
54
+
55
+ ### Doing a Rubygem release
56
+
57
+ Any tool works. The current releases were done with
58
+ [`gem-release`](https://github.com/svenfuchs/gem-release) which allows creating
59
+ a Git tag, pushing it to GitHub, building the gem and pushing it to Rubygems in
60
+ one go:
61
+
62
+ ```bash
63
+ $ gem install gem-release
64
+ $ gem release --tag
65
+ ```
@@ -9,16 +9,12 @@ module Travis
9
9
 
10
10
  def load
11
11
  filenames.inject({}) do |config, filename|
12
- deep_merge(config, load_file(filename)[env] || {})
12
+ deep_merge(config, load_file(filename)[Config.env] || {})
13
13
  end
14
14
  end
15
15
 
16
16
  private
17
17
 
18
- def env
19
- ENV['ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
20
- end
21
-
22
18
  def load_file(filename)
23
19
  YAML.load_file(filename) || {} rescue {}
24
20
  end
@@ -6,20 +6,31 @@ module Travis
6
6
  class Heroku
7
7
  include Helpers
8
8
 
9
+ DATABASE_URL = %r((?:.+?)://(?<username>.+):(?<password>.+)@(?<host>[^:]+):?(?<port>.*)/(?<database>.+))
10
+
9
11
  def load
10
- { database: parse_database_url(database_url) || {} }
12
+ { database: database }
11
13
  end
12
14
 
13
15
  private
14
16
 
15
- def database_url
16
- ENV.values_at('DATABASE_URL', 'SHARED_DATABASE_URL').first
17
+ def database
18
+ config = parse_database_url(database_url)
19
+ config = config.merge(pool: pool_size.to_i) if pool_size
20
+ config
17
21
  end
18
22
 
19
23
  def parse_database_url(url)
20
- pattern = %r((?:.+?)://(?<username>.+):(?<password>.+)@(?<host>[^:]+):?(?<port>.*)/(?<database>.+))
21
- matches = pattern.match(url.to_s)
22
- compact(Hash[matches.names.zip(matches.captures)]) if matches
24
+ matches = DATABASE_URL.match(url.to_s)
25
+ matches ? compact(Hash[matches.names.zip(matches.captures)]) : {}
26
+ end
27
+
28
+ def pool_size
29
+ ENV.values_at('DB_POOL', 'DATABASE_POOL_SIZE').first
30
+ end
31
+
32
+ def database_url
33
+ ENV.values_at('DATABASE_URL', 'SHARED_DATABASE_URL').first
23
34
  end
24
35
  end
25
36
  end
@@ -1,5 +1,3 @@
1
- module Travis
2
- class Config
3
- VERSION = '0.1.0'
4
- end
1
+ module TravisConfig
2
+ VERSION = '0.1.1'
5
3
  end
data/lib/travis/config.rb CHANGED
@@ -24,6 +24,10 @@ module Travis
24
24
  class << self
25
25
  include Helpers
26
26
 
27
+ def env
28
+ @env ||= ENV['ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
29
+ end
30
+
27
31
  def load(*loaders)
28
32
  loaders = [:files, :env, :heroku, :docker] if loaders.empty?
29
33
 
@@ -36,5 +40,9 @@ module Travis
36
40
  new(data)
37
41
  end
38
42
  end
43
+
44
+ def env
45
+ self.class.env
46
+ end
39
47
  end
40
48
  end
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,13 @@ ENV['ENV'] = 'test'
3
3
  require 'mocha'
4
4
  require 'travis/config'
5
5
 
6
+ module Travis::Test
7
+ class Config < Travis::Config
8
+ define amqp: { username: 'guest', password: 'guest', host: 'localhost', prefetch: 1 }
9
+ define database: { adapter: 'postgresql', database: 'test', encoding: 'unicode' }
10
+ end
11
+ end
12
+
6
13
  RSpec.configure do |config|
7
14
  config.mock_with :mocha
8
15
  end
@@ -0,0 +1,37 @@
1
+ describe Travis::Config::Docker do
2
+ let(:config) { Travis::Test::Config.load(:docker) }
3
+ let(:vars) { %w(POSTGRESQL_PORT RABBITMQ_PORT REDIS_PORT) }
4
+ after { vars.each { |key| ENV.delete(key) } }
5
+
6
+ describe 'loads POSTGRESQL_PORT to config.database' do
7
+ before { ENV['POSTGRESQL_PORT'] = 'tcp://172.17.0.11:5432' }
8
+
9
+ it 'loads host and port from the env var' do
10
+ expect(config.database.values_at(:host, :port)).to eq(['172.17.0.11', '5432'])
11
+ end
12
+
13
+ it 'keeps adapter, database, encoding from the regular config' do
14
+ expect(config.database.values_at(:adapter, :database, :encoding)).to eq(['postgresql', 'test', 'unicode'])
15
+ end
16
+ end
17
+
18
+ describe 'loads RABBITMQ_PORT to config.amqp' do
19
+ before { ENV['RABBITMQ_PORT'] = 'tcp://172.17.0.11:5672' }
20
+
21
+ it 'loads host and port from the env var' do
22
+ expect(config.amqp.values_at(:host, :port)).to eq(['172.17.0.11', '5672'])
23
+ end
24
+
25
+ it 'keeps username, password, prefetch from the regular config' do
26
+ expect(config.amqp.values_at(:username, :password, :prefetch)).to eq(['guest', 'guest', 1])
27
+ end
28
+ end
29
+
30
+ describe 'loads REDIS_PORT' do
31
+ before { ENV['REDIS_PORT'] = 'tcp://172.17.0.7:6379' }
32
+
33
+ it 'loads the port to redis.url' do
34
+ expect(config.redis).to eq({ url: 'tcp://172.17.0.7:6379' })
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ describe Travis::Config::Files do
2
+ let(:config) { Travis::Test::Config.load(:files) }
3
+
4
+ describe 'reads custom config files' do
5
+ before :each do
6
+ Dir.stubs(:[]).returns ['config/travis.yml', 'config/travis/foo.yml', 'config/travis/bar.yml']
7
+ YAML.stubs(:load_file).with('config/travis.yml').returns('test' => { 'travis' => 'travis', 'shared' => 'travis' })
8
+ YAML.stubs(:load_file).with('config/travis/foo.yml').returns('test' => { 'foo' => 'foo' })
9
+ YAML.stubs(:load_file).with('config/travis/bar.yml').returns('test' => { 'bar' => 'bar', 'shared' => 'bar' })
10
+ end
11
+
12
+ it 'still reads the default config file' do
13
+ expect(config.travis).to eq('travis')
14
+ end
15
+
16
+ it 'merges custom files' do
17
+ expect(config.foo).to eq('foo')
18
+ expect(config.bar).to eq('bar')
19
+ end
20
+
21
+ it 'overwrites previously set values with values loaded later' do
22
+ expect(config.shared).to eq('bar')
23
+ end
24
+ end
25
+
26
+
27
+ it 'deep symbolizes arrays, too' do
28
+ config = Travis::Config.new('queues' => [{ 'slug' => 'rails/rails', 'queue' => 'rails' }])
29
+ expect(config.queues.first.values_at(:slug, :queue)).to eq(['rails/rails', 'rails'])
30
+ end
31
+ end
@@ -0,0 +1,46 @@
1
+ describe Travis::Config::Heroku do
2
+ let(:config) { Travis::Test::Config.load(:heroku) }
3
+ let(:vars) { %w(DATABASE_URL POOL_SIZE) }
4
+ after { vars.each { |key| ENV.delete(key) } }
5
+ before { ENV['DATABASE_URL'] = 'postgres://username:password@hostname:port/database' }
6
+
7
+ it 'loads a DATABASE_URL with a port' do
8
+ expect(config.database.to_hash).to eq(
9
+ adapter: 'postgresql',
10
+ host: 'hostname',
11
+ port: 'port',
12
+ database: 'database',
13
+ username: 'username',
14
+ password: 'password',
15
+ encoding: 'unicode'
16
+ )
17
+ end
18
+
19
+ it 'loads a DATABASE_URL without a port' do
20
+ ENV['DATABASE_URL'] = 'postgres://username:password@hostname/database'
21
+
22
+ expect(config.database.to_hash).to eq(
23
+ adapter: 'postgresql',
24
+ host: 'hostname',
25
+ database: 'database',
26
+ username: 'username',
27
+ password: 'password',
28
+ encoding: 'unicode'
29
+ )
30
+ end
31
+
32
+ it 'loads DB_POOL' do
33
+ ENV['DB_POOL'] = '25'
34
+
35
+ expect(config.database.to_hash).to eq(
36
+ adapter: 'postgresql',
37
+ host: 'hostname',
38
+ port: 'port',
39
+ database: 'database',
40
+ username: 'username',
41
+ password: 'password',
42
+ encoding: 'unicode',
43
+ pool: 25
44
+ )
45
+ end
46
+ end
@@ -1,12 +1,3 @@
1
- require 'spec_helper'
2
-
3
- module Travis::Test
4
- class Config < Travis::Config
5
- define amqp: { username: 'guest', password: 'guest', host: 'localhost', prefetch: 1 }
6
- define database: { adapter: 'postgresql', database: 'test', encoding: 'unicode' }
7
- end
8
- end
9
-
10
1
  describe Travis::Config do
11
2
  let(:config) { Travis::Test::Config.load(:files, :env, :heroku, :docker) }
12
3
 
@@ -28,52 +19,15 @@ describe Travis::Config do
28
19
  config.foo = { :bar => { :baz => 'baz' } }
29
20
  expect(config.foo.bar).to be_kind_of(Hashr)
30
21
  end
31
- end
32
-
33
- describe 'using DATABASE_URL for database configuration if present' do
34
- after :each do
35
- ENV.delete('DATABASE_URL')
36
- end
37
-
38
- it 'works when given a url with a port' do
39
- ENV['DATABASE_URL'] = 'postgres://username:password@hostname:port/database'
40
-
41
- expect(config.database.to_hash).to eq(
42
- :adapter => 'postgresql',
43
- :host => 'hostname',
44
- :port => 'port',
45
- :database => 'database',
46
- :username => 'username',
47
- :password => 'password',
48
- :encoding => 'unicode'
49
- )
50
- end
51
-
52
- it 'works when given a url without a port' do
53
- ENV['DATABASE_URL'] = 'postgres://username:password@hostname/database'
54
-
55
- expect(config.database.to_hash).to eq(
56
- :adapter => 'postgresql',
57
- :host => 'hostname',
58
- :database => 'database',
59
- :username => 'username',
60
- :password => 'password',
61
- :encoding => 'unicode'
62
- )
63
- end
64
- end
65
-
66
- describe 'the example config file' do
67
- let(:data) { {} }
68
22
 
69
23
  it 'can access all keys recursively' do
70
- nested_access = lambda do |config, data|
71
- data.keys.each do |key|
72
- expect(-> { config.send(key) }).to_not raise_error
73
- nested_access.call(config.send(key), data[key]) if data[key].is_a?(Hash)
24
+ access = proc do |config|
25
+ config.keys.each do |key|
26
+ expect(proc { config.send(key) }).to_not raise_error
27
+ access.call(config.send(key)) if config[key].is_a?(Hash)
74
28
  end
75
29
  end
76
- nested_access.call(config, data)
30
+ access.call(config)
77
31
  end
78
32
  end
79
33
 
@@ -99,46 +53,6 @@ describe Travis::Config do
99
53
  end
100
54
  end
101
55
 
102
- describe 'loads docker-style env vars' do
103
- after :each do
104
- %w(POSTGRESQL_PORT RABBITMQ_PORT REDIS_PORT).each do |key|
105
- ENV.delete(key)
106
- end
107
- end
108
-
109
- describe 'loads POSTGRESQL_PORT to config.database' do
110
- before :each do
111
- ENV['POSTGRESQL_PORT'] = 'tcp://172.17.0.11:5432'
112
- end
113
-
114
- it 'loads host and port from the env var' do
115
- expect(config.database.values_at(:host, :port)).to eq(['172.17.0.11', '5432'])
116
- end
117
-
118
- it 'keeps adapter, database, encoding from the regular config' do
119
- expect(config.database.values_at(:adapter, :database, :encoding)).to eq(['postgresql', 'test', 'unicode'])
120
- end
121
- end
122
-
123
- describe 'loads RABBITMQ_PORT to config.amqp' do
124
- before :each do
125
- ENV['RABBITMQ_PORT'] = 'tcp://172.17.0.11:5672'
126
- end
127
-
128
- it 'loads host and port from the env var' do
129
- expect(config.amqp.values_at(:host, :port)).to eq(['172.17.0.11', '5672'])
130
- end
131
-
132
- it 'keeps username, password, prefetch from the regular config' do
133
- expect(config.amqp.values_at(:username, :password, :prefetch)).to eq(['guest', 'guest', 1])
134
- end
135
- end
136
-
137
- it 'loads REDIS_PORT' do
138
- ENV['REDIS_PORT'] = 'tcp://172.17.0.7:6379'
139
- expect(config.redis).to eq({ url: 'tcp://172.17.0.7:6379' })
140
- end
141
- end
142
56
 
143
57
  it 'deep symbolizes arrays, too' do
144
58
  config = Travis::Config.new('queues' => [{ 'slug' => 'rails/rails', 'queue' => 'rails' }])
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ $:.unshift File.expand_path('../lib', __FILE__)
4
+ require 'travis/config/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "travis-config"
8
+ s.version = TravisConfig::VERSION
9
+ s.authors = ["Travis CI"]
10
+ s.email = "contact@travis-ci.org"
11
+ s.homepage = "https://github.com/travis-ci/travis-core"
12
+ s.summary = "Travis CI config"
13
+ s.description = "#{s.summary}."
14
+ s.license = "MIT"
15
+
16
+ s.files = Dir['{lib/**/*,spec/**/*,[A-Z]*}']
17
+ s.platform = Gem::Platform::RUBY
18
+ s.require_path = 'lib'
19
+ s.rubyforge_project = '[none]'
20
+
21
+ s.add_dependency 'hashr', '~> 0.0'
22
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: travis-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis CI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-19 00:00:00.000000000 Z
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashr
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.0'
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '3.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '3.0'
41
27
  description: Travis CI config.
42
28
  email: contact@travis-ci.org
43
29
  executables: []
@@ -45,7 +31,9 @@ extensions: []
45
31
  extra_rdoc_files: []
46
32
  files:
47
33
  - Gemfile
34
+ - Gemfile.lock
48
35
  - LICENSE
36
+ - README.md
49
37
  - lib/travis/config.rb
50
38
  - lib/travis/config/docker.rb
51
39
  - lib/travis/config/env.rb
@@ -54,7 +42,11 @@ files:
54
42
  - lib/travis/config/heroku.rb
55
43
  - lib/travis/config/version.rb
56
44
  - spec/spec_helper.rb
45
+ - spec/travis/config/docker_spec.rb
46
+ - spec/travis/config/files_spec.rb
47
+ - spec/travis/config/heroku_spec.rb
57
48
  - spec/travis/config_spec.rb
49
+ - travis-config.gemspec
58
50
  homepage: https://github.com/travis-ci/travis-core
59
51
  licenses:
60
52
  - MIT
@@ -75,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
67
  version: '0'
76
68
  requirements: []
77
69
  rubyforge_project: "[none]"
78
- rubygems_version: 2.2.2
70
+ rubygems_version: 2.4.5
79
71
  signing_key:
80
72
  specification_version: 4
81
73
  summary: Travis CI config