Skip to content

Commit 8c7941e

Browse files
author
Nuo Yan
committed
revised work with initial RSpec work
1 parent aed6222 commit 8c7941e

7 files changed

Lines changed: 119 additions & 23 deletions

File tree

chef/lib/chef/util/FileEdit.rb

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ def write_file
7474
#method is to control operation on whole line or only the match (1 for line, 2 for match)
7575
def search_match(regex, replace, command, method)
7676

77-
#check if regex is Regexp object or simple string and store the Regexp object in exp.
78-
exp = (regex.respond_to?(:gsub!) ? regex : Regexp.new(regex.to_s))
77+
#convert regex to a Regexp object (if not already is one) and store it in exp.
78+
exp = Regexp.new(regex)
7979

8080
#loop through contents and do the appropriate operation depending on 'command' and 'method'
8181
new_contents = []
8282

8383
contents.each do |line|
84-
if exp.match(line) # =~ exp
84+
if line.match(exp)
8585
self.file_edited = true
8686
case
8787
when command == 'r'
@@ -102,14 +102,3 @@ def search_match(regex, replace, command, method)
102102
self.contents = new_contents
103103
end
104104
end
105-
106-
#test
107-
if __FILE__ == $0
108-
fedit = FileEdit.new("test")
109-
fedit.insert_line_after_match(/test/, "new Line Inserted")
110-
fedit.search_file_replace(/test/, "replace")
111-
fedit.search_file_replace_line(/replace/, "this line is replaced")
112-
fedit.search_file_delete(/this/)
113-
fedit.search_file_delete_line(/new/)
114-
fedit.write_file()
115-
end
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+

chef/lib/chef/util/spec/FileEdit/blank

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
127.0.0.1 localhost
2+
255.255.255.255 broadcasthost
3+
::1 localhost
4+
fe80::1%lo0 localhost
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$: << File.join(File.dirname(__FILE__), "..")
2+
3+
require 'spec'
4+
require 'FileEdit'
5+

chef/lib/chef/util/test

Lines changed: 0 additions & 4 deletions
This file was deleted.

chef/lib/chef/util/test.backup

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)