Skip to content

Commit

Permalink
Use Teaspoon.root instead of Dir.pwd. Resolves #465.
Browse files Browse the repository at this point in the history
  • Loading branch information
jejacks0n committed May 9, 2019
1 parent 0e1ecbe commit 5b912da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/teaspoon/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.find_env(override = nil)
env_files = override && !override.empty? ? [override] : standard_environments

env_files.each do |filename|
file = File.expand_path(filename, Dir.pwd)
file = File.expand_path(filename, Teaspoon.root)
ENV["TEASPOON_ENV"] = file if override
return file if File.exist?(file)
end
Expand All @@ -52,7 +52,7 @@ def self.rails_loaded?
end

def self.load_rails
rails_env = ENV["TEASPOON_RAILS_ENV"] || File.expand_path("config/environment.rb", Dir.pwd)
rails_env = ENV["TEASPOON_RAILS_ENV"] || File.expand_path("config/environment.rb", Teaspoon.root)

# Try to load rails, assume teaspoon_env will do it if the expected
# environment isn't found.
Expand Down
4 changes: 4 additions & 0 deletions lib/teaspoon/utility.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module Teaspoon
def self.root
defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)
end

def self.abort(message = nil, code = 1)
STDOUT.print("#{message}\n") if message
exit(code)
Expand Down
18 changes: 10 additions & 8 deletions spec/teaspoon/environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
end

describe "when loading with an override" do
let(:expanded) { File.expand_path("_override_", Dir.pwd) }
let(:expanded) { File.expand_path("_override_", Teaspoon.root) }

before do
allow(subject).to receive(:require_env).and_call_original
Expand All @@ -79,18 +79,20 @@
end

describe "when loading from defaults" do
let(:root) { Teaspoon.root }

it "looks for the standard files" do
expect(File).to receive(:exist?).with(File.expand_path("spec/teaspoon_env.rb", Dir.pwd)).and_return(false)
expect(File).to receive(:exist?).with(File.expand_path("test/teaspoon_env.rb", Dir.pwd)).and_return(false)
expect(File).to receive(:exist?).with(File.expand_path("teaspoon_env.rb", Dir.pwd)).and_return(true)
expect(subject).to receive(:require_env).with(File.expand_path("teaspoon_env.rb", Dir.pwd))
expect(File).to receive(:exist?).with(File.expand_path("spec/teaspoon_env.rb", Teaspoon.root)).and_return(false)
expect(File).to receive(:exist?).with(File.expand_path("test/teaspoon_env.rb", Teaspoon.root)).and_return(false)
expect(File).to receive(:exist?).with(File.expand_path("teaspoon_env.rb", Teaspoon.root)).and_return(true)
expect(subject).to receive(:require_env).with(File.expand_path("teaspoon_env.rb", Teaspoon.root))
subject.require_environment
end

it "short circuits when it finds a file" do
expect(File).to receive(:exist?).with(File.expand_path("spec/teaspoon_env.rb", Dir.pwd)).and_return(true)
expect(File).not_to receive(:exist?).with(File.expand_path("test/teaspoon_env.rb", Dir.pwd))
expect(subject).to receive(:require_env).with(File.expand_path("spec/teaspoon_env.rb", Dir.pwd))
expect(File).to receive(:exist?).with(File.expand_path("spec/teaspoon_env.rb", Teaspoon.root)).and_return(true)
expect(File).not_to receive(:exist?).with(File.expand_path("test/teaspoon_env.rb", Teaspoon.root))
expect(subject).to receive(:require_env).with(File.expand_path("spec/teaspoon_env.rb", Teaspoon.root))
subject.require_environment
end

Expand Down

0 comments on commit 5b912da

Please sign in to comment.