dotenv 0.5.0 → 0.7.0

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.
data/.gitignore CHANGED
@@ -4,3 +4,4 @@
4
4
  .config
5
5
  .yardoc
6
6
  Gemfile.lock
7
+ tmp
data/Changelog.md CHANGED
@@ -1,17 +1,51 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.0 - April 15, 2013
4
+
5
+ * Remove deprectated autoloading. Upgrade to 0.6 first and fix any warnings.
6
+
7
+ * Add Dotenv.load! which raises Errno::ENOENT if the file does not exist
8
+
9
+ ## 0.6.0 - Mar 22, 2013
10
+
11
+ * Add dotenv-rails gem for autoloading in a Rails app
12
+
13
+ * Deprecated autoloading with plain dotenv gem
14
+
15
+ * Support for double quotes
16
+
17
+ A="some value"
18
+ B="with \"escaped\" quotes"
19
+ C="and newline\n expansion"
20
+
21
+ * Support for pow-style variables prefixed with export
22
+
23
+ export VARIABLE="some value"
24
+
25
+ [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.5.0...v0.6.0)
26
+
3
27
  ## 0.5.0 - Jan 25, 2013
4
28
 
5
29
  * Load immediately on require in Rails instead of waiting for initialization
30
+
6
31
  * Add YAML-style variables
7
- VARIABLE: some value
32
+
33
+ VARIABLE: some value
34
+
35
+ [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.4.0...v0.5.0)
8
36
 
9
37
  ## 0.4.0 - Nov 13, 2012
10
38
 
11
39
  * Add support for quoted options, e.g.:
12
- VARIABLE='some value'
40
+
41
+ VARIABLE='some value'
42
+
13
43
  * Fix rake deprecation warnings
14
44
 
45
+ [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.3.0...v0.4.0)
46
+
15
47
  ## 0.3.0 - Oct 25, 2012
16
48
 
17
49
  * Avoid overriding existing ENV variables so values set before loading the app are maintained.
50
+
51
+ [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.2.0...v0.3.0)
data/Gemfile CHANGED
@@ -1,2 +1,6 @@
1
1
  source 'https://rubygems.org'
