From ca81bca5c55d75dcc397eda2f4b0b77e66fbcfbc Mon Sep 17 00:00:00 2001 From: Oliver Palmer Date: Wed, 6 Jan 2016 18:03:59 -0500 Subject: [PATCH 1/4] remove unused tempdir(), tempfile() and configure() from testutil --- pywincffi/core/testutil.py | 35 +++------------------------------- tests/test_core/test_config.py | 11 +++++++---- 2 files changed, 10 insertions(+), 36 deletions(-) diff --git a/pywincffi/core/testutil.py b/pywincffi/core/testutil.py index c04f462..3f813fb 100644 --- a/pywincffi/core/testutil.py +++ b/pywincffi/core/testutil.py @@ -105,11 +105,13 @@ def setUp(self): # test does not causes an error to be raised in another. self.SetLastError(0) - self.configure(config) + config.load() # pylint: disable=invalid-name def SetLastError(self, value=0, lib=None): """Calls the Windows API function SetLastError()""" + self.failIf(os.name != "nt") + if lib is None: lib = libtest @@ -120,34 +122,3 @@ def SetLastError(self, value=0, lib=None): self.fail("Expected int for `value`") return lib.SetLastError(ffi.cast("DWORD", value)) - - def configure(self, config_object): # pylint: disable=no-self-use - """Sets up the configuration for the test""" - config_object.load() - - def tempdir(self): - """ - Creates a temporary directory and returns the path. Best attempts - will be made to cleanup the directory at the end of the test run. - """ - path = tempfile.mkdtemp() - self.addCleanup(remove, path) - return path - - def tempfile(self, data=None): - """ - Creates a temporary file and returns the path. Best attempts - will be made to cleanup the file at the end of the test run. - - :param str data: - Optionally write the provided data to the file on disk. - """ - fd, path = tempfile.mkstemp() - if data is None: - os.close(fd) - else: - with os.fdopen(fd, "w") as file_: - file_.write(data) - - self.addCleanup(remove, path) - return path diff --git a/tests/test_core/test_config.py b/tests/test_core/test_config.py index e687c4f..ba9df09 100644 --- a/tests/test_core/test_config.py +++ b/tests/test_core/test_config.py @@ -2,6 +2,7 @@ import logging import os +import tempfile from textwrap import dedent from os.path import isfile, join, expanduser @@ -99,13 +100,15 @@ def test_contains_override_path_local(self): self.assertIn(path, Configuration.FILES) def test_loads_override(self): - paths = ( - Configuration.FILES[0], - self.tempfile(data=dedent(""" + fd, path = tempfile.mkstemp() + self.addCleanup(os.remove, path) + with os.fdopen(fd, "w") as file_: + file_.write(dedent(""" [pywincffi] log_level=-1 """)) - ) + + paths = (Configuration.FILES[0], path) with patch.object(Configuration, "FILES", paths): config = Configuration() self.assertEqual(config.getint("pywincffi", "log_level"), -1) From fe3a901a82bbd8fd8e4c0e617bd4864de4dd8cbf Mon Sep 17 00:00:00 2001 From: Oliver Palmer Date: Wed, 6 Jan 2016 18:04:51 -0500 Subject: [PATCH 2/4] remove unused c_file(), remove() from testutil --- pywincffi/core/testutil.py | 49 +------------------------------------- 1 file changed, 1 insertion(+), 48 deletions(-) diff --git a/pywincffi/core/testutil.py b/pywincffi/core/testutil.py index 3f813fb..f52e7fb 100644 --- a/pywincffi/core/testutil.py +++ b/pywincffi/core/testutil.py @@ -46,53 +46,6 @@ logger.warning("Failed to build SetLastError()") -def remove(path, onexit=True): - """ - Removes the request ``path`` from disk. This is meant to - be used as a cleanup function by a test case. - - :param str path: - The file or directory to remove. - - :param bool onexit: - If we can't remove the request ``path``, try again - when Python exists to remove it. - """ - if isdir(path): - try: - shutil.rmtree(path) - except (WindowsError, OSError, IOError) as error: - if error.errno == ENOENT: - return - if error.errno in (EACCES, EAGAIN, EIO) and onexit: - atexit.register(remove, path, onexit=False) - - elif isfile(path): - try: - os.remove(path) - except (WindowsError, OSError, IOError) as error: - if error.errno == ENOENT: - return - if error.errno in (EACCES, EAGAIN, EIO) and onexit: - atexit.register(remove, path, onexit=False) - - -def c_file(path): - """ - A generator which yields lines from a C header or source - file. - - :param str path: - The filepath to read from. - """ - with open(path, "r") as header: - for line in header: - line = line.strip() - if not line or line.startswith("//"): - continue - yield line - - class TestCase(_TestCase): """ A base class for all test cases. By default the @@ -111,7 +64,7 @@ def setUp(self): def SetLastError(self, value=0, lib=None): """Calls the Windows API function SetLastError()""" self.failIf(os.name != "nt") - + if lib is None: lib = libtest From 72ed6596cb9f46e7ad6997b6fc441ba24ef157d2 Mon Sep 17 00:00:00 2001 From: Oliver Palmer Date: Wed, 6 Jan 2016 18:09:22 -0500 Subject: [PATCH 3/4] removing unused imports --- pywincffi/core/testutil.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pywincffi/core/testutil.py b/pywincffi/core/testutil.py index f52e7fb..d4f758b 100644 --- a/pywincffi/core/testutil.py +++ b/pywincffi/core/testutil.py @@ -5,12 +5,7 @@ This module is used by the unittests. """ -import atexit import os -import shutil -import tempfile -from errno import ENOENT, EACCES, EAGAIN, EIO -from os.path import isfile, isdir from cffi import FFI, CDefError From 55e27bd8633ef6928111c06fe1b443144a0c29bf Mon Sep 17 00:00:00 2001 From: Oliver Palmer Date: Sat, 9 Jan 2016 12:54:04 -0500 Subject: [PATCH 4/4] failIf is deprecated --- pywincffi/core/testutil.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pywincffi/core/testutil.py b/pywincffi/core/testutil.py index d4f758b..9cae87f 100644 --- a/pywincffi/core/testutil.py +++ b/pywincffi/core/testutil.py @@ -58,7 +58,8 @@ def setUp(self): # pylint: disable=invalid-name def SetLastError(self, value=0, lib=None): """Calls the Windows API function SetLastError()""" - self.failIf(os.name != "nt") + if os.name != "nt": + self.fail("Only an NT system should call this method") if lib is None: lib = libtest