|
| 1 | +require File.join(File.dirname(__FILE__), "/../spec_helper") |
| 2 | + |
| 3 | +describe FileEdit, "initialiize" do |
| 4 | + it "should create a new FileEdit object" do |
| 5 | + FileEdit.new("./spec/FileEdit/hosts").should be_kind_of(FileEdit) |
| 6 | + end |
| 7 | + |
| 8 | + it "should throw an exception if the input file does not exist" do |
| 9 | + lambda{FileEdit.new("nonexistfile")}.should raise_error |
| 10 | + end |
| 11 | + |
| 12 | + it "should throw an exception if the input file is blank" do |
| 13 | + lambda{FileEdit.new("./spec/FileEdit/blank")}.should raise_error |
| 14 | + end |
| 15 | + |
| 16 | +end |
| 17 | + |
| 18 | +describe FileEdit, "search_file_replace" do |
| 19 | + |
| 20 | + it "should accept regex passed in as a string (not Regexp object) and replace the match if there is one" do |
| 21 | + helper_method("./spec/FileEdit/hosts", "localhost", true) |
| 22 | + end |
| 23 | + |
| 24 | + |
| 25 | + it "should accept regex passed in as a Regexp object and replace the match if there is one" do |
| 26 | + helper_method("./spec/FileEdit/hosts", /localhost/, true) |
| 27 | + end |
| 28 | + |
| 29 | + |
| 30 | + it "should do nothing if there isn't a match" do |
| 31 | + helper_method("./spec/FileEdit/hosts", /pattern/, false) |
| 32 | + end |
| 33 | + |
| 34 | + |
| 35 | + def helper_method(filename, regex, value) |
| 36 | + fedit = FileEdit.new(filename) |
| 37 | + fedit.search_file_replace(regex, "replacement") |
| 38 | + fedit.write_file |
| 39 | + (File.exist? filename+".old").should be(value) |
| 40 | + if value == true |
| 41 | + newfile = File.new(filename).readlines |
| 42 | + newfile[0].should match(/replacement/) |
| 43 | + File.delete("./spec/FileEdit/hosts") |
| 44 | + File.rename("./spec/FileEdit/hosts.old", "./spec/FileEdit/hosts") |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | +end |
| 49 | + |
| 50 | +describe FileEdit, "search_file_replace_line" do |
| 51 | + |
| 52 | + it "should search for match and replace the whole line" do |
| 53 | + fedit = FileEdit.new("./spec/FileEdit/hosts") |
| 54 | + fedit.search_file_replace_line(/localhost/, "replacement line") |
| 55 | + fedit.write_file |
| 56 | + newfile = File.new("./spec/FileEdit/hosts").readlines |
| 57 | + newfile[0].should match(/replacement/) |
| 58 | + File.delete("./spec/FileEdit/hosts") |
| 59 | + File.rename("./spec/FileEdit/hosts.old", "./spec/FileEdit/hosts") |
| 60 | + end |
| 61 | + |
| 62 | +end |
| 63 | + |
| 64 | + |
| 65 | +describe FileEdit, "search_file_delete" do |
| 66 | + it "should search for match and delete the match" do |
| 67 | + fedit = FileEdit.new("./spec/FileEdit/hosts") |
| 68 | + fedit.search_file_delete(/localhost/) |
| 69 | + fedit.write_file |
| 70 | + newfile = File.new("./spec/FileEdit/hosts").readlines |
| 71 | + newfile[0].should_not match(/localhost/) |
| 72 | + File.delete("./spec/FileEdit/hosts") |
| 73 | + File.rename("./spec/FileEdit/hosts.old", "./spec/FileEdit/hosts") |
| 74 | + end |
| 75 | +end |
| 76 | + |
| 77 | +describe FileEdit, "search_file_delete_line" do |
| 78 | + it "should search for match and delete the matching line" do |
| 79 | + fedit = FileEdit.new("./spec/FileEdit/hosts") |
| 80 | + fedit.search_file_delete_line(/localhost/) |
| 81 | + fedit.write_file |
| 82 | + newfile = File.new("./spec/FileEdit/hosts").readlines |
| 83 | + newfile[0].should_not match(/localhost/) |
| 84 | + newfile[0].should match(/broadcasthost/) |
| 85 | + File.delete("./spec/FileEdit/hosts") |
| 86 | + File.rename("./spec/FileEdit/hosts.old", "./spec/FileEdit/hosts") |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +describe FileEdit, "insert_line_after_match" do |
| 91 | + it "should search for match and insert the given line after the matching line" do |
| 92 | + fedit = FileEdit.new("./spec/FileEdit/hosts") |
| 93 | + fedit.insert_line_after_match(/localhost/, "new line inserted") |
| 94 | + fedit.write_file |
| 95 | + newfile = File.new("./spec/FileEdit/hosts").readlines |
| 96 | + newfile[1].should match(/new/) |
| 97 | + File.delete("./spec/FileEdit/hosts") |
| 98 | + File.rename("./spec/FileEdit/hosts.old", "./spec/FileEdit/hosts") |
| 99 | + end |
| 100 | + |
| 101 | +end |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
0 commit comments