2
- gemspec
2
+ gemspec :name => 'dotenv'
3
+
4
+ gem 'guard-rspec'
5
+ gem 'guard-bundler'
6
+ gem 'rb-fsevent'
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ guard 'bundler' do
2
+ watch('Gemfile')
3
+ end
4
+
5
+ guard 'rspec', :cli => '--color' do
6
+ watch(%r{^spec/.+_spec\.rb$})
7
+ watch(%r{^spec/spec_helper.rb$}) { "spec" }
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
9
+ end
data/README.md CHANGED
@@ -11,7 +11,7 @@ Read more about the [motivation for dotenv at opensoul.org](http://opensoul.org/
11
11
  Add this line to your application's Gemfile:
12
12
 
13
13
  ```ruby
14
- gem 'dotenv', :groups => [:development, :test]
14
+ gem 'dotenv-rails', :groups => [:development, :test]
15
15
  ```
16
16
 
17
17
  And then execute:
@@ -43,25 +43,26 @@ end
43
43
 
44
44
  ## Usage
45
45
 
46
- Add your application configuration to `.env`.
46
+ Add your application configuration to your `.env` file in the root of
47
+ your project:
47
48
 
48
49
  ```shell
49
- S3_BUCKET=dotenv
50
- SECRET_KEY=sssshhh!
50
+ S3_BUCKET=YOURS3BUCKET
51
+ SECRET_KEY=YOURSECRETKEYGOESHERE
51
52
  ```
52
53
 
53
54
  You can also create files per environment, such as `.env.test`:
54
55
 
55
56
  ```shell
56
- S3_BUCKET=test
57
- SECRET_KEY=test
57
+ S3_BUCKET=tests3bucket
58
+ SECRET_KEY=testsecretkey
58
59
  ```
59
60
 
60
61
  An alternate yaml-like syntax is supported:
61
62
 
62
63
  ```yaml
63
- S3_BUCKET: dotenv
64
- SECRET_KEY: 'sesame, open'
64
+ S3_BUCKET: yamlstyleforyours3bucket
65
+ SECRET_KEY: thisisalsoanokaysecret
65
66
  ```
66
67
 
67
68
  Whenever your application loads, these variables will be available in `ENV`:
data/Rakefile CHANGED
@@ -1,5 +1,24 @@
1
1
  #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
2
+
3
+ require 'bundler/gem_helper'
4
+
5
+ namespace 'dotenv' do
6
+ Bundler::GemHelper.install_tasks :name => 'dotenv'
7
+ end
8
+
9
+ namespace 'dotenv-rails' do
10
+ class DotenvRailsGemHelper < Bundler::GemHelper
11
+ def guard_already_tagged; end # noop
12
+ def tag_version; end # noop
13
+ end
14
+
15
+ DotenvRailsGemHelper.install_tasks :name => 'dotenv-rails'
16
+ end
17
+
18
+ task :build => ["dotenv:build", 'dotenv-rails:build']
19
+ task :install => ["dotenv:install", 'dotenv-rails:install']
20
+ task :release => ["dotenv:release", 'dotenv-rails:release']
21
+
3
22
  require 'rspec/core/rake_task'
4
23
 
5
24
  desc "Run all specs"
@@ -8,5 +27,4 @@ RSpec::Core::RakeTask.new(:spec) do |t|
8
27
  t.verbose = false
9
28
  end
10
29
 
11
-
12
- task :default => :spec
30
+ task :default => :spec
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/dotenv/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.version = Dotenv::VERSION
6
+ gem.authors = ["Brandon Keepers"]
7
+ gem.email = ["brandon@opensoul.org"]
8
+ gem.description = %q{Autoload dotenv in Rails.}
9
+ gem.summary = %q{Autoload dotenv in Rails.}
10
+ gem.homepage = "https://github.com/bkeepers/dotenv"
11
+
12
+ gem.files = ["lib/dotenv-rails.rb"]
13
+ gem.name = "dotenv-rails"
14
+ gem.require_paths = ["lib"]
15
+
16
+ gem.add_dependency 'dotenv', Dotenv::VERSION
17
+ end
data/dotenv.gemspec CHANGED
@@ -1,7 +1,8 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/dotenv/version', __FILE__)
2
3
 
3
4
  Gem::Specification.new do |gem|
4
- gem.version = '0.5.0'
5
+ gem.version = Dotenv::VERSION
5
6
  gem.authors = ["Brandon Keepers"]
6
7
  gem.email = ["brandon@opensoul.org"]
7
8
  gem.description = %q{Loads environment variables from `.env`.}
@@ -2,12 +2,21 @@ module Dotenv
2
2
  class Environment < Hash
3
3
  def initialize(filename)
4
4
  @filename = filename
5
- load if File.exists? @filename
5
+ load
6
6
  end
7
7
 
8
8
  def load
9
9
  read.each do |line|
10
- self[$1] = $2 || $3 if line =~ /\A(\w+)(?:=|: ?)(?:'([^']*)'|([^']*))\z/
10
+ if line =~ /\A(?:export\s+)?(\w+)(?:=|: ?)(.*)\z/
11
+ key = $1
12
+ case val = $2
13
+ # Remove single quotes
14
+ when /\A'(.*)'\z/ then self[key] = $1
15
+ # Remove double quotes and unescape string preserving newline characters
16
+ when /\A"(.*)"\z/ then self[key] = $1.gsub('\n', "\n").gsub(/\\(.)/, '\1')
17
+ else self[key] = val
18
+ end
19
+ end
11
20
  end
12
21
  end
13
22
 
@@ -1,3 +1,5 @@
1
+ require 'dotenv'
2
+
1
3
  module Dotenv
