def test_simple(self):
        """Test whether the file content is correctly written when using string."""
        name = self.filename
        safe_write(name, "foo")

        with open(name) as fp:
            self.assertEqual(fp.read(), "foo")
    def test_simple_callable(self):
        """Test whether the file content is correctly written when using callable."""
        name = self.filename
        safe_write(name, lambda fp: fp.write("foo"))

        with open(name) as fp:
            self.assertEqual(fp.read(), "foo")
Beispiel #3
0
    def test_simple_callable(self):
        """Test whether the file content is correctly written when using callable."""
        name = self.filename
        safe_write(name, lambda fp: fp.write("foo"))

        with open(name) as fp:
            self.assertEqual(fp.read(), "foo")
Beispiel #4
0
    def test_already_exists(self):
        """Test whether safe_write works when the target already exists."""
        safe_write(self.filename, "foo")
        safe_write(self.filename, "foo")

        with open(self.filename) as fp:
            self.assertEqual(fp.read(), "foo")
Beispiel #5
0
    def test_simple(self):
        """Test whether the file content is correctly written when using string."""
        name = self.filename
        safe_write(name, "foo")

        with open(name) as fp:
            self.assertEqual(fp.read(), "foo")
    def test_already_exists(self):
        """Test whether safe_write works when the target already exists."""
        safe_write(self.filename, "foo")
        safe_write(self.filename, "foo")

        with open(self.filename) as fp:
            self.assertEqual(fp.read(), "foo")
    def test_simple_error(self):
        """Test whether the file content is not half written if error happens."""
        name = self.filename
        safe_write(name, "foo")

        def simulate_interrupted(fp):
            fp.write("bar")
            raise KeyboardInterrupt()
            fp.write("foo")

        self.assertRaises(KeyboardInterrupt, safe_write, name, simulate_interrupted)

        with open(name) as fp:
            self.assertEqual(fp.read(), "foo")
Beispiel #8
0
    def test_simple_error(self):
        """Test whether the file content is not half written if error happens."""
        name = self.filename
        safe_write(name, "foo")

        def simulate_interrupted(fp):
            fp.write("bar")
            raise KeyboardInterrupt()
            fp.write("foo")

        self.assertRaises(KeyboardInterrupt, safe_write, name,
                          simulate_interrupted)

        with open(name) as fp:
            self.assertEqual(fp.read(), "foo")