Пример #1
0
    def test_read_yaml(self, content, truth):
        with closeable_tempfile(mode="wt", encoding="utf-8") as (f, fname):
            f.write(content)
            f.flush()  # or f.close()
            result = read_yaml(fname)

        self.assertEqual(truth, result)
Пример #2
0
 def test_closeable_tempfile_eoferror(self):
     name = None
     with self.assertRaises(EOFError):
         with closeable_tempfile() as (f, name):
             name = name
             raise EOFError
     self.assertFalse(os.path.exists(name))
Пример #3
0
    def test_read_json_lines(self, content, truth):
        with closeable_tempfile(mode="wt", encoding="utf-8") as (f, fname):
            f.write(content)
            f.close()  # Windows compatibility, otherwise sharing violation
            result = tuple(read_json_lines(fname))

        self.assertEqual(truth, result)
Пример #4
0
 def test_closeable_tempfile_rw_close(self):
     truth = b"asd"
     with closeable_tempfile() as (f, name):
         f.write(truth)
         f.close()
         with open(name, "rb") as fr:
             self.assertEqual(truth, fr.read())
     self.assertFalse(os.path.exists(name))
Пример #5
0
 def test_closeable_tempfile_ctrlc(self):
     name = None
     with self.assertRaises(KeyboardInterrupt):
         with closeable_tempfile() as (f, name):
             name = name
             input(
                 "Press ctrl-c to continue"
             )  # this is pretty broken, it first raises EOFError and then shortly after KeyboardInterrupt
     self.assertFalse(os.path.exists(name))
Пример #6
0
    def test_write_yaml(self, content, truth):
        with closeable_tempfile(mode="w+t", encoding="utf-8") as (f, fname):
            write_yaml(content, fname)
            result = f.read()

        self.assertEqual(truth, result)