2
4
  class Railtie < Rails::Railtie
3
5
  include Rake::DSL if defined?(Rake)
@@ -0,0 +1,3 @@
1
+ module Dotenv
2
+ VERSION = '0.7.0'
3
+ end
@@ -0,0 +1 @@
1
+ require 'dotenv/railtie'
data/lib/dotenv.rb CHANGED
@@ -2,11 +2,22 @@ require 'dotenv/environment'
2
2
 
3
3
  module Dotenv
4
4
  def self.load(*filenames)
5
- filenames << '.env' if filenames.empty?
6
- filenames.inject({}) do |hash, filename|
7
- hash.merge Dotenv::Environment.new(filename).apply
5
+ default_if_empty(filenames).inject({}) do |hash, filename|
6
+ hash.merge(File.exists?(filename) ? Environment.new(filename).apply : {})
8
7
  end
9
8
  end
10
- end
11
9
 
12
- require 'dotenv/railtie' if defined?(Rails) and defined?(Rails::Railtie)
10
+ # same as `load`, but raises Errno::ENOENT if any files don't exist
11
+ def self.load!(*filenames)
12
+ load(
13
+ *default_if_empty(filenames).each do |filename|
14
+ raise(Errno::ENOENT.new(filename)) unless File.exists?(filename)
15
+ end
16
+ )
17
+ end
18
+
19
+ protected
20
+ def self.default_if_empty(filenames)
21
+ filenames.empty? ? (filenames << '.env') : filenames
22
+ end
23
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dotenv::Environment do
4
+ subject { env("OPTION_A=1\nOPTION_B=2") }
5
+
6
+ describe 'initialize' do
7
+ it 'reads the file' do
8
+ expect(subject['OPTION_A']).to eq('1')
9
+ expect(subject['OPTION_B']).to eq('2')
10
+ end
11
+
12
+ it 'fails if file does not exist' do
13
+ expect {
14
+ Dotenv::Environment.new('.does_not_exists')
15
+ }.to raise_error(Errno::ENOENT)
16
+ end
17
+ end
18
+
19
+ describe 'apply' do
20
+ it 'sets variables in ENV' do
21
+ subject.apply
22
+ expect(ENV['OPTION_A']).to eq('1')
23
+ end
24
+
25
+ it 'does not override defined variables' do
26
+ ENV['OPTION_A'] = 'predefined'
27
+ subject.apply
28
+ expect(ENV['OPTION_A']).to eq('predefined')
29
+ end
30
+ end
31
+
32
+ it 'parses unquoted values' do
33
+ expect(env('FOO=bar')).to eql({'FOO' => 'bar'})
34
+ end
35
+
36
+ it 'parses double quoted values' do
37
+ expect(env('FOO="bar"')).to eql({'FOO' => 'bar'})
38
+ end
39
+
40
+ it 'parses single quoted values' do
41
+ expect(env("FOO='bar'")).to eql({'FOO' => 'bar'})
42
+ end
43
+
44
+ it 'parses escaped double quotes' do
45
+ expect(env('FOO="escaped\"bar"')).to eql({'FOO' => 'escaped"bar'})
46
+ end
47
+
48
+ it 'parses yaml style options' do
49
+ expect(env("OPTION_A: 1")).to eql('OPTION_A' => '1')
50
+ end
51
+
52
+ it 'parses export keyword' do
53
+ expect(env("export OPTION_A=2")).to eql('OPTION_A' => '2')
54
+ end
55
+
56
+ it 'expands newlines in quoted strings' do
57
+ expect(env('FOO="bar\nbaz"')).to eql({"FOO" => "bar\nbaz"})
58
+ end
59
+
60
+ require 'tempfile'
61
+ def env(text)
62
+ file = Tempfile.new('dotenv')
63
+ file.write text
64
+ file.close
65
+ env = Dotenv::Environment.new(file.path)
66
+ file.unlink
67
+ env
68
+ end
69
+ end
data/spec/dotenv_spec.rb CHANGED
@@ -1,34 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Dotenv do
4
- let(:env_path) { fixture_path('plain.env') }
5
-
6
- before do
7
- @env_keys = ENV.keys
8
- end
9
-
10
- after do
11
- ENV.delete_if { |k,v| !@env_keys.include?(k) }
12
- end
13
-
14
- describe 'load' do
4
+ shared_examples 'load' do
15
5
  context 'with no args' do
