summaryrefslogtreecommitdiff
path: root/spec/unit/util/diff_spec.rb
blob: f46bc314887e589a0b207636ec8279db5c7fd22e (plain)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#
# Author:: Lamont Granquist (<lamont@opscode.com>)
# Copyright:: Copyright (c) 2013 Opscode, 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 "tmpdir"

shared_context "using file paths with spaces" do
  let!(:old_tempfile) { Tempfile.new("chef-util diff-spec") }
  let!(:new_tempfile) { Tempfile.new("chef-util diff-spec") }
end

shared_context "using file paths without spaces" do
  let!(:old_tempfile) { Tempfile.new("chef-util-diff-spec") }
  let!(:new_tempfile) { Tempfile.new("chef-util-diff-spec") }
end

shared_examples_for "a diff util" do
  it "should return a Chef::Util::Diff" do
    expect(differ).to be_a_kind_of(Chef::Util::Diff)
  end

  it "produces a diff even if the old_file does not exist" do
    old_tempfile.close
    old_tempfile.unlink
    expect(differ.for_output).to eql(["(no diff)"])
  end

  it "produces a diff even if the new_file does not exist" do
    new_tempfile.close
    new_tempfile.unlink
    expect(differ.for_output).to eql(["(no diff)"])
  end

  describe "when the two files exist with no content" do
    it "calling for_output should return the error message" do
      expect(differ.for_output).to eql(["(no diff)"])
    end

    it "calling for_reporting should be nil" do
      expect(differ.for_reporting).to be_nil
    end
  end

  describe "when diffs are disabled" do
    before do
      Chef::Config[:diff_disabled] = true
    end

    after do
      Chef::Config[:diff_disabled] = false
    end

    it "calling for_output should return the error message" do
      expect(differ.for_output).to eql( [ "(diff output suppressed by config)" ] )
    end

    it "calling for_reporting should be nil" do
      expect(differ.for_reporting).to be_nil
    end
  end

  describe "when the old_file has binary content" do
    before do
      old_tempfile.write("\x01\xff")
      old_tempfile.close
    end

    it "calling for_output should return the error message" do
      expect(differ.for_output).to eql( [ "(current file is binary, diff output suppressed)" ] )
    end

    it "calling for_reporting should be nil" do
      expect(differ.for_reporting).to be_nil
    end
  end

  describe "when the new_file has binary content" do
    before do
      new_tempfile.write("\x01\xff")
      new_tempfile.close
    end

    it "calling for_output should return the error message" do
      expect(differ.for_output).to eql( [ "(new content is binary, diff output suppressed)" ])
    end

    it "calling for_reporting should be nil" do
      expect(differ.for_reporting).to be_nil
    end
  end

  describe "when the default external encoding is UTF-8" do

    before do
      @saved_default_external = Encoding.default_external
      Encoding.default_external = Encoding::UTF_8
    end

    after do
      Encoding.default_external = @saved_default_external
    end

    describe "when a file has ASCII text" do
      before do
        new_tempfile.write(plain_ascii)
        new_tempfile.close
      end
      it "calling for_output should return a valid diff" do
        expect(differ.for_output.join("\\n")).to match(/\A--- .*\\n\+\+\+ .*\\n@@/m)
      end
      it "calling for_reporting should return a utf-8 string" do
        expect(differ.for_reporting.encoding).to equal(Encoding::UTF_8)
      end
    end

    describe "when a file has UTF-8 text" do
      before do
        new_tempfile.write(utf_8)
        new_tempfile.close
      end
      it "calling for_output should return a valid diff" do
        expect(differ.for_output.join("\\n")).to match(/\A--- .*\\n\+\+\+ .*\\n@@/m)
      end
      it "calling for_reporting should return a utf-8 string" do
        expect(differ.for_reporting.encoding).to equal(Encoding::UTF_8)
      end
    end

    describe "when a file has Latin-1 text" do
      before do
        new_tempfile.write(latin_1)
        new_tempfile.close
      end
      it "calling for_output should complain that the content is binary" do
        expect(differ.for_output).to eql( [ "(new content is binary, diff output suppressed)" ])
      end
      it "calling for_reporting should be nil" do
        expect(differ.for_reporting).to be_nil
      end
    end

    describe "when a file has Shift-JIS text" do
      before do
        new_tempfile.write(shift_jis)
        new_tempfile.close
      end
      it "calling for_output should complain that the content is binary" do
        expect(differ.for_output).to eql( [ "(new content is binary, diff output suppressed)" ])
      end
      it "calling for_reporting should be nil" do
        expect(differ.for_reporting).to be_nil
      end
    end

  end

  describe "when the default external encoding is Latin-1" do

    before do
      @saved_default_external = Encoding.default_external
      Encoding.default_external = Encoding::ISO_8859_1
    end

    after do
      Encoding.default_external = @saved_default_external
    end

    describe "when a file has ASCII text" do
      before do
        new_tempfile.write(plain_ascii)
        new_tempfile.close
      end
      it "calling for_output should return a valid diff" do
        expect(differ.for_output.join("\\n")).to match(/\A--- .*\\n\+\+\+ .*\\n@@/m)
      end
      it "calling for_reporting should return a utf-8 string" do
        expect(differ.for_reporting.encoding).to equal(Encoding::UTF_8)
      end
    end

    describe "when a file has UTF-8 text" do
      before do
        new_tempfile.write(utf_8)
        new_tempfile.close
      end
      it "calling for_output should complain that the content is binary" do
        expect(differ.for_output).to eql( [ "(new content is binary, diff output suppressed)" ])
      end
      it "calling for_reporting should be nil" do
        expect(differ.for_reporting).to be_nil
      end
    end

    describe "when a file has Latin-1 text" do
      before do
        new_tempfile.write(latin_1)
        new_tempfile.close
      end
      it "calling for_output should return a valid diff" do
        expect(differ.for_output.join("\\n")).to match(/\A--- .*\\n\+\+\+ .*\\n@@/m)
      end
      it "calling for_reporting should return a utf-8 string" do
        expect(differ.for_reporting.encoding).to equal(Encoding::UTF_8)
      end
    end

    describe "when a file has Shift-JIS text" do
      before do
        new_tempfile.write(shift_jis)
        new_tempfile.close
      end
      it "calling for_output should complain that the content is binary" do
        expect(differ.for_output).to eql( [ "(new content is binary, diff output suppressed)" ])
      end
      it "calling for_reporting should be nil" do
        expect(differ.for_reporting).to be_nil
      end
    end

  end
  describe "when the default external encoding is Shift_JIS" do

    before do
      @saved_default_external = Encoding.default_external
      Encoding.default_external = Encoding::Shift_JIS
    end

    after do
      Encoding.default_external = @saved_default_external
    end

    describe "when a file has ASCII text" do
      before do
        new_tempfile.write(plain_ascii)
        new_tempfile.close
      end
      it "calling for_output should return a valid diff" do
        expect(differ.for_output.join("\\n")).to match(/\A--- .*\\n\+\+\+ .*\\n@@/m)
      end
      it "calling for_reporting should return a utf-8 string" do
        expect(differ.for_reporting.encoding).to equal(Encoding::UTF_8)
      end
    end

    describe "when a file has UTF-8 text" do
      before do
        new_tempfile.write(utf_8)
        new_tempfile.close
      end
      it "calling for_output should complain that the content is binary" do
        expect(differ.for_output).to eql( [ "(new content is binary, diff output suppressed)" ])
      end
      it "calling for_reporting should be nil" do
        expect(differ.for_reporting).to be_nil
      end
    end

    describe "when a file has Latin-1 text" do
      before do
        new_tempfile.write(latin_1)
        new_tempfile.close
      end
      it "calling for_output should complain that the content is binary" do
        expect(differ.for_output).to eql( [ "(new content is binary, diff output suppressed)" ])
      end
      it "calling for_reporting should be nil" do
        expect(differ.for_reporting).to be_nil
      end
    end

    describe "when a file has Shift-JIS text" do
      before do
        new_tempfile.write(shift_jis)
        new_tempfile.close
      end
      it "calling for_output should return a valid diff" do
        expect(differ.for_output.join("\\n")).to match(/\A--- .*\\n\+\+\+ .*\\n@@/m)
      end
      it "calling for_reporting should return a utf-8 string" do
        expect(differ.for_reporting.encoding).to equal(Encoding::UTF_8)
      end
    end

  end

  describe "when testing the diff_filesize_threshold" do
    before do
      @diff_filesize_threshold_saved = Chef::Config[:diff_filesize_threshold]
      Chef::Config[:diff_filesize_threshold] = 10
    end

    after do
      Chef::Config[:diff_filesize_threshold] = @diff_filesize_threshold_saved
    end

    describe "when the old_file goes over the threshold" do
      before do
        old_tempfile.write("But thats what you get when Wu-Tang raised you")
        old_tempfile.close
      end

      it "calling for_output should return the error message" do
        expect(differ.for_output).to eql( [ "(file sizes exceed 10 bytes, diff output suppressed)" ])
      end

      it "calling for_reporting should be nil" do
        expect(differ.for_reporting).to be_nil
      end
    end

    describe "when the new_file goes over the threshold" do
      before do
        new_tempfile.write("But thats what you get when Wu-Tang raised you")
        new_tempfile.close
      end

      it "calling for_output should return the error message" do
        expect(differ.for_output).to eql( [ "(file sizes exceed 10 bytes, diff output suppressed)" ])
      end

      it "calling for_reporting should be nil" do
        expect(differ.for_reporting).to be_nil
      end
    end
  end

  describe "when generating a valid diff" do
    before do
      old_tempfile.write("foo")
      old_tempfile.close
      new_tempfile.write("bar")
      new_tempfile.close
    end

    it "calling for_output should return a unified diff" do
      expect(differ.for_output.size).to eql(5)
      expect(differ.for_output.join("\\n")).to match(/\A--- .*\\n\+\+\+ .*\\n@@/m)
    end

    it "calling for_reporting should return a unified diff" do
      expect(differ.for_reporting).to match(/\A--- .*\\n\+\+\+ .*\\n@@/m)
    end

    describe "when the diff output is too long" do

      before do
        @diff_output_threshold_saved = Chef::Config[:diff_output_threshold]
        Chef::Config[:diff_output_threshold] = 10
      end

      after do
        Chef::Config[:diff_output_threshold] = @diff_output_threshold_saved
      end

      it "calling for_output should return the error message" do
        expect(differ.for_output).to eql(["(long diff of over 10 characters, diff output suppressed)"])
      end

      it "calling for_reporting should be nil" do
        expect(differ.for_reporting).to be_nil
      end
    end
  end

  describe "when checking if files are binary or text" do

    it "should identify zero-length files as text" do
      Tempfile.open("chef-util-diff-spec") do |file|
        file.close
        expect(differ.send(:is_binary?, file.path)).to be_falsey
      end
    end

    it "should identify text files as text" do
      Tempfile.open("chef-util-diff-spec") do |file|
        file.write(plain_ascii)
        file.close
        expect(differ.send(:is_binary?, file.path)).to be_falsey
      end
    end

    it "should identify a null-terminated string files as binary" do
      Tempfile.open("chef-util-diff-spec") do |file|
        file.write("This is a binary file.\0")
        file.close
        expect(differ.send(:is_binary?, file.path)).to be_truthy
      end
    end

    it "should identify null-teriminated multi-line string files as binary" do
      Tempfile.open("chef-util-diff-spec") do |file|
        file.write("This is a binary file.\nNo Really\nit is\0")
        file.close
        expect(differ.send(:is_binary?, file.path)).to be_truthy
      end
    end

    describe "when the default external encoding is UTF-8" do

      before do
        @saved_default_external = Encoding.default_external
        Encoding.default_external = Encoding::UTF_8
      end

      after do
        Encoding.default_external = @saved_default_external
      end

      it "should identify normal ASCII as text" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(plain_ascii)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_falsey
        end
      end

      it "should identify UTF-8 as text" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(utf_8)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_falsey
        end
      end

      it "should identify Latin-1 that is invalid UTF-8 as binary" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(latin_1)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_truthy
        end
      end

      it "should identify Shift-JIS that is invalid UTF-8 as binary" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(shift_jis)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_truthy
        end
      end

    end

    describe "when the default external encoding is Latin-1" do

      before do
        @saved_default_external = Encoding.default_external
        Encoding.default_external = Encoding::ISO_8859_1
      end

      after do
        Encoding.default_external = @saved_default_external
      end

      it "should identify normal ASCII as text" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(plain_ascii)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_falsey
        end
      end

      it "should identify UTF-8 that is invalid Latin-1 as binary" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(utf_8)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_truthy
        end
      end

      it "should identify Latin-1 as text" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(latin_1)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_falsey
        end
      end

      it "should identify Shift-JIS that is invalid Latin-1 as binary" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(shift_jis)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_truthy
        end
      end
    end

    describe "when the default external encoding is Shift-JIS" do

      before do
        @saved_default_external = Encoding.default_external
        Encoding.default_external = Encoding::Shift_JIS
      end

      after do
        Encoding.default_external = @saved_default_external
      end

      it "should identify normal ASCII as text" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(plain_ascii)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_falsey
        end
      end
      it "should identify UTF-8 that is invalid Shift-JIS as binary" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(utf_8)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_truthy
        end
      end

      it "should identify Latin-1 that is invalid Shift-JIS as binary" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(latin_1)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_truthy
        end
      end

      it "should identify Shift-JIS as text" do
        Tempfile.open("chef-util-diff-spec") do |file|
          file.write(shift_jis)
          file.close
          expect(differ.send(:is_binary?, file.path)).to be_falsey
        end
      end

    end
  end

