def test_fd(self): path = os.path.join(self.temp_dir.name, "testfile") with open(path, "wb") as f: fd = f.fileno() stat_before = os.fstat(fd) with kas._open_file(fd, "wb") as f: f.write(b"xyz") stat_after = os.fstat(fd) self.assertEqual(stat_before.st_mode, stat_after.st_mode) with open(path, "rb") as f: self.assertEqual(f.read(), b"xyz")
def test_non_file(self): for bad_type in [None, {}, []]: with self.assertRaises(TypeError): with kas._open_file(bad_type, "wb"): pass
def test_bytes_io(self): buff = io.BytesIO() with kas._open_file(buff, "wb") as f: f.write(b"xyz") self.assertEqual(buff.getvalue(), b"xyz")
def test_string_io(self): buff = io.StringIO() with kas._open_file(buff, "w") as f: f.write("xyz") self.assertEqual(buff.getvalue(), "xyz")
def test_temp_file(self): with tempfile.TemporaryFile() as f_input: with kas._open_file(f_input, "wb") as f: f.write(b"xyz") f_input.seek(0) self.assertEqual(f_input.read(), b"xyz")
def verify_path(self, path): with kas._open_file(path, "w") as f: f.write("xyz") with open(path) as f: self.assertEqual(f.read(), "xyz")