Skip to content

Commit

Permalink
Factor out zip setup code
Browse files Browse the repository at this point in the history
  • Loading branch information
brettcannon committed Oct 27, 2017
1 parent b401980 commit 6ac5da6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
28 changes: 2 additions & 26 deletions importlib_resources/tests/test_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,6 @@
from . import util


ZIP_DATA_PATH = None # type: Optional[pathlib.Path]
zip_data = None # type: Optional[]
def setUpModule():
global ZIP_DATA_PATH, zip_data
data_path = pathlib.Path(data.__spec__.origin)
data_dir = data_path.parent
ZIP_DATA_PATH = str(data_dir / 'ziptestdata.zip')
sys.path.append(ZIP_DATA_PATH)
import ziptestdata
zip_data = ziptestdata


def tearDownModule():
global zip_data
try:
sys.path.remove(ZIP_DATA_PATH)
del sys.path_importer_cache[ZIP_DATA_PATH]
del sys.modules[zip_data.__spec__.name]
except ValueError:
pass


class CommonTests(util.CommonTests, unittest.TestCase):

def execute(self, package, path):
Expand Down Expand Up @@ -65,10 +43,8 @@ def setUp(self):
self.data = data


class OpenZipTests(OpenTests, unittest.TestCase):

def setUp(self):
self.data = zip_data
class OpenZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
pass


if __name__ == '__main__':
Expand Down
24 changes: 24 additions & 0 deletions importlib_resources/tests/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
import importlib
import pathlib
import sys
import unittest
Expand Down Expand Up @@ -52,3 +53,26 @@ def test_non_package(self):
# The anchor package cannot be a module.
with self.assertRaises(TypeError):
self.execute(__spec__.name, 'utf-8.file')


class ZipSetup:

@classmethod
def setUpClass(cls):
global ZIP_DATA_PATH, zip_data
data_path = pathlib.Path(data.__spec__.origin)
data_dir = data_path.parent
cls._zip_path = str(data_dir / 'ziptestdata.zip')
sys.path.append(cls._zip_path)
cls.data = importlib.import_module('ziptestdata')

@classmethod
def tearDownClass(cls):
try:
sys.path.remove(cls._zip_path)
del sys.path_importer_cache[cls._zip_path]
del sys.modules[cls.data.__spec__.name]
del cls.data
del cls._zip_path
except (KeyError, ValueError):
pass

0 comments on commit 6ac5da6

Please sign in to comment.