|
11 | 11 | import os |
12 | 12 | import random |
13 | 13 | import re |
| 14 | +import six |
14 | 15 | import socket |
15 | 16 | import stat |
16 | 17 | import string |
@@ -2001,6 +2002,41 @@ def test_settings_load_and_save_471(self): |
2001 | 2002 | config.load(**load_logging_options) |
2002 | 2003 | self.assertTrue(config.data_objects.auto_close, RANDOM_VALUE - i - 1) |
2003 | 2004 |
|
| 2005 | + def test_append_mode_will_append_to_data_object__issue_495(self): |
| 2006 | + append_string = b'to_be_written'.lower() |
| 2007 | + reverse_bytes = lambda s: ''.join(reversed(s)) if six.PY2 else bytes(reversed(s)) |
| 2008 | + session, data = (self.sess, self.sess.data_objects) |
| 2009 | + testfile = '{}/issue_495'.format(helpers.home_collection(session)) |
| 2010 | + # Make sure data object doesn't exist. |
| 2011 | + self.assertFalse(data.exists(testfile)) |
| 2012 | + try: |
| 2013 | + # Append to a previously nonexistent data object. |
| 2014 | + with data.open(testfile,'a') as f: |
| 2015 | + f.write(append_string.upper()) |
| 2016 | + # Test that the data object, once opened, has the right offset. Then perform a second append. |
| 2017 | + with data.open(testfile,'a+') as f: |
| 2018 | + self.assertTrue(f.tell(), len(append_string)) |
| 2019 | + f.write(append_string) |
| 2020 | + # Verify original content written at offset 0. |
| 2021 | + with data.open(testfile,'r') as f: |
| 2022 | + self.assertEqual(f.read(), append_string.upper()+append_string) |
| 2023 | + # Do an open for read/write (in particular, not appending); then perform a write. |
| 2024 | + # (This overwrites the original content at offset 0.) |
| 2025 | + with data.open(testfile,'r+') as f: |
| 2026 | + f.write(reverse_bytes(append_string)) |
| 2027 | + # Final test of data object content: |
| 2028 | + # Test that (1) the non-appending write has modified the first half of the content, and |
| 2029 | + # (2) the second append has placed the written content in the right location. |
| 2030 | + with data.open(testfile,'r') as f: |
| 2031 | + f.seek(0,io.SEEK_END) |
| 2032 | + self.assertEqual(f.tell(), 2*len(append_string)) |
| 2033 | + f.seek(0) |
| 2034 | + self.assertEqual(f.read(), reverse_bytes(append_string) + append_string) |
| 2035 | + finally: |
| 2036 | + if data.exists(testfile): |
| 2037 | + data.unlink(testfile,force=True) |
| 2038 | + |
| 2039 | + |
2004 | 2040 | if __name__ == '__main__': |
2005 | 2041 | # let the tests find the parent irods lib |
2006 | 2042 | sys.path.insert(0, os.path.abspath('../..')) |
|
0 commit comments