Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Unused Code From core.testutil #39

Merged
merged 4 commits into from
Jan 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 4 additions & 84 deletions pywincffi/core/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -46,53 +41,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
Expand All @@ -105,11 +53,14 @@ 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()"""
if os.name != "nt":
self.fail("Only an NT system should call this method")

if lib is None:
lib = libtest

Expand All @@ -120,34 +71,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
11 changes: 7 additions & 4 deletions tests/test_core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import os
import tempfile
from textwrap import dedent
from os.path import isfile, join, expanduser

Expand Down Expand Up @@ -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)
Expand Down