6
+ let(:env_files) { [] }
7
+
16
8
  it 'defaults to .env' do
17
9
  Dotenv::Environment.should_receive(:new).with('.env').
18
10
  and_return(mock(:apply => {}))
19
- Dotenv.load
11
+ subject
20
12
  end
21
13
  end
22
14
 
23
15
  context 'with multiple files' do
16
+ let(:env_files) { ['.env', fixture_path('plain.env')] }
17
+
24
18
  let(:expected) do
25
19
  {'OPTION_A' => '1', 'OPTION_B' => '2', 'DOTENV' => 'true'}
26
20
  end
27
21
 
28
- subject do
29
- Dotenv.load('.env', env_path)
30
- end
31
-
32
22
  it 'loads all files' do
33
23
  subject
34
24
  expected.each do |key, value|
@@ -39,79 +29,43 @@ describe Dotenv do
39
29
  it 'returns hash of loaded environments' do
40
30
  expect(subject).to eq(expected)
41
31
  end
42
-
43
- context 'with quoted file' do
44
- let(:expected) do
45
- {'OPTION_A' => '1', 'OPTION_B' => '2', 'OPTION_C' => '', 'OPTION_D' => '\n', 'DOTENV' => 'true'}
46
- end
47
- let(:env_path) { fixture_path('quoted.env') }
48
-
49
- it 'returns hash of loaded environments' do
50
- expect(subject).to eq(expected)
51
- end
52
-
53
- end
54
-
55
- context 'with yaml file' do
56
- let(:expected) do
57
- {'OPTION_A' => '1', 'OPTION_B' => '2', 'OPTION_C' => '', 'OPTION_D' => '\n', 'DOTENV' => 'true'}
58
- end
59
- let(:env_path) { fixture_path('yaml.env') }
60
-
61
- it 'returns hash of loaded environments' do
62
- expect(subject).to eq(expected)
63
- end
64
-
65
- end
66
32
  end
67
33
  end
68
34
 
69
- describe Dotenv::Environment do
70
- subject { Dotenv::Environment.new(env_path) }
71
-
72
- context 'with a plain env file' do
35
+ describe 'load' do
36
+ subject { Dotenv.load(*env_files) }
73
37
 
74
- describe 'initialize' do
75
- it 'reads environment config' do
76
- expect(subject['OPTION_A']).to eq('1')
77
- expect(subject['OPTION_B']).to eq('2')
78
- end
79
- end
38
+ it_behaves_like 'load'
80
39
 
81
- describe 'apply' do
82
- it 'sets variables in ENV' do
83
- subject.apply
84
- expect(ENV['OPTION_A']).to eq('1')
85
- end
40
+ context 'when the file does not exist' do
41
+ let(:env_files) { ['.env_does_not_exist'] }
86
42
 
87
- it 'does not override defined variables' do
88
- ENV['OPTION_A'] = 'predefined'
89
- subject.apply
90
- expect(ENV['OPTION_A']).to eq('predefined')
91
- end
43
+ it 'fails silently' do
44
+ expect { subject }.not_to raise_error
45
+ expect(ENV.keys).to eq(@env_keys)
92
46
  end
93
47
  end
48
+ end
94
49
 
95
- context 'when the file does not exist' do
96
- let(:env_path) { fixture_path('.env_does_not_exist') }
50
+ describe 'load!' do
51
+ subject { Dotenv.load!(*env_files) }
97
52
 
