summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2013-10-23 15:05:54 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2013-10-23 17:15:11 -0700
commitcc2720bbe3bec1f86f564466e093539f80b2887f (patch)
tree3bad5a10c0935a79bbe519434366391ef77406a6
parent48bcedb011bbd1d033c43e54c601adf0ab2d26c0 (diff)
downloadchef-cc2720bbe3bec1f86f564466e093539f80b2887f.tar.gz
port node_editor to use ui.edit_data()
- easier to change the code and reduce the code dupliation than to write the specs for this object.
-rw-r--r--lib/chef/knife/core/node_editor.rb47
-rw-r--r--spec/unit/knife/node_edit_spec.rb39
2 files changed, 48 insertions, 38 deletions
diff --git a/lib/chef/knife/core/node_editor.rb b/lib/chef/knife/core/node_editor.rb
index 454af77690..073492197c 100644
--- a/lib/chef/knife/core/node_editor.rb
+++ b/lib/chef/knife/core/node_editor.rb
@@ -36,11 +36,24 @@ class Chef
abort "You specified the --disable_editing option, nothing to edit" if config[:disable_editing]
assert_editor_set!
- updated_node_data = edit_data(view)
+ updated_node_data = @ui.edit_data(view)
apply_updates(updated_node_data)
@updated_node
end
+ def updated?
+ pristine_copy = Chef::JSONCompat.from_json(Chef::JSONCompat.to_json(node), :create_additions => false)
+ updated_copy = Chef::JSONCompat.from_json(Chef::JSONCompat.to_json(@updated_node), :create_additions => false)
+ unless pristine_copy == updated_copy
+ updated_properties = %w{name normal chef_environment run_list default override automatic}.reject do |key|
+ pristine_copy[key] == updated_copy[key]
+ end
+ end
+ ( pristine_copy != updated_copy ) && updated_properties
+ end
+
+ private
+
def view
result = {}
result["name"] = node.name
@@ -53,12 +66,7 @@ class Chef
result["override"] = node.override_attrs
result["automatic"] = node.automatic_attrs
end
- Chef::JSONCompat.to_json_pretty(result)
- end
-
- def edit_data(text)
- edited_data = tempfile_for(text) {|filename| system("#{config[:editor]} #{filename}")}
- Chef::JSONCompat.from_json(edited_data)
+ result
end
def apply_updates(updated_data)
@@ -85,19 +93,6 @@ class Chef
end
end
- def updated?
- pristine_copy = Chef::JSONCompat.from_json(Chef::JSONCompat.to_json(node), :create_additions => false)
- updated_copy = Chef::JSONCompat.from_json(Chef::JSONCompat.to_json(@updated_node), :create_additions => false)
- unless pristine_copy == updated_copy
- updated_properties = %w{name normal chef_environment run_list default override automatic}.reject do |key|
- pristine_copy[key] == updated_copy[key]
- end
- end
- ( pristine_copy != updated_copy ) && updated_properties
- end
-
- private
-
def abort(message)
ui.error(message)
exit 1
@@ -109,18 +104,6 @@ class Chef
end
end
- def tempfile_for(data)
- Tempfile.open([ 'knife-edit-', '.json' ]) do |file|
-
- file.sync = true
- file.puts data
- file.close
-
- yield file.path
-
- IO.read(file.path)
- end
- end
end
end
end
diff --git a/spec/unit/knife/node_edit_spec.rb b/spec/unit/knife/node_edit_spec.rb
index e8f6937a31..7fbd8192d1 100644
--- a/spec/unit/knife/node_edit_spec.rb
+++ b/spec/unit/knife/node_edit_spec.rb
@@ -20,6 +20,12 @@ require 'spec_helper'
Chef::Knife::NodeEdit.load_deps
describe Chef::Knife::NodeEdit do
+
+ # helper to convert the view from Chef objects into Ruby objects representing JSON
+ def deserialized_json_view
+ actual = Chef::JSONCompat.from_json(Chef::JSONCompat.to_json_pretty(@knife.node_editor.send(:view)))
+ end
+
before(:each) do
Chef::Config[:node_name] = "webmonkey.example.com"
@knife = Chef::Knife::NodeEdit.new
@@ -49,7 +55,7 @@ describe Chef::Knife::NodeEdit do
end
it "creates a view of the node without attributes from roles or ohai" do
- actual = Chef::JSONCompat.from_json(@knife.node_editor.view)
+ actual = deserialized_json_view
actual.should_not have_key("automatic")
actual.should_not have_key("override")
actual.should_not have_key("default")
@@ -61,7 +67,7 @@ describe Chef::Knife::NodeEdit do
it "shows the extra attributes when given the --all option" do
@knife.config[:all_attributes] = true
- actual = Chef::JSONCompat.from_json(@knife.node_editor.view)
+ actual = deserialized_json_view
actual["automatic"].should == {"go" => "away"}
actual["override"].should == {"dont" => "show"}
actual["default"].should == {"hide" => "me"}
@@ -71,18 +77,39 @@ describe Chef::Knife::NodeEdit do
end
it "does not consider unedited data updated" do
- view = Chef::JSONCompat.from_json( @knife.node_editor.view )
- @knife.node_editor.apply_updates(view)
+ view = deserialized_json_view
+ @knife.node_editor.send(:apply_updates, view)
@knife.node_editor.should_not be_updated
end
it "considers edited data updated" do
- view = Chef::JSONCompat.from_json( @knife.node_editor.view )
+ view = deserialized_json_view
view["run_list"] << "role[fuuu]"
- @knife.node_editor.apply_updates(view)
+ @knife.node_editor.send(:apply_updates, view)
@knife.node_editor.should be_updated
end
end
+
+ describe "edit_node" do
+
+ before do
+ @knife.stub!(:node).and_return(@node)
+ end
+
+ let(:subject) { @knife.node_editor.edit_node }
+
+ it "raises an exception when editing is disabled" do
+ @knife.config[:disable_editing] = true
+ expect{ subject }.to raise_error(SystemExit)
+ end
+
+ it "raises an exception when the editor is not set" do
+ @knife.config[:editor] = nil
+ expect{ subject }.to raise_error(SystemExit)
+ end
+
+ end
+
end