end

describe Chef::Util::Diff, :uses_diff => true do
  let!(:old_file) { old_tempfile.path }
  let!(:new_file) { new_tempfile.path }

  let(:plain_ascii) { "This is a text file.\nWith more than one line.\nAnd a \tTab.\nAnd lets make sure that other printable chars work too: ~!@\#$%^&*()`:\"<>?{}|_+,./;'[]\\-=\n" }
  # these are all byte sequences that are illegal in the other encodings... (but they may legally transcode)
  let(:utf_8) { "testing utf-8 unicode...\n\n\non a new line: \xE2\x80\x93\n" }  # unicode em-dash
  let(:latin_1) { "It is more metal.\nif you have an \xFDmlaut.\n" } # NB: changed to y-with-diaresis, but i'm American so I don't know the difference
  let(:shift_jis) { "I have no idea what this character is:\n \x83\x80.\n" } # seriously, no clue, but \x80 is nice and illegal in other encodings

  let(:differ) do  # subject
    differ = Chef::Util::Diff.new
    differ.diff(old_file, new_file)
    differ
  end

  describe "when file path has spaces" do
    include_context "using file paths with spaces"

    it_behaves_like "a diff util"
  end


  describe "when file path doesn't have spaces" do
    include_context "using file paths without spaces"

    it_behaves_like "a diff util"
  end
end