Permalink
Browse files

kv edit now requires -c to create a new node

  • Loading branch information...
1 parent d3500e8 commit fb5053aaa67afb5a02bf296032a05565ddb9f60f @fetep committed Jun 3, 2012
Showing with 38 additions and 1 deletion.
  1. +7 −1 lib/kv/command.rb
  2. +31 −0 spec/kv/command_spec.rb
View
@@ -266,14 +266,20 @@ def edit(args)
kv_init
opts = Trollop::options(args) do
- banner "Usage: kv [-d dir] edit <node>"
+ banner "Usage: kv [-d dir] edit [-c] <node>"
+
+ opt :create, "allow creation of new nodes",
+ :default => false
end
if args.length != 1
raise KV::Error, "edit takes one node name"
end
node_name = args.shift
+ if not opts[:create] and not @kv.node?(node_name)
+ raise KV::Error, "node #{node_name} does not exist (-c to create)"
+ end
node = @kv.node(node_name)
node_path = node.path
@@ -435,6 +435,37 @@
n["key1"].should eq("value2")
end
+ it "should create a new node with -c" do
+ kv = KV.new(:path => @kvdb_path)
+
+ data_file = File.join(@tmp_dir, "kvedit")
+ File.open(data_file, "w+") do |f|
+ f.puts "key1: value1"
+ end
+
+ ENV["EDITOR"] = "cp #{data_file}"
+ KV::Command.new(@kvdb_path).run("edit", ["-c", "test/1"])
+
+ kv = KV.new(:path => @kvdb_path)
+ kv.node?("test/1").should eq(true)
+ kv.node("test/1")["key1"].should eq("value1")
+ end
+
+ it "should fail on a non-existant node without -c" do
+ kv = KV.new(:path => @kvdb_path)
+
+ data_file = File.join(@tmp_dir, "kvedit")
+ File.open(data_file, "w+") do |f|
+ f.puts "key1: value2"
+ end
+
+ ENV["EDITOR"] = "cp #{data_file}"
+ expect { KV::Command.new(@kvdb_path).run("edit", ["test/1"]) }.should \
+ raise_error(KV::Error, "node test/1 does not exist (-c to create)")
+
+ kv.node?("test/1").should eq(false)
+ end
+
it "should handle attribute deletion" do
kv = KV.new(:path => @kvdb_path)
n = kv.node("test/1")

0 comments on commit fb5053a

Please sign in to comment.