summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@chef.io>2016-01-22 18:40:54 -0800
committerdanielsdeleo <dan@chef.io>2016-01-26 18:12:46 -0800
commitc958ec94037a4014685e0229cc47af12944b4a97 (patch)
tree7c296d5aff2ed28156282498f61de249ede2f962
parentb38c9d3c5164e5ff2ac328fc726517d6438b8462 (diff)
downloadchef-enable-policies-in-local-mode-by-default.tar.gz
Derive repo path from cookbook_artifact pathenable-policies-in-local-mode-by-default
Allows you to run local mode with autodetected paths using native policyfile objects only.
-rw-r--r--chef-config/lib/chef-config/config.rb7
-rw-r--r--chef-config/spec/unit/config_spec.rb24
2 files changed, 23 insertions, 8 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb
index fca7429120..68cece43da 100644
--- a/chef-config/lib/chef-config/config.rb
+++ b/chef-config/lib/chef-config/config.rb
@@ -106,9 +106,6 @@ module ChefConfig
# that upload or download files (such as knife upload, knife role from file,
# etc.) work.
default :chef_repo_path do
- # TODO: this should also look at the cookbook_artifacts path, for the
- # case when we have a repo from `chef export` that has cookbook artifacts
- # and no cookbooks.
if self.configuration[:cookbook_path]
if self.configuration[:cookbook_path].kind_of?(String)
File.expand_path("..", self.configuration[:cookbook_path])
@@ -117,6 +114,8 @@ module ChefConfig
File.expand_path("..", path)
end
end
+ elsif configuration[:cookbook_artifact_path]
+ File.expand_path("..", self.configuration[:cookbook_artifact_path])
else
cache_path
end
@@ -126,7 +125,7 @@ module ChefConfig
# In local mode, we auto-discover the repo root by looking for a path with "cookbooks" under it.
# This allows us to run config-free.
path = cwd
- until File.directory?(PathHelper.join(path, "cookbooks"))
+ until File.directory?(PathHelper.join(path, "cookbooks")) || File.directory?(PathHelper.join(path, "cookbook_artifacts"))
new_path = File.expand_path("..", path)
if new_path == path
ChefConfig.logger.warn("No cookbooks directory found at or above current directory. Assuming #{Dir.pwd}.")
diff --git a/chef-config/spec/unit/config_spec.rb b/chef-config/spec/unit/config_spec.rb
index 6b0fddcea9..ec14ad065a 100644
--- a/chef-config/spec/unit/config_spec.rb
+++ b/chef-config/spec/unit/config_spec.rb
@@ -338,7 +338,8 @@ RSpec.describe ChefConfig::Config do
before { ChefConfig::Config[:cookbook_path] = "/home/anne/repo/cookbooks" }
it "is set to a path one directory up from the cookbook_path" do
- expect(ChefConfig::Config[:chef_repo_path]).to eq("/home/anne/repo")
+ expected = File.expand_path("/home/anne/repo")
+ expect(ChefConfig::Config[:chef_repo_path]).to eq(expected)
end
end
@@ -348,19 +349,28 @@ RSpec.describe ChefConfig::Config do
before do
ChefConfig::Config[:cookbook_path] = [
"/home/anne/repo/cookbooks",
- "/home/anne/other_repo/cookbooks"
+ "/home/anne/other_repo/cookbooks",
]
end
it "is set to an Array of paths one directory up from the cookbook_paths" do
- expect(ChefConfig::Config[:chef_repo_path]).to eq([ "/home/anne/repo", "/home/anne/other_repo"])
+ expected = [ "/home/anne/repo", "/home/anne/other_repo"].map { |p| File.expand_path(p) }
+ expect(ChefConfig::Config[:chef_repo_path]).to eq(expected)
end
end
context "when cookbook_path is not set but cookbook_artifact_path is set" do
- it "is set to a path one directory up from the cookbook_artifact_path", :skip
+ before do
+ ChefConfig::Config[:cookbook_path] = nil
+ ChefConfig::Config[:cookbook_artifact_path] = "/home/roxie/repo/cookbook_artifacts"
+ end
+
+ it "is set to a path one directory up from the cookbook_artifact_path" do
+ expected = File.expand_path("/home/roxie/repo")
+ expect(ChefConfig::Config[:chef_repo_path]).to eq(expected)
+ end
end
@@ -397,6 +407,12 @@ RSpec.describe ChefConfig::Config do
expect(ChefConfig::Config[:environment_path]).to eq(environment_path)
end
+ it "ChefConfig::Config[:cookbook_artifact_path] defaults to /var/chef/cookbook_artifacts" do
+ allow(ChefConfig::Config).to receive(:cache_path).and_return(primary_cache_path)
+ environment_path = is_windows ? "#{primary_cache_path}\\cookbook_artifacts" : "#{primary_cache_path}/cookbook_artifacts"
+ expect(ChefConfig::Config[:cookbook_artifact_path]).to eq(environment_path)
+ end
+
describe "setting the config dir" do
context "when the config file is given with a relative path" do