98
- describe 'initialize' do
99
- it 'fails silently' do
100
- expect { Dotenv::Environment.new('.env_does_not_exist') }.not_to raise_error
101
- end
102
- end
53
+ it_behaves_like 'load'
103
54
 
104
- describe 'apply' do
105
- it 'does not effect env' do
106
- subject.apply
107
- expect(ENV.keys).to eq(@env_keys)
108
- end
55
+ context 'when one file exists and one does not' do
56
+ let(:env_files) { ['.env', '.env_does_not_exist'] }
57
+
58
+ it 'raises an Errno::ENOENT error and does not load any files' do
59
+ expect do
60
+ expect do
61
+ subject
62
+ end.to raise_error(Errno::ENOENT)
63
+ end.to_not change { ENV.keys }
109
64
  end
110
65
  end
111
-
112
66
  end
113
67
 
114
68
  def fixture_path(name)
115
69
  File.join(File.expand_path('../fixtures', __FILE__), name)
116
70
  end
117
- end
71
+ end
@@ -0,0 +1,2 @@
1
+ export OPTION_A=2
2
+ export OPTION_B='\n'
@@ -2,3 +2,7 @@ OPTION_A='1'
2
2
  OPTION_B='2'
3
3
  OPTION_C=''
4
4
  OPTION_D='\n'
5
+ OPTION_E="1"
6
+ OPTION_F="2"
7
+ OPTION_G=""
8
+ OPTION_H="\n"
data/spec/spec_helper.rb CHANGED
@@ -1 +1,7 @@
1
- require 'dotenv'
1
+ require 'dotenv'
2
+
3
+ RSpec.configure do |config|
4
+ # Restore the state of ENV after each spec
5
+ config.before { @env_keys = ENV.keys }
6
+ config.after { ENV.delete_if { |k,v| !@env_keys.include?(k) } }
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-25 00:00:00.000000000 Z
12
+ date: 2013-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -55,15 +55,21 @@ files:
55
55
  - .travis.yml
56
56
  - Changelog.md
57
57
  - Gemfile
58
+ - Guardfile
58
59
  - LICENSE
59
60
  - README.md
60
61
  - Rakefile
62
+ - dotenv-rails.gemspec
61
63
  - dotenv.gemspec
64
+ - lib/dotenv-rails.rb
62
65
  - lib/dotenv.rb
63
66
  - lib/dotenv/environment.rb
64
67
  - lib/dotenv/railtie.rb
65
68
  - lib/dotenv/tasks.rb
69
+ - lib/dotenv/version.rb
70
+ - spec/dotenv/environment_spec.rb
66
71
  - spec/dotenv_spec.rb
72
+ - spec/fixtures/exported.env
67
73
  - spec/fixtures/plain.env
68
74
  - spec/fixtures/quoted.env
69
75
  - spec/fixtures/yaml.env
@@ -80,18 +86,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
86
  - - ! '>='
81
87
  - !ruby/object:Gem::Version
82
88
  version: '0'
83
- segments:
84
- - 0
85
- hash: -3615033000965864371
86
89
  required_rubygems_version: !ruby/object:Gem::Requirement
87
90
  none: false
88
91
  requirements:
89
92
  - - ! '>='
90
93
  - !ruby/object:Gem::Version
91
94
  version: '0'
92
- segments:
93
- - 0
94
- hash: -3615033000965864371
95
95
  requirements: []
96
96
  rubyforge_project:
97
97
  rubygems_version: 1.8.23
@@ -99,7 +99,9 @@ signing_key:
99
99
  specification_version: 3
100
100
  summary: Loads environment variables from `.env`.
101
101
  test_files:
102
+ - spec/dotenv/environment_spec.rb
102
103
  - spec/dotenv_spec.rb
104
+ - spec/fixtures/exported.env
103
105
  - spec/fixtures/plain.env
104
106
  - spec/fixtures/quoted.env
105
107
  - spec/fixtures/yaml.env