diff options
Diffstat (limited to 'spec/unit/util/file_edit_spec.rb')
-rw-r--r-- | spec/unit/util/file_edit_spec.rb | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/spec/unit/util/file_edit_spec.rb b/spec/unit/util/file_edit_spec.rb index d1d87a6bda..139b29d9ce 100644 --- a/spec/unit/util/file_edit_spec.rb +++ b/spec/unit/util/file_edit_spec.rb @@ -17,6 +17,7 @@ # require 'spec_helper' +require 'tempfile' describe Chef::Util::FileEdit do @@ -80,6 +81,17 @@ new line inserted EOF end + let(:append_twice) do + <<-EOF +127.0.0.1 localhost +255.255.255.255 broadcasthost +::1 localhost +fe80::1%lo0 localhost +once +twice + EOF + end + let(:target_file) do f = Tempfile.open('file_edit_spec') f.write(starting_content) @@ -104,7 +116,7 @@ new line inserted # CHEF-5018: people have monkey patched this and it has accidentally been broken it "should read the contents into memory as an array" do - expect(fedit.send(:contents)).to be_instance_of(Array) + expect(fedit.send(:editor).lines).to be_instance_of(Array) end end @@ -123,18 +135,21 @@ new line inserted describe "search_file_replace" do it "should accept regex passed in as a string (not Regexp object) and replace the match if there is one" do fedit.search_file_replace("localhost", "replacement") + fedit.unwritten_changes?.should be_true fedit.write_file expect(edited_file_contents).to eq(localhost_replaced) end it "should accept regex passed in as a Regexp object and replace the match if there is one" do fedit.search_file_replace(/localhost/, "replacement") + fedit.unwritten_changes?.should be_true fedit.write_file expect(edited_file_contents).to eq(localhost_replaced) end it "should do nothing if there isn't a match" do fedit.search_file_replace(/pattern/, "replacement") + fedit.unwritten_changes?.should be_false fedit.write_file expect(edited_file_contents).to eq(starting_content) end @@ -143,6 +158,7 @@ new line inserted describe "search_file_replace_line" do it "should search for match and replace the whole line" do fedit.search_file_replace_line(/localhost/, "replacement line") + fedit.unwritten_changes?.should be_true fedit.write_file expect(edited_file_contents).to eq(localhost_line_replaced) end @@ -151,6 +167,7 @@ new line inserted describe "search_file_delete" do it "should search for match and delete the match" do fedit.search_file_delete(/localhost/) + fedit.unwritten_changes?.should be_true fedit.write_file expect(edited_file_contents).to eq(localhost_deleted) end @@ -159,6 +176,7 @@ new line inserted describe "search_file_delete_line" do it "should search for match and delete the matching line" do fedit.search_file_delete_line(/localhost/) + fedit.unwritten_changes?.should be_true fedit.write_file expect(edited_file_contents).to eq(localhost_line_deleted) end @@ -167,6 +185,7 @@ new line inserted describe "insert_line_after_match" do it "should search for match and insert the given line after the matching line" do fedit.insert_line_after_match(/localhost/, "new line inserted") + fedit.unwritten_changes?.should be_true fedit.write_file expect(edited_file_contents).to eq(append_after_all_localhost) end @@ -175,14 +194,31 @@ new line inserted describe "insert_line_if_no_match" do it "should search for match and insert the given line if no line match" do fedit.insert_line_if_no_match(/pattern/, "new line inserted") + fedit.unwritten_changes?.should be_true fedit.write_file expect(edited_file_contents).to eq(append_after_content) end it "should do nothing if there is a match" do fedit.insert_line_if_no_match(/localhost/, "replacement") + fedit.unwritten_changes?.should be_false fedit.write_file expect(edited_file_contents).to eq(starting_content) end + + it "should work more than once" do + fedit.insert_line_if_no_match(/missing/, "once") + fedit.insert_line_if_no_match(/missing/, "twice") + fedit.write_file + expect(edited_file_contents).to eq(append_twice) + end + end + + describe "file_edited" do + it "should return true if a file got edited" do + fedit.insert_line_if_no_match(/pattern/, "new line inserted") + fedit.write_file + expect(fedit.file_edited?).to be_true + end end end |