import os.path
import unittest
import tempfile
import shutil

from ..TestUtils import write_file, write_newer_file


class TestTestUtils(unittest.TestCase):
    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self._test_path = lambda filename: os.path.join(self.temp_dir, filename)

    def tearDown(self):
        if self.temp_dir and os.path.isdir(self.temp_dir):
            shutil.rmtree(self.temp_dir)

    def _test_write_file(self, content, expected, **kwargs):
        file_path = self._test_path("abcfile")
        write_file(file_path, content)
        assert os.path.isfile(file_path)

        with open(file_path, 'rb') as f:
            found = f.read()
        assert found == expected, repr(found)

    def test_write_file_text(self):
        self._test_write_file("abcüöä", b'abc\xc3\xbc\xc3\xb6\xc3\xa4')

    def test_write_file_bytes(self):
        self._test_write_file(b"ab\0c", b"ab\0c")

    def test_write_newer_file(self):
        file_path_1 = self._test_path("abcfile1.txt")
        file_path_2 = self._test_path("abcfile2.txt")
        write_file(file_path_1, "abc")
        assert os.path.isfile(file_path_1)
        write_newer_file(file_path_2, file_path_1, "xyz")
        assert os.path.isfile(file_path_2)
        assert os.path.getmtime(file_path_2) > os.path.getmtime(file_path_1)

    def test_write_newer_file_same(self):
        file_path = self._test_path("abcfile.txt")
        write_file(file_path, "abc")
        mtime = os.path.getmtime(file_path)
        write_newer_file(file_path, file_path, "xyz")
        assert os.path.getmtime(file_path) > mtime

    def test_write_newer_file_fresh(self):
        file_path = self._test_path("abcfile.txt")
        assert not os.path.exists(file_path)
        write_newer_file(file_path, file_path, "xyz")
        assert os.path.isfile(file_path)

# -*- coding: utf-8 -*-

import os.path
import unittest
import tempfile
import shutil

from ..TestUtils import write_file, write_newer_file


class TestTestUtils(unittest.TestCase):
    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self._test_path = lambda filename: os.path.join(self.temp_dir, filename)

    def tearDown(self):
        if self.temp_dir and os.path.isdir(self.temp_dir):
            shutil.rmtree(self.temp_dir)

    def _test_write_file(self, content, expected, **kwargs):
        file_path = self._test_path("abcfile")
        write_file(file_path, content)
        assert os.path.isfile(file_path)

        with open(file_path, 'rb') as f:
            found = f.read()
        assert found == expected, repr(found)

    def test_write_file_text(self):
        text = u"abcüöä"
        self._test_write_file(text, text.encode('utf8'))

    def test_write_file_bytes(self):
        self._test_write_file(b"ab\0c", b"ab\0c")

    def test_write_newer_file(self):
        file_path_1 = self._test_path("abcfile1.txt")
        file_path_2 = self._test_path("abcfile2.txt")
        write_file(file_path_1, "abc")
        assert os.path.isfile(file_path_1)
        write_newer_file(file_path_2, file_path_1, "xyz")
        assert os.path.isfile(file_path_2)
        assert os.path.getmtime(file_path_2) > os.path.getmtime(file_path_1)

    def test_write_newer_file_same(self):
        file_path = self._test_path("abcfile.txt")
        write_file(file_path, "abc")
        mtime = os.path.getmtime(file_path)
        write_newer_file(file_path, file_path, "xyz")
        assert os.path.getmtime(file_path) > mtime

    def test_write_newer_file_fresh(self):
        file_path = self._test_path("abcfile.txt")
        assert not os.path.exists(file_path)
        write_newer_file(file_path, file_path, "xyz")
        assert os.path.isfile(file_path)

Fix test in Py2.