test/blob_test.rb in rugged-0.18.0.gh.de28323 vs test/blob_test.rb in rugged-0.19.0

- old
+ new

@@ -8,11 +8,86 @@ blob = @repo.lookup(oid) assert_equal 9, blob.size assert_equal "new file\n", blob.content assert_equal :blob, blob.type assert_equal oid, blob.oid + assert_equal "new file\n", blob.text end + + def test_blob_sloc + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + assert_equal 328, blob.sloc + end + + def test_blob_content_with_size + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + content = blob.content(10) + assert_equal "# Rugged\n*", content + assert_equal 10, content.size + end + + def test_blob_content_with_size_gt_file_size + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + content = blob.content(1000000) + assert_equal blob.size, content.size + end + + def test_blob_content_with_zero_size + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + content = blob.content(0) + assert_equal '', content + end + + def test_blob_content_with_negative_size + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + content = blob.content(-100) + assert_equal blob.size, content.size + end + + def test_blob_text_with_max_lines + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + assert_equal "# Rugged\n", blob.text(1) + end + + def test_blob_text_with_lines_gt_file_lines + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + text = blob.text(1000000) + assert_equal 464, text.lines.count + end + + def test_blob_text_with_zero_lines + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + text = blob.text(0) + assert_equal '', text + end + + def test_blob_text_with_negative_lines + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + text = blob.text(-100) + assert_equal 464, text.lines.count + end + + def test_blob_text_default_encoding + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + assert_equal Encoding::UTF_8, blob.text.encoding + end + + def test_blob_text_set_encoding + oid = "7771329dfa3002caf8c61a0ceb62a31d09023f37" + blob = @repo.lookup(oid) + assert_equal Encoding::ASCII_8BIT, blob.text(0, Encoding::ASCII_8BIT).encoding + end end class BlobWriteTest < Rugged::TestCase include Rugged::TempRepositoryAccess @@ -27,8 +102,240 @@ blob = @repo.lookup(oid) assert_equal content, blob.read_raw.data end def test_write_blob_data - Rugged::Blob.create(@repo, "a new blob content") + assert_equal '1d83f106355e4309a293e42ad2a2c4b8bdbe77ae', + Rugged::Blob.from_buffer(@repo, "a new blob content") end + + def test_write_blob_from_workdir + assert_equal '1385f264afb75a56a5bec74243be9b367ba4ca08', + Rugged::Blob.from_workdir(@repo, "README") + end + + def test_write_blob_from_disk + file_path = File.join(TEST_DIR, (File.join('fixtures', 'archive.tar.gz'))) + File.open(file_path, 'rb') do |file| + oid = Rugged::Blob.from_disk(@repo, file.path) + assert oid + + blob = @repo.lookup(oid) + file.rewind + assert_equal file.read, blob.content + end + end + + def test_blob_is_binary + binary_file_path = File.join(TEST_DIR, (File.join('fixtures', 'archive.tar.gz'))) + binary_blob = @repo.lookup(Rugged::Blob.from_disk(@repo, binary_file_path)) + assert binary_blob.binary? + + text_file_path = File.join(TEST_DIR, (File.join('fixtures', 'text_file.md'))) + text_blob = @repo.lookup(Rugged::Blob.from_disk(@repo, text_file_path)) + refute text_blob.binary? + end +end + +class BlobDiffTest < Rugged::SandboxedTestCase + def test_diff_blob + repo = sandbox_init("diff") + + a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69") + b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10") + + blob = repo.lookup(a.tree["readme.txt"][:oid]) + other = repo.lookup(b.tree["readme.txt"][:oid]) + + patch = blob.diff(other) + + assert_equal :modified, patch.delta.status + + hunks = [] + patch.each_hunk do |hunk| + assert_instance_of Rugged::Diff::Hunk, hunk + hunks << hunk + end + assert_equal 3, hunks.size + + assert hunks[0].header.start_with? "@@ -1,4 +1,4 @@" + assert hunks[1].header.start_with? "@@ -7,10 +7,6 @@" + assert hunks[2].header.start_with? "@@ -24,12 +20,9 @@" + + lines = [] + hunks[0].each_line do |line| + lines << line + end + assert_equal 5, lines.size + + assert_equal :deletion, lines[0].line_origin + assert_equal "The Git feature that really makes it stand apart from nearly every other SCM\n", lines[0].content + + assert_equal :addition, lines[1].line_origin + assert_equal "The Git feature that r3ally mak3s it stand apart from n3arly 3v3ry other SCM\n", lines[1].content + + assert_equal :context, lines[2].line_origin + assert_equal "out there is its branching model.\n", lines[2].content + + assert_equal :context, lines[3].line_origin + assert_equal "\n", lines[3].content + + assert_equal :context, lines[4].line_origin + assert_equal "Git allows and encourages you to have multiple local branches that can be\n", lines[4].content + end + + def test_diff_string + repo = sandbox_init("diff") + + a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69") + b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10") + + blob = repo.lookup(a.tree["readme.txt"][:oid]) + other = repo.lookup(b.tree["readme.txt"][:oid]).content + + patch = blob.diff(other) + + assert_equal :modified, patch.delta.status + + hunks = [] + patch.each_hunk do |hunk| + assert_instance_of Rugged::Diff::Hunk, hunk + hunks << hunk + end + assert_equal 3, hunks.size + + assert hunks[0].header.start_with? "@@ -1,4 +1,4 @@" + assert hunks[1].header.start_with? "@@ -7,10 +7,6 @@" + assert hunks[2].header.start_with? "@@ -24,12 +20,9 @@" + + lines = [] + hunks[0].each_line do |line| + lines << line + end + assert_equal 5, lines.size + + assert_equal :deletion, lines[0].line_origin + assert_equal "The Git feature that really makes it stand apart from nearly every other SCM\n", lines[0].content + + assert_equal :addition, lines[1].line_origin + assert_equal "The Git feature that r3ally mak3s it stand apart from n3arly 3v3ry other SCM\n", lines[1].content + + assert_equal :context, lines[2].line_origin + assert_equal "out there is its branching model.\n", lines[2].content + + assert_equal :context, lines[3].line_origin + assert_equal "\n", lines[3].content + + assert_equal :context, lines[4].line_origin + assert_equal "Git allows and encourages you to have multiple local branches that can be\n", lines[4].content + end + + def test_diff_nil + repo = sandbox_init("diff") + + a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69") + + blob = repo.lookup(a.tree["readme.txt"][:oid]) + + patch = blob.diff(nil) + + assert_equal :deleted, patch.delta.status + + hunks = [] + patch.each_hunk do |hunk| + assert_instance_of Rugged::Diff::Hunk, hunk + hunks << hunk + end + assert_equal 1, hunks.size + + assert hunks[0].header.start_with? "@@ -1,35 +0,0 @@" + + lines = [] + hunks[0].each_line do |line| + lines << line + end + assert_equal 35, lines.size + + lines.each do |line| + assert_equal :deletion, line.line_origin + end + end + + def test_diff_with_paths + repo = sandbox_init("diff") + + a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69") + b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10") + + blob = repo.lookup(a.tree["readme.txt"][:oid]) + other = repo.lookup(b.tree["readme.txt"][:oid]) + + patch = blob.diff(other, :old_path => "old_readme.txt", :new_path => "new_readme.txt") + assert_equal "old_readme.txt", patch.delta.old_file[:path] + assert_equal "new_readme.txt", patch.delta.new_file[:path] + end +end + +class BlobCreateFromChunksTest < Rugged::TestCase + include Rugged::TempRepositoryAccess + + def test_write_blob_from_chunks_with_hintpath + file_path= File.join(TEST_DIR, (File.join('fixtures', 'archive.tar.gz'))) + File.open(file_path, 'rb') do |io| + oid = Rugged::Blob.from_chunks(@repo, io, 'archive.tar.gz2') + io.rewind + blob = @repo.lookup(oid) + assert_equal io.read, blob.content + end + end + + def test_write_blob_from_chunks_without_hintpath + file_path= File.join(TEST_DIR, (File.join('fixtures', 'archive.tar.gz'))) + File.open(file_path, 'rb') do |io| + oid = Rugged::Blob.from_chunks(@repo, io) + io.rewind + blob = @repo.lookup(oid) + assert_equal io.read, blob.content + end + end + + class BrokenIO + def read(length) + raise IOError + end + end + + def test_write_blob_from_chunks_broken_io + oid = Rugged::Blob.from_chunks(@repo, BrokenIO.new) + blob = @repo.lookup(oid) + assert_equal 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', + blob.oid + assert_equal 0, blob.size + end + + class OverflowIO + def initialize() + @called = false + end + + def read(size) + res = @called ? nil : 'a' * size * 4 + @called = true + res + end + end + + def test_write_blob_from_chunks_overflow_io + assert Rugged::Blob.from_chunks(@repo, OverflowIO.new) + end + + class BadIO + def read(length) + :invalid_data + end + end + + def test_write_blob_from_chunks_bad_io + assert Rugged::Blob.from_chunks(@repo, BadIO.new) + end + end