X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/df5c912a9eb5af7222e5446bc437ee97262542c8..7bbb0940b0398d17bb85fe410c3f23e4cd6ec8d6:/sdk/python/tests/test_collections.py diff --git a/sdk/python/tests/test_collections.py b/sdk/python/tests/test_collections.py index ac18c44c68..66f062c167 100644 --- a/sdk/python/tests/test_collections.py +++ b/sdk/python/tests/test_collections.py @@ -836,17 +836,58 @@ class CollectionOpenModes(run_test_server.TestCaseWithServers): with c.open('foo', 'wb') as f: f.write('foo') for mode in ['r', 'rt', 'r+', 'rt+', 'w', 'wt', 'a', 'at']: - if sys.version_info >= (3, 0): - with self.assertRaises(NotImplementedError): - c.open('foo', mode) - else: - with c.open('foo', mode) as f: - if mode[0] == 'r' and '+' not in mode: - self.assertEqual('foo', f.read(3)) - else: - f.write('bar') - f.seek(-3, os.SEEK_CUR) - self.assertEqual('bar', f.read(3)) + with c.open('foo', mode) as f: + if mode[0] == 'r' and '+' not in mode: + self.assertEqual('foo', f.read(3)) + else: + f.write('bar') + f.seek(0, os.SEEK_SET) + self.assertEqual('bar', f.read(3)) + + +class TextModes(run_test_server.TestCaseWithServers): + + def setUp(self): + arvados.config.KEEP_BLOCK_SIZE = 4 + if sys.version_info < (3, 0): + import unicodedata + self.sailboat = unicodedata.lookup('SAILBOAT') + self.snowman = unicodedata.lookup('SNOWMAN') + else: + self.sailboat = '\N{SAILBOAT}' + self.snowman = '\N{SNOWMAN}' + + def tearDown(self): + arvados.config.KEEP_BLOCK_SIZE = 2 ** 26 + + def test_read_sailboat_across_block_boundary(self): + c = Collection() + f = c.open('sailboats', 'wb') + data = self.sailboat.encode('utf-8') + f.write(data) + f.write(data[:1]) + f.write(data[1:]) + f.write(b'\n') + f.close() + self.assertRegex(c.portable_manifest_text(), r'\+4 .*\+3 ') + + f = c.open('sailboats', 'r') + string = f.readline() + self.assertEqual(string, self.sailboat+self.sailboat+'\n') + f.close() + + def test_write_snowman_across_block_boundary(self): + c = Collection() + f = c.open('snowmany', 'w') + data = self.snowman + f.write(data+data+'\n'+data+'\n') + f.close() + self.assertRegex(c.portable_manifest_text(), r'\+4 .*\+4 .*\+3 ') + + f = c.open('snowmany', 'r') + self.assertEqual(f.readline(), self.snowman+self.snowman+'\n') + self.assertEqual(f.readline(), self.snowman+'\n') + f.close() class NewCollectionTestCase(unittest.TestCase, CollectionTestMixin): @@ -911,10 +952,49 @@ class NewCollectionTestCase(unittest.TestCase, CollectionTestMixin): self.assertIs(c.find("./nonexistant.txt"), None) self.assertIs(c.find("./nonexistantsubdir/nonexistant.txt"), None) + def test_escaped_paths_dont_get_unescaped_on_manifest(self): + # Dir & file names are literally '\056' (escaped form: \134056) + manifest = './\\134056\\040Test d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\134056\n' + c = Collection(manifest) + self.assertEqual(c.portable_manifest_text(), manifest) + + def test_other_special_chars_on_file_token(self): + cases = [ + ('\\000', '\0'), + ('\\011', '\t'), + ('\\012', '\n'), + ('\\072', ':'), + ('\\134400', '\\400'), + ] + for encoded, decoded in cases: + manifest = '. d41d8cd98f00b204e9800998ecf8427e+0 0:0:some%sfile.txt\n' % encoded + c = Collection(manifest) + self.assertEqual(c.portable_manifest_text(), manifest) + self.assertIn('some%sfile.txt' % decoded, c.keys()) + + def test_escaped_paths_do_get_unescaped_on_listing(self): + # Dir & file names are literally '\056' (escaped form: \134056) + manifest = './\\134056\\040Test d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\134056\n' + c = Collection(manifest) + self.assertIn('\\056 Test', c.keys()) + self.assertIn('\\056', c['\\056 Test'].keys()) + + def test_make_empty_dir_with_escaped_chars(self): + c = Collection() + c.mkdirs('./Empty\\056Dir') + self.assertEqual(c.portable_manifest_text(), + './Empty\\134056Dir d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\056\n') + + def test_make_empty_dir_with_spaces(self): + c = Collection() + c.mkdirs('./foo bar/baz waz') + self.assertEqual(c.portable_manifest_text(), + './foo\\040bar/baz\\040waz d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\056\n') + def test_remove_in_subdir(self): c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n') c.remove("foo/count2.txt") - self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text()) + self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\056\n", c.portable_manifest_text()) def test_remove_empty_subdir(self): c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')