Permalink
Browse files

[CHEF-3663] Search for .chef in ENV[PWD] if set

Prefer ENV["PWD"] for current working dir, but fall back to Dir.pwd if
not set. Fixes config path searching when part of the path hierarchy is
a symlinked directory.
  • Loading branch information...
danielsdeleo committed Dec 5, 2012
1 parent c18db33 commit 7b0eba077937ff2518fdc32bf2c9a7dc6c7714b1
Showing with 34 additions and 2 deletions.
  1. +11 −2 lib/chef/knife.rb
  2. +23 −0 spec/unit/knife_spec.rb
@@ -226,13 +226,22 @@ def self.subcommand_not_found!(args)
exit 10
end

@@chef_config_dir = nil
def self.working_directory
ENV['PWD'] || Dir.pwd
end

def self.reset_config_path!
@@chef_config_dir = nil
end

reset_config_path!


# search upward from current_dir until .chef directory is found
def self.chef_config_dir
if @@chef_config_dir.nil? # share this with subclasses
@@chef_config_dir = false
full_path = Dir.pwd.split(File::SEPARATOR)
full_path = working_directory.split(File::SEPARATOR)
(full_path.length - 1).downto(0) do |i|
candidate_directory = File.join(full_path[0..i] + [".chef" ])
if File.exist?(candidate_directory) && File.directory?(candidate_directory)
@@ -40,6 +40,29 @@ module KnifeSpecs
@stdout = StringIO.new
end

describe "selecting a config file" do
context "when the current working dir is inside a symlinked directory" do
before do
Chef::Knife.reset_config_path!
# pwd according to your shell is /home/someuser/prod/chef-repo, but
# chef-repo is a symlink to /home/someuser/codes/chef-repo
ENV.stub!(:[]).with("PWD").and_return("/home/someuser/prod/chef-repo")
Dir.stub!(:pwd).and_return("/home/someuser/codes/chef-repo")
end

after do
Chef::Knife.reset_config_path!
end

it "loads the config from the non-dereferenced directory path" do
File.should_receive(:exist?).with("/home/someuser/prod/chef-repo/.chef").and_return(false)
File.should_receive(:exist?).with("/home/someuser/prod/.chef").and_return(true)
File.should_receive(:directory?).with("/home/someuser/prod/.chef").and_return(true)
Chef::Knife.chef_config_dir.should == "/home/someuser/prod/.chef"
end
end
end

describe "after loading a subcommand" do
before do
Chef::Knife.reset_subcommands!

0 comments on commit 7b0eba0

Please sign in to comment.