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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
#
# Author:: John Keiser (<jkeiser@opscode.com>)
# Copyright:: Copyright 2012-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 "chef/chef_fs/file_pattern"
require "chef/chef_fs/command_line"
# Removes the date stamp from the diff and replaces it with ' DATE'
# example match: "/dev/null\t2012-10-16 16:15:54.000000000 +0000"
# windows match: "--- /dev/null\tTue Oct 16 18:04:34 2012"
def remove_os_differences(diff)
diff = diff.gsub(/([+-]{3}.*)\t.*/, '\1 DATE')
diff.gsub(/^@@ -\d(,\d)? \+\d(,\d)? @@/, "CONTEXT_LINE_NUMBERS")
end
describe "diff", :uses_diff => true do
include FileSystemSupport
context "with two filesystems with all types of difference" do
let(:a) {
memory_fs("a", {
:both_dirs => {
:sub_both_dirs => { :subsub => nil },
:sub_both_files => nil,
:sub_both_files_different => "a\n",
:sub_both_dirs_empty => {},
:sub_dirs_empty_in_a_filled_in_b => {},
:sub_dirs_empty_in_b_filled_in_a => { :subsub => nil },
:sub_a_only_dir => { :subsub => nil },
:sub_a_only_file => nil,
:sub_dir_in_a_file_in_b => {},
:sub_file_in_a_dir_in_b => nil,
},
:both_files => nil,
:both_files_different => "a\n",
:both_dirs_empty => {},
:dirs_empty_in_a_filled_in_b => {},
:dirs_empty_in_b_filled_in_a => { :subsub => nil },
:dirs_in_a_cannot_be_in_b => {},
:file_in_a_cannot_be_in_b => nil,
:a_only_dir => { :subsub => nil },
:a_only_file => nil,
:dir_in_a_file_in_b => {},
:file_in_a_dir_in_b => nil,
}, /cannot_be_in_a/)
}
let(:b) {
memory_fs("b", {
:both_dirs => {
:sub_both_dirs => { :subsub => nil },
:sub_both_files => nil,
:sub_both_files_different => "b\n",
:sub_both_dirs_empty => {},
:sub_dirs_empty_in_a_filled_in_b => { :subsub => nil },
:sub_dirs_empty_in_b_filled_in_a => {},
:sub_b_only_dir => { :subsub => nil },
:sub_b_only_file => nil,
:sub_dir_in_a_file_in_b => nil,
:sub_file_in_a_dir_in_b => {},
},
:both_files => nil,
:both_files_different => "b\n",
:both_dirs_empty => {},
:dirs_empty_in_a_filled_in_b => { :subsub => nil },
:dirs_empty_in_b_filled_in_a => {},
:dirs_in_b_cannot_be_in_a => {},
:file_in_b_cannot_be_in_a => nil,
:b_only_dir => { :subsub => nil },
:b_only_file => nil,
:dir_in_a_file_in_b => nil,
:file_in_a_dir_in_b => {},
}, /cannot_be_in_b/)
}
it "Chef::ChefFS::CommandLine.diff_print(/)" do
results = []
Chef::ChefFS::CommandLine.diff_print(pattern("/"), a, b, nil, nil) do |diff|
results << remove_os_differences(diff)
end
expect(results).to match_array([
'diff --knife a/both_dirs/sub_both_files_different b/both_dirs/sub_both_files_different
--- a/both_dirs/sub_both_files_different DATE
+++ b/both_dirs/sub_both_files_different DATE
CONTEXT_LINE_NUMBERS
-a
+b
','diff --knife a/both_dirs/sub_dirs_empty_in_a_filled_in_b/subsub b/both_dirs/sub_dirs_empty_in_a_filled_in_b/subsub
new file
--- /dev/null DATE
+++ b/both_dirs/sub_dirs_empty_in_a_filled_in_b/subsub DATE
CONTEXT_LINE_NUMBERS
+subsub
','diff --knife a/both_dirs/sub_dirs_empty_in_b_filled_in_a/subsub b/both_dirs/sub_dirs_empty_in_b_filled_in_a/subsub
deleted file
--- a/both_dirs/sub_dirs_empty_in_b_filled_in_a/subsub DATE
+++ /dev/null DATE
CONTEXT_LINE_NUMBERS
-subsub
',"Only in a/both_dirs: sub_a_only_dir
",'diff --knife a/both_dirs/sub_a_only_file b/both_dirs/sub_a_only_file
deleted file
--- a/both_dirs/sub_a_only_file DATE
+++ /dev/null DATE
CONTEXT_LINE_NUMBERS
-sub_a_only_file
',"File a/both_dirs/sub_dir_in_a_file_in_b is a directory while file b/both_dirs/sub_dir_in_a_file_in_b is a regular file
","File a/both_dirs/sub_file_in_a_dir_in_b is a regular file while file b/both_dirs/sub_file_in_a_dir_in_b is a directory
","Only in b/both_dirs: sub_b_only_dir
",'diff --knife a/both_dirs/sub_b_only_file b/both_dirs/sub_b_only_file
new file
--- /dev/null DATE
+++ b/both_dirs/sub_b_only_file DATE
CONTEXT_LINE_NUMBERS
+sub_b_only_file
','diff --knife a/both_files_different b/both_files_different
--- a/both_files_different DATE
+++ b/both_files_different DATE
CONTEXT_LINE_NUMBERS
-a
+b
','diff --knife a/dirs_empty_in_a_filled_in_b/subsub b/dirs_empty_in_a_filled_in_b/subsub
new file
--- /dev/null DATE
+++ b/dirs_empty_in_a_filled_in_b/subsub DATE
CONTEXT_LINE_NUMBERS
+subsub
','diff --knife a/dirs_empty_in_b_filled_in_a/subsub b/dirs_empty_in_b_filled_in_a/subsub
deleted file
--- a/dirs_empty_in_b_filled_in_a/subsub DATE
+++ /dev/null DATE
CONTEXT_LINE_NUMBERS
-subsub
',"Only in a: a_only_dir
",'diff --knife a/a_only_file b/a_only_file
deleted file
--- a/a_only_file DATE
+++ /dev/null DATE
CONTEXT_LINE_NUMBERS
-a_only_file
',"File a/dir_in_a_file_in_b is a directory while file b/dir_in_a_file_in_b is a regular file
","File a/file_in_a_dir_in_b is a regular file while file b/file_in_a_dir_in_b is a directory
","Only in b: b_only_dir
",'diff --knife a/b_only_file b/b_only_file
new file
--- /dev/null DATE
+++ b/b_only_file DATE
CONTEXT_LINE_NUMBERS
+b_only_file
' ])
end
it "Chef::ChefFS::CommandLine.diff_print(/both_dirs)" do
results = []
Chef::ChefFS::CommandLine.diff_print(pattern("/both_dirs"), a, b, nil, nil) do |diff|
results << remove_os_differences(diff)
end
expect(results).to match_array([
'diff --knife a/both_dirs/sub_both_files_different b/both_dirs/sub_both_files_different
--- a/both_dirs/sub_both_files_different DATE
+++ b/both_dirs/sub_both_files_different DATE
CONTEXT_LINE_NUMBERS
-a
+b
','diff --knife a/both_dirs/sub_dirs_empty_in_a_filled_in_b/subsub b/both_dirs/sub_dirs_empty_in_a_filled_in_b/subsub
new file
--- /dev/null DATE
+++ b/both_dirs/sub_dirs_empty_in_a_filled_in_b/subsub DATE
CONTEXT_LINE_NUMBERS
+subsub
','diff --knife a/both_dirs/sub_dirs_empty_in_b_filled_in_a/subsub b/both_dirs/sub_dirs_empty_in_b_filled_in_a/subsub
deleted file
--- a/both_dirs/sub_dirs_empty_in_b_filled_in_a/subsub DATE
+++ /dev/null DATE
CONTEXT_LINE_NUMBERS
-subsub
',"Only in a/both_dirs: sub_a_only_dir
",'diff --knife a/both_dirs/sub_a_only_file b/both_dirs/sub_a_only_file
deleted file
--- a/both_dirs/sub_a_only_file DATE
+++ /dev/null DATE
CONTEXT_LINE_NUMBERS
-sub_a_only_file
',"File a/both_dirs/sub_dir_in_a_file_in_b is a directory while file b/both_dirs/sub_dir_in_a_file_in_b is a regular file
","File a/both_dirs/sub_file_in_a_dir_in_b is a regular file while file b/both_dirs/sub_file_in_a_dir_in_b is a directory
","Only in b/both_dirs: sub_b_only_dir
",'diff --knife a/both_dirs/sub_b_only_file b/both_dirs/sub_b_only_file
new file
--- /dev/null DATE
+++ b/both_dirs/sub_b_only_file DATE
CONTEXT_LINE_NUMBERS
+sub_b_only_file
' ])
end
it "Chef::ChefFS::CommandLine.diff_print(/) with depth 1" do
results = []
Chef::ChefFS::CommandLine.diff_print(pattern("/"), a, b, 1, nil) do |diff|
results << remove_os_differences(diff)
end
expect(results).to match_array([
"Common subdirectories: b/both_dirs
",'diff --knife a/both_files_different b/both_files_different
--- a/both_files_different DATE
+++ b/both_files_different DATE
CONTEXT_LINE_NUMBERS
-a
+b
',"Common subdirectories: b/both_dirs_empty
","Common subdirectories: b/dirs_empty_in_b_filled_in_a
","Common subdirectories: b/dirs_empty_in_a_filled_in_b
","Only in a: a_only_dir
",'diff --knife a/a_only_file b/a_only_file
deleted file
--- a/a_only_file DATE
+++ /dev/null DATE
CONTEXT_LINE_NUMBERS
-a_only_file
',"File a/dir_in_a_file_in_b is a directory while file b/dir_in_a_file_in_b is a regular file
","File a/file_in_a_dir_in_b is a regular file while file b/file_in_a_dir_in_b is a directory
","Only in b: b_only_dir
",'diff --knife a/b_only_file b/b_only_file
new file
--- /dev/null DATE
+++ b/b_only_file DATE
CONTEXT_LINE_NUMBERS
+b_only_file
' ])
end
it "Chef::ChefFS::CommandLine.diff_print(/*_*) with depth 0" do
results = []
Chef::ChefFS::CommandLine.diff_print(pattern("/*_*"), a, b, 0, nil) do |diff|
results << remove_os_differences(diff)
end
expect(results).to match_array([
"Common subdirectories: b/both_dirs
",'diff --knife a/both_files_different b/both_files_different
--- a/both_files_different DATE
+++ b/both_files_different DATE
CONTEXT_LINE_NUMBERS
-a
+b
',"Common subdirectories: b/both_dirs_empty
","Common subdirectories: b/dirs_empty_in_b_filled_in_a
","Common subdirectories: b/dirs_empty_in_a_filled_in_b
","Only in a: a_only_dir
",'diff --knife a/a_only_file b/a_only_file
deleted file
--- a/a_only_file DATE
+++ /dev/null DATE
CONTEXT_LINE_NUMBERS
-a_only_file
',"File a/dir_in_a_file_in_b is a directory while file b/dir_in_a_file_in_b is a regular file
","File a/file_in_a_dir_in_b is a regular file while file b/file_in_a_dir_in_b is a directory
","Only in b: b_only_dir
",'diff --knife a/b_only_file b/b_only_file
new file
--- /dev/null DATE
+++ b/b_only_file DATE
CONTEXT_LINE_NUMBERS
+b_only_file
' ])
end
it "Chef::ChefFS::CommandLine.diff_print(/) in name-only mode" do
results = []
Chef::ChefFS::CommandLine.diff_print(pattern("/"), a, b, nil, :name_only) do |diff|
results << remove_os_differences(diff)
end
expect(results).to match_array([
"b/both_dirs/sub_both_files_different\n",
"b/both_dirs/sub_dirs_empty_in_b_filled_in_a/subsub\n",
"b/both_dirs/sub_dirs_empty_in_a_filled_in_b/subsub\n",
"b/both_dirs/sub_a_only_dir\n",
"b/both_dirs/sub_a_only_file\n",
"b/both_dirs/sub_b_only_dir\n",
"b/both_dirs/sub_b_only_file\n",
"b/both_dirs/sub_dir_in_a_file_in_b\n",
"b/both_dirs/sub_file_in_a_dir_in_b\n",
"b/both_files_different\n",
"b/dirs_empty_in_b_filled_in_a/subsub\n",
"b/dirs_empty_in_a_filled_in_b/subsub\n",
"b/a_only_dir\n",
"b/a_only_file\n",
"b/b_only_dir\n",
"b/b_only_file\n",
"b/dir_in_a_file_in_b\n",
"b/file_in_a_dir_in_b\n",
])
end
it "Chef::ChefFS::CommandLine.diff_print(/) in name-status mode" do
results = []
Chef::ChefFS::CommandLine.diff_print(pattern("/"), a, b, nil, :name_status) do |diff|
results << remove_os_differences(diff)
end
expect(results).to match_array([
"M\tb/both_dirs/sub_both_files_different\n",
"D\tb/both_dirs/sub_dirs_empty_in_b_filled_in_a/subsub\n",
"A\tb/both_dirs/sub_dirs_empty_in_a_filled_in_b/subsub\n",
"D\tb/both_dirs/sub_a_only_dir\n",
"D\tb/both_dirs/sub_a_only_file\n",
"A\tb/both_dirs/sub_b_only_dir\n",
"A\tb/both_dirs/sub_b_only_file\n",
"T\tb/both_dirs/sub_dir_in_a_file_in_b\n",
"T\tb/both_dirs/sub_file_in_a_dir_in_b\n",
"M\tb/both_files_different\n",
"D\tb/dirs_empty_in_b_filled_in_a/subsub\n",
"A\tb/dirs_empty_in_a_filled_in_b/subsub\n",
"D\tb/a_only_dir\n",
"D\tb/a_only_file\n",
"A\tb/b_only_dir\n",
"A\tb/b_only_file\n",
"T\tb/dir_in_a_file_in_b\n",
"T\tb/file_in_a_dir_in_b\n",
])
end
end
end
|