Skip to content

Commit

Permalink
Port test_file to Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
garyvdm committed Nov 21, 2014
1 parent b5a3921 commit 325e5be
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions dulwich/tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,16 @@
SkipTest,
TestCase,
)
from dulwich.tests.utils import (
skipIfPY3
)


@skipIfPY3
class FancyRenameTests(TestCase):

def setUp(self):
super(FancyRenameTests, self).setUp()
self._tempdir = tempfile.mkdtemp()
self.foo = self.path('foo')
self.bar = self.path('bar')
self.create(self.foo, 'foo contents')
self.create(self.foo, b'foo contents')

def tearDown(self):
shutil.rmtree(self._tempdir)
Expand All @@ -61,44 +57,43 @@ def test_no_dest_exists(self):
self.assertFalse(os.path.exists(self.foo))

new_f = open(self.bar, 'rb')
self.assertEqual('foo contents', new_f.read())
self.assertEqual(b'foo contents', new_f.read())
new_f.close()

def test_dest_exists(self):
self.create(self.bar, 'bar contents')
self.create(self.bar, b'bar contents')
fancy_rename(self.foo, self.bar)
self.assertFalse(os.path.exists(self.foo))

new_f = open(self.bar, 'rb')
self.assertEqual('foo contents', new_f.read())
self.assertEqual(b'foo contents', new_f.read())
new_f.close()

def test_dest_opened(self):
if sys.platform != "win32":
raise SkipTest("platform allows overwriting open files")
self.create(self.bar, 'bar contents')
self.create(self.bar, b'bar contents')
dest_f = open(self.bar, 'rb')
self.assertRaises(OSError, fancy_rename, self.foo, self.bar)
dest_f.close()
self.assertTrue(os.path.exists(self.path('foo')))

new_f = open(self.foo, 'rb')
self.assertEqual('foo contents', new_f.read())
self.assertEqual(b'foo contents', new_f.read())
new_f.close()

new_f = open(self.bar, 'rb')
self.assertEqual('bar contents', new_f.read())
self.assertEqual(b'bar contents', new_f.read())
new_f.close()


@skipIfPY3
class GitFileTests(TestCase):

def setUp(self):
super(GitFileTests, self).setUp()
self._tempdir = tempfile.mkdtemp()
f = open(self.path('foo'), 'wb')
f.write('foo contents')
f.write(b'foo contents')
f.close()

def tearDown(self):
Expand All @@ -119,23 +114,23 @@ def test_invalid(self):
def test_readonly(self):
f = GitFile(self.path('foo'), 'rb')
self.assertTrue(isinstance(f, io.IOBase))
self.assertEqual('foo contents', f.read())
self.assertEqual('', f.read())
self.assertEqual(b'foo contents', f.read())
self.assertEqual(b'', f.read())
f.seek(4)
self.assertEqual('contents', f.read())
self.assertEqual(b'contents', f.read())
f.close()

def test_default_mode(self):
f = GitFile(self.path('foo'))
self.assertEqual('foo contents', f.read())
self.assertEqual(b'foo contents', f.read())
f.close()

def test_write(self):
foo = self.path('foo')
foo_lock = '%s.lock' % foo

orig_f = open(foo, 'rb')
self.assertEqual(orig_f.read(), 'foo contents')
self.assertEqual(orig_f.read(), b'foo contents')
orig_f.close()

self.assertFalse(os.path.exists(foo_lock))
Expand All @@ -144,51 +139,51 @@ def test_write(self):
self.assertRaises(AttributeError, getattr, f, 'not_a_file_property')

self.assertTrue(os.path.exists(foo_lock))
f.write('new stuff')
f.write(b'new stuff')
f.seek(4)
f.write('contents')
f.write(b'contents')
f.close()
self.assertFalse(os.path.exists(foo_lock))

new_f = open(foo, 'rb')
self.assertEqual('new contents', new_f.read())
self.assertEqual(b'new contents', new_f.read())
new_f.close()

def test_open_twice(self):
foo = self.path('foo')
f1 = GitFile(foo, 'wb')
f1.write('new')
f1.write(b'new')
try:
f2 = GitFile(foo, 'wb')
self.fail()
except OSError as e:
self.assertEqual(errno.EEXIST, e.errno)
else:
f2.close()
f1.write(' contents')
f1.write(b' contents')
f1.close()

# Ensure trying to open twice doesn't affect original.
f = open(foo, 'rb')
self.assertEqual('new contents', f.read())
self.assertEqual(b'new contents', f.read())
f.close()

def test_abort(self):
foo = self.path('foo')
foo_lock = '%s.lock' % foo

orig_f = open(foo, 'rb')
self.assertEqual(orig_f.read(), 'foo contents')
self.assertEqual(orig_f.read(), b'foo contents')
orig_f.close()

f = GitFile(foo, 'wb')
f.write('new contents')
f.write(b'new contents')
f.abort()
self.assertTrue(f.closed)
self.assertFalse(os.path.exists(foo_lock))

new_orig_f = open(foo, 'rb')
self.assertEqual(new_orig_f.read(), 'foo contents')
self.assertEqual(new_orig_f.read(), b'foo contents')
new_orig_f.close()

def test_abort_close(self):
Expand Down

0 comments on commit 325e5be

Please sign in to comment.