Home | History | Annotate | Download | only in PrettyPatch
      1 #!/usr/bin/ruby
      2 
      3 require 'test/unit'
      4 require 'open-uri'
      5 require 'PrettyPatch'
      6 
      7 # Note: internet connection is needed to run this test suite.
      8 
      9 class PrettyPatch_test < Test::Unit::TestCase
     10     class Info
     11         TITLE = 0
     12         FILE = 1
     13         ADD = 2
     14         REMOVE = 3
     15         SHARED = 4
     16     end
     17 
     18     PATCHES = {
     19         20510 => ["Single change", 1, 1, 0, 2],
     20         20528 => ["No 'Index' or 'diff' in patch header", 1, 4, 3, 7],
     21         21151 => ["Leading '/' in the path of files", 4, 9, 1, 16],
     22         # Binary files use shared blocks, there are three in 30488.
     23         30488 => ["Quoted filenames in git diff", 23, 28, 25, 64 + 3],
     24         23920 => ["Mac line ending", 3, 3, 0, 5],
     25         39615 => ["Git signature", 2, 2, 0, 3],
     26         80852 => ["Changes one line plus ChangeLog", 2, 2, 1, 4],
     27         83127 => ["Only add stuff", 2, 2, 0, 3],
     28         85071 => ["Adds and removes from a file plus git signature", 2, 5, 3, 9],
     29         106368 => ["Images with git delta binary patch", 69, 8, 23, 10],
     30     }
     31 
     32     def get_patch_uri(id)
     33         "https://bugs.webkit.org/attachment.cgi?id=" + id.to_s
     34     end
     35 
     36     def get_patch(id)
     37         result = nil
     38         patch_uri = get_patch_uri(id)
     39         begin
     40             result = open(patch_uri) { |f| result = f.read }
     41         rescue => exception
     42             assert(false, "Fail to get patch " + patch_uri)
     43         end
     44         result
     45     end
     46 
     47     def check_one_patch(id, info)
     48         patch = get_patch(id)
     49         description = get_patch_uri(id)
     50         description +=  " (" + info[Info::TITLE] + ")" unless info[Info::TITLE].nil?
     51         puts "Testing " + description
     52         pretty = nil
     53         assert_nothing_raised("Crash while prettifying " + description) {
     54             pretty = PrettyPatch.prettify(patch)
     55         }
     56         assert(pretty, "Empty result while prettifying " + description)
     57         assert_equal(info[Info::FILE], $last_prettify_file_count, "Wrong number of files changed in " + description)
     58         assert_equal(info[Info::ADD], $last_prettify_part_count["add"], "Wrong number of 'add' parts in " + description)
     59         assert_equal(info[Info::REMOVE], $last_prettify_part_count["remove"], "Wrong number of 'remove' parts in " + description)
     60         assert_equal(info[Info::SHARED], $last_prettify_part_count["shared"], "Wrong number of 'shared' parts in " + description)
     61         assert_equal(0, $last_prettify_part_count["binary"], "Wrong number of 'binary' parts in " + description)
     62         assert_equal(0, $last_prettify_part_count["extract-error"], "Wrong number of 'extract-error' parts in " + description)
     63         return pretty
     64     end
     65 
     66     def test_patches
     67         PATCHES.each { |id, info| check_one_patch(id, info) }
     68     end
     69 
     70     def test_images_without_checksum
     71         pretty = check_one_patch(144064, ["Images without checksums", 10, 5, 4, 8])
     72         matches = pretty.match("INVALID: Image lacks a checksum.")
     73         # FIXME: This should match, but there's a bug when running the tests where the image data
     74         # doesn't get properly written out to the temp files, so there is no image and we don't print
     75         # the warning that the image is missing its checksum.
     76         assert(!matches, "Should have invalid checksums")
     77         # FIXME: This should only have 4 invalid images, but due to the above tempfile issue, there are 0.
     78         assert_equal(0, pretty.scan(/INVALID\: Image lacks a checksum\./).size)
     79     end
     80 
     81     def test_new_image
     82         pretty = check_one_patch(145881, ["New image", 19, 36, 19, 56])
     83         matches = pretty.match("INVALID: Image lacks a checksum.")
     84         assert(!matches, "Should not have invalid checksums")
     85     end
     86 
     87     def test_images_correctly_without_checksum_git
     88         pretty = check_one_patch(101620, ["Images correctly without checksums git", 7, 15, 10, 26])
     89         matches = pretty.match("INVALID: Image lacks a checksum.")
     90         assert(!matches, "Png should lack a checksum without an error.")
     91     end
     92 
     93     def test_images_correctly_without_checksum_svn
     94         pretty = check_one_patch(31202, ["Images correctly without checksums svn", 4, 4, 1, 4])
     95         matches = pretty.match("INVALID: Image lacks a checksum.")
     96         assert(!matches, "Png should lack a checksum without an error.")
     97     end
     98 
     99 end
    100