summaryrefslogtreecommitdiff
path: root/chef/spec/unit/util/file_edit_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'chef/spec/unit/util/file_edit_spec.rb')
-rw-r--r--chef/spec/unit/util/file_edit_spec.rb54
1 files changed, 30 insertions, 24 deletions
diff --git a/chef/spec/unit/util/file_edit_spec.rb b/chef/spec/unit/util/file_edit_spec.rb
index 24d6503fc2..8f4075f6a7 100644
--- a/chef/spec/unit/util/file_edit_spec.rb
+++ b/chef/spec/unit/util/file_edit_spec.rb
@@ -16,12 +16,18 @@
# limitations under the License.
#
+require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
-require File.join(File.dirname(__FILE__), '..', '..', "spec_helper")
+module Home
+ PATH = File.expand_path(File.dirname(__FILE__))
+ DATA = File.join(PATH, "..", "..", "data", "fileedit")
+ HOSTS = File.join(DATA, "hosts")
+ HOSTS_OLD = File.join(DATA, "hosts.old")
+end
describe Chef::Util::FileEdit, "initialiize" do
it "should create a new Chef::Util::FileEdit object" do
- Chef::Util::FileEdit.new("./spec/data/fileedit/hosts").should be_kind_of(Chef::Util::FileEdit)
+ Chef::Util::FileEdit.new(Home::HOSTS).should be_kind_of(Chef::Util::FileEdit)
end
it "should throw an exception if the input file does not exist" do
@@ -29,7 +35,7 @@ describe Chef::Util::FileEdit, "initialiize" do
end
it "should throw an exception if the input file is blank" do
- lambda{Chef::Util::FileEdit.new("./spec/data/fileedit/blank")}.should raise_error
+ lambda{Chef::Util::FileEdit.new(Home::DATA + "/blank")}.should raise_error
end
end
@@ -37,17 +43,17 @@ end
describe Chef::Util::FileEdit, "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
- helper_method("./spec/data/fileedit/hosts", "localhost", true)
+ helper_method(Home::HOSTS, "localhost", true)
end
it "should accept regex passed in as a Regexp object and replace the match if there is one" do
- helper_method("./spec/data/fileedit/hosts", /localhost/, true)
+ helper_method(Home::HOSTS, /localhost/, true)
end
it "should do nothing if there isn't a match" do
- helper_method("./spec/data/fileedit/hosts", /pattern/, false)
+ helper_method(Home::HOSTS, /pattern/, false)
end
@@ -59,8 +65,8 @@ describe Chef::Util::FileEdit, "search_file_replace" do
if value == true
newfile = File.new(filename).readlines
newfile[0].should match(/replacement/)
- File.delete("./spec/data/fileedit/hosts")
- File.rename("./spec/data/fileedit/hosts.old", "./spec/data/fileedit/hosts")
+ File.delete(Home::HOSTS)
+ File.rename(Home::HOSTS_OLD, Home::HOSTS)
end
end
@@ -69,53 +75,53 @@ end
describe Chef::Util::FileEdit, "search_file_replace_line" do
it "should search for match and replace the whole line" do
- fedit = Chef::Util::FileEdit.new("./spec/data/fileedit/hosts")
+ fedit = Chef::Util::FileEdit.new(Home::HOSTS)
fedit.search_file_replace_line(/localhost/, "replacement line")
fedit.write_file
- newfile = File.new("./spec/data/fileedit/hosts").readlines
+ newfile = File.new(Home::HOSTS).readlines
newfile[0].should match(/replacement/)
newfile[0].should_not match(/127/)
- File.delete("./spec/data/fileedit/hosts")
- File.rename("./spec/data/fileedit/hosts.old", "./spec/data/fileedit/hosts")
+ File.delete(Home::HOSTS)
+ File.rename(Home::HOSTS_OLD, Home::HOSTS)
end
end
describe Chef::Util::FileEdit, "search_file_delete" do
it "should search for match and delete the match" do
- fedit = Chef::Util::FileEdit.new("./spec/data/fileedit/hosts")
+ fedit = Chef::Util::FileEdit.new(Home::HOSTS)
fedit.search_file_delete(/localhost/)
fedit.write_file
- newfile = File.new("./spec/data/fileedit/hosts").readlines
+ newfile = File.new(Home::HOSTS).readlines
newfile[0].should_not match(/localhost/)
newfile[0].should match(/127/)
- File.delete("./spec/data/fileedit/hosts")
- File.rename("./spec/data/fileedit/hosts.old", "./spec/data/fileedit/hosts")
+ File.delete(Home::HOSTS)
+ File.rename(Home::HOSTS_OLD, Home::HOSTS)
end
end
describe Chef::Util::FileEdit, "search_file_delete_line" do
it "should search for match and delete the matching line" do
- fedit = Chef::Util::FileEdit.new("./spec/data/fileedit/hosts")
+ fedit = Chef::Util::FileEdit.new(Home::HOSTS)
fedit.search_file_delete_line(/localhost/)
fedit.write_file
- newfile = File.new("./spec/data/fileedit/hosts").readlines
+ newfile = File.new(Home::HOSTS).readlines
newfile[0].should_not match(/localhost/)
newfile[0].should match(/broadcasthost/)
- File.delete("./spec/data/fileedit/hosts")
- File.rename("./spec/data/fileedit/hosts.old", "./spec/data/fileedit/hosts")
+ File.delete(Home::HOSTS)
+ File.rename(Home::HOSTS_OLD, Home::HOSTS)
end
end
describe Chef::Util::FileEdit, "insert_line_after_match" do
it "should search for match and insert the given line after the matching line" do
- fedit = Chef::Util::FileEdit.new("./spec/data/fileedit/hosts")
+ fedit = Chef::Util::FileEdit.new(Home::HOSTS)
fedit.insert_line_after_match(/localhost/, "new line inserted")
fedit.write_file
- newfile = File.new("./spec/data/fileedit/hosts").readlines
+ newfile = File.new(Home::HOSTS).readlines
newfile[1].should match(/new/)
- File.delete("./spec/data/fileedit/hosts")
- File.rename("./spec/data/fileedit/hosts.old", "./spec/data/fileedit/hosts")
+ File.delete(Home::HOSTS)
+ File.rename(Home::HOSTS_OLD, Home::HOSTS)
end
end