def test_read_and_write_file(self): fs = FileSystem() text_path = None binary_path = None unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D' hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD' malformed_text_hex = '\x4D\x69\x63\x72\x6F\x73\x6F\x66\x74\xAE\x20\x56\x69\x73\x75\x61\x6C\x20\x53\x74\x75\x64\x69\x6F\xAE\x20\x32\x30\x31\x30\x0D\x0A' malformed_ignored_text_hex = '\x4D\x69\x63\x72\x6F\x73\x6F\x66\x74\x20\x56\x69\x73\x75\x61\x6C\x20\x53\x74\x75\x64\x69\x6F\x20\x32\x30\x31\x30\x0D\x0A' try: text_path = tempfile.mktemp(prefix='tree_unittest_') binary_path = tempfile.mktemp(prefix='tree_unittest_') fs.write_text_file(text_path, unicode_text_string) contents = fs.read_binary_file(text_path) self.assertEqual(contents, hex_equivalent) fs.write_binary_file(binary_path, hex_equivalent) text_contents = fs.read_text_file(binary_path) self.assertEqual(text_contents, unicode_text_string) self.assertRaises(ValueError, fs.write_text_file, binary_path, malformed_text_hex) fs.write_binary_file(binary_path, malformed_text_hex) self.assertRaises(ValueError, fs.read_text_file, binary_path) text_contents = fs.read_binary_file(binary_path).decode( 'utf8', 'ignore') self.assertEquals(text_contents, malformed_ignored_text_hex) fs.open_text_file_for_reading(binary_path, 'replace').readline() finally: if text_path and fs.isfile(text_path): os.remove(text_path) if binary_path and fs.isfile(binary_path): os.remove(binary_path)
def test_read_and_write_file(self): fs = FileSystem() text_path = None binary_path = None unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D' hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD' malformed_text_hex = '\x4D\x69\x63\x72\x6F\x73\x6F\x66\x74\xAE\x20\x56\x69\x73\x75\x61\x6C\x20\x53\x74\x75\x64\x69\x6F\xAE\x20\x32\x30\x31\x30\x0D\x0A' malformed_ignored_text_hex = '\x4D\x69\x63\x72\x6F\x73\x6F\x66\x74\x20\x56\x69\x73\x75\x61\x6C\x20\x53\x74\x75\x64\x69\x6F\x20\x32\x30\x31\x30\x0D\x0A' try: text_path = tempfile.mktemp(prefix='tree_unittest_') binary_path = tempfile.mktemp(prefix='tree_unittest_') fs.write_text_file(text_path, unicode_text_string) contents = fs.read_binary_file(text_path) self.assertEqual(contents, hex_equivalent) fs.write_binary_file(binary_path, hex_equivalent) text_contents = fs.read_text_file(binary_path) self.assertEqual(text_contents, unicode_text_string) self.assertRaises(ValueError, fs.write_text_file, binary_path, malformed_text_hex) fs.write_binary_file(binary_path, malformed_text_hex) self.assertRaises(ValueError, fs.read_text_file, binary_path) text_contents = fs.read_binary_file(binary_path).decode('utf8', 'ignore') self.assertEquals(text_contents, malformed_ignored_text_hex) fs.open_text_file_for_reading(binary_path, 'replace').readline() finally: if text_path and fs.isfile(text_path): os.remove(text_path) if binary_path and fs.isfile(binary_path): os.remove(binary_path)
def test_append_to_text_file(self): fs = FileSystem() text_path = None unicode_text_string1 = u'\u016An\u012Dc\u014Dde\u033D' unicode_text_string2 = 'Hello' try: text_path = tempfile.mktemp(prefix='tree_unittest_') file = fs.open_text_file_for_writing(text_path) file.write(unicode_text_string1) file.close() file = fs.open_text_file_for_writing(text_path, should_append=True) file.write(unicode_text_string2) file.close() file = fs.open_text_file_for_reading(text_path) read_text = file.read() file.close() self.assertEqual(read_text, unicode_text_string1 + unicode_text_string2) finally: if text_path and fs.isfile(text_path): os.remove(text_path)
def test_write_text_file_unicode_encode_error(self): fs = FileSystem() text_path = None try: text_path = tempfile.mktemp(prefix='write_text_unittest_') bin_path = tempfile.mktemp(prefix='write_bin_unittest_') fs.write_binary_file(bin_path, bytearray(b'\x73\x74\x72\x8b')) data_to_write = fs.read_binary_file(bin_path) self.assertRaises(UnicodeDecodeError, fs.write_text_file, text_path, data_to_write) fs.write_text_file(text_path, data_to_write, 'replace') self.assertEqual(u'str\ufffd', fs.read_text_file(text_path)) fs.write_text_file(text_path, data_to_write, 'ignore') self.assertEqual('str', fs.read_text_file(text_path)) finally: if text_path and fs.isfile(text_path): os.remove(text_path) if bin_path and fs.isfile(bin_path): os.remove(bin_path)
def test_read_and_write_file(self): fs = FileSystem() text_path = None binary_path = None unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D' hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD' try: text_path = tempfile.mktemp(prefix='tree_unittest_') binary_path = tempfile.mktemp(prefix='tree_unittest_') fs.write_text_file(text_path, unicode_text_string) contents = fs.read_binary_file(text_path) self.assertEqual(contents, hex_equivalent) fs.write_binary_file(binary_path, hex_equivalent) text_contents = fs.read_text_file(binary_path) self.assertEqual(text_contents, unicode_text_string) finally: if text_path and fs.isfile(text_path): os.remove(text_path) if binary_path and fs.isfile(binary_path): os.remove(binary_path)
def test_read_and_write_text_file(self): fs = FileSystem() text_path = None unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D' hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD' try: text_path = tempfile.mktemp(prefix='tree_unittest_') file = fs.open_text_file_for_writing(text_path) file.write(unicode_text_string) file.close() file = fs.open_text_file_for_reading(text_path) read_text = file.read() file.close() self.assertEqual(read_text, unicode_text_string) finally: if text_path and fs.isfile(text_path): os.remove(text_path)