def test_unicode(self): t = File(self.path) with t.open('w') as b: b.write(u"bar") with t.open('r') as b: self.assertEqual(b.read(), u'bar')
def test_close(self): t = File(self.path) p = t.open('w') print('test', file=p) self.assertFalse(os.path.exists(self.path)) p.close() self.assertTrue(os.path.exists(self.path))
def test_close(self): t = File(self.path) p = t.open('w') print >> p, 'test' self.assertFalse(os.path.exists(self.path)) p.close() self.assertTrue(os.path.exists(self.path))
def test_write_cleanup_with_error(self): t = File(self.path) try: with t.open('w'): raise Exception('something broke') except: pass self.assertFalse(t.exists())
def test_text(self): t = File(self.path, luigi.format.UTF8) a = u'我éçф' with t.open('w') as f: f.write(a) with t.open('r') as f: b = f.read() self.assertEqual(a, b)
def test_write_cleanup_no_close(self): t = File(self.path) def context(): f = t.open('w') f.write('stuff') context() gc.collect() # force garbage collection of f variable self.assertFalse(t.exists())
def test_del(self): t = File(self.path) p = t.open('w') print('test', file=p) tp = p.tmp_path del p self.assertFalse(os.path.exists(tp)) self.assertFalse(os.path.exists(self.path))
def test_del(self): t = File(self.path) p = t.open('w') print >> p, 'test' tp = p.tmp_path del p self.assertFalse(os.path.exists(tp)) self.assertFalse(os.path.exists(self.path))
def test_del_with_Text(self): t = File(self.path, luigi.format.UTF8) p = t.open('w') print(u'test', file=p) tp = p.tmp_path del p self.assertFalse(os.path.exists(tp)) self.assertFalse(os.path.exists(self.path))
def test_move(self): t = File(self.path) f = t.open('w') test_data = 'test' f.write(test_data) f.close() self.assertTrue(os.path.exists(self.path)) self.assertFalse(os.path.exists(self.copy)) t.move(self.copy) self.assertFalse(os.path.exists(self.path)) self.assertTrue(os.path.exists(self.copy))
def test_format_chain_reverse(self): t = File(self.path, luigi.format.UTF8 >> luigi.format.Gzip) f = gzip.open(self.path, 'wb') f.write(b'\xe6\x88\x91\xc3\xa9\r\n\xc3\xa7\xd1\x84') f.close() with t.open('r') as f: b = f.read() self.assertEqual(u'我é\nçф', b)
def test_copy(self): t = File(self.path) f = t.open('w') test_data = 'test' f.write(test_data) f.close() self.assertTrue(os.path.exists(self.path)) self.assertFalse(os.path.exists(self.copy)) t.copy(self.copy) self.assertTrue(os.path.exists(self.path)) self.assertTrue(os.path.exists(self.copy)) self.assertEqual(t.open('r').read(), File(self.copy).open('r').read())
def test_tmp(self): t = File(is_tmp=True) p = t.open('w') print >> p, 'test' p.close() path = t.path self.assertTrue(os.path.exists(path)) q = t.open('r') self.assertEqual(q.readline(), 'test\n') q.close() del t # should remove the underlying file self.assertFalse(os.path.exists(path))
def test_format_chain(self): UTF8WIN = luigi.format.TextFormat(encoding='utf8', newline='\r\n') t = File(self.path, UTF8WIN >> luigi.format.Gzip) a = u'我é\nçф' with t.open('w') as f: f.write(a) f = gzip.open(self.path, 'rb') b = f.read() f.close() self.assertEqual(b'\xe6\x88\x91\xc3\xa9\r\n\xc3\xa7\xd1\x84', b)
def test_format_newline(self): t = File(self.path, luigi.format.SysNewLine) with t.open('w') as f: f.write(b'a\rb\nc\r\nd') with t.open('r') as f: b = f.read() with open(self.path, 'rb') as f: c = f.read() self.assertEqual(b'a\nb\nc\nd', b) self.assertEqual(b'a\r\nb\r\nc\r\nd', c)
def test_format_injection(self): class CustomFormat(luigi.format.Format): def pipe_reader(self, input_pipe): input_pipe.foo = "custom read property" return input_pipe def pipe_writer(self, output_pipe): output_pipe.foo = "custom write property" return output_pipe t = File(self.path, format=CustomFormat()) with t.open("w") as f: self.assertEqual(f.foo, "custom write property") with t.open("r") as f: self.assertEqual(f.foo, "custom read property")
def test_gzip(self): t = File(self.path, luigi.format.Gzip) p = t.open('w') test_data = b'test' p.write(test_data) print(self.path) self.assertFalse(os.path.exists(self.path)) p.close() self.assertTrue(os.path.exists(self.path)) # Using gzip module as validation f = gzip.open(self.path, 'r') self.assertTrue(test_data == f.read()) f.close() # Verifying our own gzip reader f = File(self.path, luigi.format.Gzip).open('r') self.assertTrue(test_data == f.read()) f.close()
def test_bzip2(self): t = File(self.path, luigi.format.Bzip2) p = t.open('w') test_data = b'test' p.write(test_data) print(self.path) self.assertFalse(os.path.exists(self.path)) p.close() self.assertTrue(os.path.exists(self.path)) # Using bzip module as validation f = bz2.BZ2File(self.path, 'r') self.assertTrue(test_data == f.read()) f.close() # Verifying our own bzip2 reader f = File(self.path, luigi.format.Bzip2).open('r') self.assertTrue(test_data == f.read()) f.close()
def test_gzip(self): t = File(self.path, luigi.format.Gzip) p = t.open('w') test_data = 'test' p.write(test_data) print self.path self.assertFalse(os.path.exists(self.path)) p.close() self.assertTrue(os.path.exists(self.path)) # Using gzip module as validation f = gzip.open(self.path, 'rb') self.assertTrue(test_data == f.read()) f.close() # Verifying our own gzip reader f = File(self.path, luigi.format.Gzip).open('r') self.assertTrue(test_data == f.read()) f.close()
def test_bzip2(self): t = File(self.path, luigi.format.Bzip2) p = t.open('w') test_data = 'test' p.write(test_data) print self.path self.assertFalse(os.path.exists(self.path)) p.close() self.assertTrue(os.path.exists(self.path)) # Using bzip module as validation f = bz2.BZ2File(self.path, 'rb') self.assertTrue(test_data == f.read()) f.close() # Verifying our own bzip2 reader f = File(self.path, luigi.format.Bzip2).open('r') self.assertTrue(test_data == f.read()) f.close()