summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjkeiser <jkeiser@opscode.com>2013-01-16 11:01:41 -0800
committerJohn Keiser <jkeiser@opscode.com>2013-06-07 13:12:20 -0700
commitf35c1a48f4098185dc6feefc7c2e80d4311f770e (patch)
tree48d67185d290d0a5a824c363d1187f91bc3ce526
parent5cf1446795ceaefb7e926726eb27f38bd19a69b2 (diff)
downloadchef-f35c1a48f4098185dc6feefc7c2e80d4311f770e.tar.gz
Make knife show exit with error code on error
-rw-r--r--lib/chef/knife/show.rb16
-rw-r--r--spec/integration/knife/delete_spec.rb18
-rw-r--r--spec/integration/knife/show_spec.rb124
3 files changed, 146 insertions, 12 deletions
diff --git a/lib/chef/knife/show.rb b/lib/chef/knife/show.rb
index 9425da845e..df9a8b6516 100644
--- a/lib/chef/knife/show.rb
+++ b/lib/chef/knife/show.rb
@@ -1,5 +1,6 @@
require 'chef/chef_fs/knife'
require 'chef/chef_fs/file_system'
+require 'chef/chef_fs/file_system/not_found_error'
class Chef
class Knife
@@ -15,21 +16,30 @@ class Chef
def run
# Get the matches (recursively)
+ error = false
pattern_args.each do |pattern|
Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern) do |result|
if result.dir?
- ui.error "#{result.path_for_printing}: is a directory" if pattern.exact_path
+ ui.error "#{format_path(result)}: is a directory" if pattern.exact_path
+ error = true
else
begin
value = result.read
- output "#{result.path_for_printing}:"
+ output "#{format_path(result)}:"
output(format_for_display(value))
+ rescue Chef::ChefFS::FileSystem::OperationNotAllowedError => e
+ ui.error "#{format_path(e.entry)}: #{e.reason}."
+ error = true
rescue Chef::ChefFS::FileSystem::NotFoundError => e
- ui.error "#{e.entry.path_for_printing}: No such file or directory"
+ ui.error "#{format_path(e.entry)}: No such file or directory"
+ error = true
end
end
end
end
+ if error
+ exit 1
+ end
end
end
end
diff --git a/spec/integration/knife/delete_spec.rb b/spec/integration/knife/delete_spec.rb
index c233b6eb05..6f2a4f5afc 100644
--- a/spec/integration/knife/delete_spec.rb
+++ b/spec/integration/knife/delete_spec.rb
@@ -6,8 +6,8 @@ describe 'knife delete' do
extend IntegrationSupport
include KnifeSupport
- let :everything do
- <<EOM
+ let :everything do
+ <<EOM
/cookbooks
/cookbooks/x
/cookbooks/x/metadata.rb
@@ -20,26 +20,26 @@ describe 'knife delete' do
/roles
/roles/x.json
EOM
- end
+ end
- let :server_nothing do
- <<EOM
+ let :server_nothing do
+ <<EOM
/cookbooks
/data_bags
/environments
/environments/_default.json
/roles
EOM
- end
+ end
- let :nothing do
- <<EOM
+ let :nothing do
+ <<EOM
/cookbooks
/data_bags
/environments
/roles
EOM
- end
+ end
when_the_chef_server "has one of each thing" do
client 'x', '{}'
diff --git a/spec/integration/knife/show_spec.rb b/spec/integration/knife/show_spec.rb
new file mode 100644
index 0000000000..97a14bfe85
--- /dev/null
+++ b/spec/integration/knife/show_spec.rb
@@ -0,0 +1,124 @@
+require 'support/shared/integration/integration_helper'
+require 'chef/knife/show'
+
+describe 'knife show' do
+ extend IntegrationSupport
+ include KnifeSupport
+
+ when_the_chef_server "has one of each thing" do
+ client 'x', '{}'
+ cookbook 'x', '1.0.0', { 'metadata.rb' => 'version "1.0.0"' }
+ data_bag 'x', { 'y' => '{}' }
+ environment 'x', '{}'
+ node 'x', '{}'
+ role 'x', '{}'
+ user 'x', '{}'
+
+ when_the_repository 'also has one of each thing' do
+ file 'clients/x.json', { 'foo' => 'bar' }
+ file 'cookbooks/x/metadata.rb', 'version "1.0.1"'
+ file 'data_bags/x/y.json', { 'foo' => 'bar' }
+ file 'environments/_default.json', { 'foo' => 'bar' }
+ file 'environments/x.json', { 'foo' => 'bar' }
+ file 'nodes/x.json', { 'foo' => 'bar' }
+ file 'roles/x.json', { 'foo' => 'bar' }
+ file 'users/x.json', { 'foo' => 'bar' }
+
+ it 'knife show /cookbooks/x/metadata.rb shows the remote version' do
+ knife('show /cookbooks/x/metadata.rb').should_succeed <<EOM
+/cookbooks/x/metadata.rb:
+version "1.0.0"
+EOM
+ end
+ it 'knife show --local /cookbooks/x/metadata.rb shows the local version' do
+ knife('show --local /cookbooks/x/metadata.rb').should_succeed <<EOM
+/cookbooks/x/metadata.rb:
+version "1.0.1"
+EOM
+ end
+ it 'knife show /data_bags/x/y.json shows the remote version' do
+ knife('show /data_bags/x/y.json').should_succeed <<EOM
+/data_bags/x/y.json:
+{
+ "id": "y"
+}
+EOM
+ end
+ it 'knife show --local /data_bags/x/y.json shows the local version' do
+ knife('show --local /data_bags/x/y.json').should_succeed <<EOM
+/data_bags/x/y.json:
+{
+ "foo": "bar"
+}
+EOM
+ end
+ it 'knife show /environments/x.json shows the remote version' do
+ knife('show /environments/x.json').should_succeed <<EOM
+/environments/x.json:
+{
+ "name": "x",
+ "description": "",
+ "cookbook_versions": {
+ },
+ "json_class": "Chef::Environment",
+ "chef_type": "environment",
+ "default_attributes": {
+ },
+ "override_attributes": {
+ }
+}
+EOM
+ end
+ it 'knife show --local /environments/x.json shows the local version' do
+ knife('show --local /environments/x.json').should_succeed <<EOM
+/environments/x.json:
+{
+ "foo": "bar"
+}
+EOM
+ end
+ it 'knife show /roles/x.json shows the remote version' do
+ knife('show /roles/x.json').should_succeed <<EOM
+/roles/x.json:
+{
+ "name": "x",
+ "description": "",
+ "json_class": "Chef::Role",
+ "default_attributes": {
+ },
+ "override_attributes": {
+ },
+ "chef_type": "role",
+ "run_list": [
+
+ ],
+ "env_run_lists": {
+ }
+}
+EOM
+ end
+ it 'knife show --local /roles/x.json shows the local version' do
+ knife('show --local /roles/x.json').should_succeed <<EOM
+/roles/x.json:
+{
+ "foo": "bar"
+}
+EOM
+ end
+ # show directory
+ it 'knife show /data_bags/x fails' do
+ knife('show /data_bags/x').should_fail "ERROR: /data_bags/x: is a directory\n"
+ end
+ it 'knife show --local /data_bags/x fails' do
+ knife('show --local /data_bags/x').should_fail "ERROR: /data_bags/x: is a directory\n"
+ end
+ # show nonexistent file
+ it 'knife show /environments/nonexistent.json fails' do
+ knife('show /environments/nonexistent.json').should_fail "ERROR: /environments/nonexistent.json: No such file or directory\n"
+ end
+ it 'knife show --local /environments/nonexistent.json fails' do
+ knife('show --local /environments/nonexistent.json').should_fail "ERROR: /environments/nonexistent.json: No such file or directory\n"
+ end
+ end
+ end
+end