1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
#
# Author:: Nuo Yan (<nuo@chef.io>)
# Copyright:: Copyright 2008-2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "spec_helper"
require "tempfile"
describe Chef::Util::FileEdit do
let(:starting_content) do
<<~EOF
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
EOF
end
let(:localhost_replaced) do
<<~EOF
127.0.0.1 replacement
255.255.255.255 broadcasthost
::1 replacement
fe80::1%lo0 replacement
EOF
end
let(:localhost_line_replaced) do
<<~EOF
replacement line
255.255.255.255 broadcasthost
replacement line
replacement line
EOF
end
let(:localhost_deleted) do
# sensitive to deliberate trailing whitespace
"127.0.0.1 \n255.255.255.255 broadcasthost\n::1 \nfe80::1%lo0 \n"
end
let(:localhost_line_deleted) do
<<~EOF
255.255.255.255 broadcasthost
EOF
end
let(:append_after_all_localhost) do
<<~EOF
127.0.0.1 localhost
new line inserted
255.255.255.255 broadcasthost
::1 localhost
new line inserted
fe80::1%lo0 localhost
new line inserted
EOF
end
let(:append_after_content) do
<<~EOF
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
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)
f.close
f
end
let(:fedit) { Chef::Util::FileEdit.new(target_file.path) }
after(:each) do
target_file.close!
end
describe "initialiize" do
it "should create a new Chef::Util::FileEdit object" do
expect(fedit).to be_instance_of(Chef::Util::FileEdit)
end
it "should throw an exception if the input file does not exist" do
expect { Chef::Util::FileEdit.new("nonexistfile") }.to raise_error(ArgumentError)
end
# 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(:editor).lines).to be_instance_of(Array)
end
end
describe "when the file is blank" do
let(:hosts_content) { "" }
it "should not throw an exception" do
expect { fedit }.not_to raise_error
end
end
def edited_file_contents
IO.read(target_file.path)
end
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")
expect(fedit.unwritten_changes?).to be_truthy
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")
expect(fedit.unwritten_changes?).to be_truthy
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")
expect(fedit.unwritten_changes?).to be_falsey
fedit.write_file
expect(edited_file_contents).to eq(starting_content)
end
end
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")
expect(fedit.unwritten_changes?).to be_truthy
fedit.write_file
expect(edited_file_contents).to eq(localhost_line_replaced)
end
end
describe "search_file_delete" do
it "should search for match and delete the match" do
fedit.search_file_delete(/localhost/)
expect(fedit.unwritten_changes?).to be_truthy
fedit.write_file
expect(edited_file_contents).to eq(localhost_deleted)
end
end
describe "search_file_delete_line" do
it "should search for match and delete the matching line" do
fedit.search_file_delete_line(/localhost/)
expect(fedit.unwritten_changes?).to be_truthy
fedit.write_file
expect(edited_file_contents).to eq(localhost_line_deleted)
end
end
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")
expect(fedit.unwritten_changes?).to be_truthy
fedit.write_file
expect(edited_file_contents).to eq(append_after_all_localhost)
end
end
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")
expect(fedit.unwritten_changes?).to be_truthy
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")
expect(fedit.unwritten_changes?).to be_falsey
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_truthy
end
end
end
|