mirror of
https://github.com/bendtherules/pytest_gui_status.git
synced 2026-08-18 13:42:26 +00:00
Move test helpers like reload_2_3 and others to basedir
Also add __init__.py to allow relative imports
This commit is contained in:
@@ -4,6 +4,8 @@ from mock import patch, MagicMock
|
||||
import redis
|
||||
import pytest
|
||||
|
||||
from ..utils_test import create_temp_case, reload_2_3
|
||||
|
||||
REDIS_TEST_PORT = status_plugin.REDIS_PORT + 1
|
||||
|
||||
mock_htmlPy_module = MagicMock()
|
||||
@@ -41,8 +43,8 @@ def redis_master(request, auto_shutdown=True):
|
||||
def test_redis_fail_1(tmpdir):
|
||||
# load new redis port
|
||||
import pytest_gui_status.status_gui.gui_backend as gui_backend
|
||||
reload(gui_backend)
|
||||
reload(status_plugin)
|
||||
reload_2_3(gui_backend)
|
||||
reload_2_3(status_plugin)
|
||||
|
||||
# dont start redis. Also if redis running, stop it
|
||||
redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0)
|
||||
@@ -69,8 +71,8 @@ def test_redis_fail_1(tmpdir):
|
||||
def test_redis_fail_2(tmpdir, redis_master):
|
||||
# load new redis port
|
||||
import pytest_gui_status.status_gui.gui_backend as gui_backend
|
||||
reload(gui_backend)
|
||||
reload(status_plugin)
|
||||
reload_2_3(gui_backend)
|
||||
reload_2_3(status_plugin)
|
||||
|
||||
# start Redis
|
||||
redis_master.init(tmpdir.strpath)
|
||||
@@ -100,8 +102,8 @@ def test_redis_fail_2(tmpdir, redis_master):
|
||||
def test_redis_fail_3(tmpdir, redis_master):
|
||||
# load new redis port
|
||||
import pytest_gui_status.status_gui.gui_backend as gui_backend
|
||||
reload(gui_backend)
|
||||
reload(status_plugin)
|
||||
reload_2_3(gui_backend)
|
||||
reload_2_3(status_plugin)
|
||||
|
||||
# start Redis
|
||||
redis_master.init(tmpdir.strpath)
|
||||
@@ -130,8 +132,8 @@ def test_redis_fail_3(tmpdir, redis_master):
|
||||
def test_redis_fail_4(tmpdir, redis_master):
|
||||
# load new redis port
|
||||
import pytest_gui_status.status_gui.gui_backend as gui_backend
|
||||
reload(gui_backend)
|
||||
reload(status_plugin)
|
||||
reload_2_3(gui_backend)
|
||||
reload_2_3(status_plugin)
|
||||
|
||||
# start Redis
|
||||
redis_master.init(tmpdir.strpath)
|
||||
|
||||
@@ -4,64 +4,12 @@ from mock import patch
|
||||
import redis
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import shutil
|
||||
from ..utils_test import create_temp_case, reload_2_3
|
||||
|
||||
REDIS_TEST_PORT = status_plugin.REDIS_PORT + 1
|
||||
s = status_plugin.s
|
||||
|
||||
|
||||
class patched_chdir(object):
|
||||
|
||||
"""Just like chdir, but can be used in `with` - sets back the old path at exit"""
|
||||
|
||||
def __init__(self, new_path):
|
||||
self.old_path = os.getcwd()
|
||||
self.new_path = new_path
|
||||
|
||||
def __enter__(self):
|
||||
os.chdir(self.new_path)
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
os.chdir(self.old_path)
|
||||
|
||||
|
||||
def create_temp_case(case_name, path_tmpdir=None):
|
||||
with patched_chdir(os.path.dirname(__file__)):
|
||||
path_test_cases = os.path.abspath("../testcases/")
|
||||
path_case = os.path.join(path_test_cases, case_name)
|
||||
|
||||
path_tmpdir = path_tmpdir or tempfile.mkdtemp()
|
||||
path_tmpdir_case = os.path.join(path_tmpdir, case_name)
|
||||
shutil.copytree(path_case, path_tmpdir_case)
|
||||
|
||||
return path_tmpdir_case
|
||||
|
||||
|
||||
def reload_2_3(module_name):
|
||||
'''
|
||||
reload that works in all versions of Python.
|
||||
Uses builtin reload in py2, imp.reload for <=py3.3,
|
||||
importlib.reload for >=3.4
|
||||
|
||||
The module six has similar functionalities,
|
||||
but would be too huge a dependency for this simple case.
|
||||
'''
|
||||
|
||||
from sys import version_info
|
||||
major_ver, minor_ver = version_info[:2]
|
||||
|
||||
if major_ver == 2:
|
||||
reload(module_name)
|
||||
elif major_ver == 3 and minor_ver <= 3:
|
||||
import imp
|
||||
imp.reload(module_name)
|
||||
elif major_ver == 3 and minor_ver >= 4:
|
||||
import importlib
|
||||
importlib.reload(module_name)
|
||||
else:
|
||||
raise NotImplementedError("Not sure how to reload in "
|
||||
"this version of Python, supported upto 3.x")
|
||||
CASE_BASEDIR = __file__
|
||||
|
||||
|
||||
@patch.dict("os.environ",
|
||||
@@ -72,7 +20,7 @@ def test_whole_1(tmpdir):
|
||||
'''
|
||||
os.chdir(os.path.dirname(__file__))
|
||||
tmpdir = str(tmpdir)
|
||||
path_case = create_temp_case("case_1", tmpdir)
|
||||
path_case = create_temp_case("case_1", CASE_BASEDIR, tmpdir)
|
||||
os.chdir(path_case)
|
||||
|
||||
assert(os.environ.get("PYTEST_STATUS_PORT") == str(REDIS_TEST_PORT))
|
||||
@@ -101,7 +49,7 @@ def test_whole_2(tmpdir):
|
||||
'''
|
||||
os.chdir(os.path.dirname(__file__))
|
||||
tmpdir = str(tmpdir)
|
||||
path_case = create_temp_case("case_2", tmpdir)
|
||||
path_case = create_temp_case("case_2", CASE_BASEDIR, tmpdir)
|
||||
os.chdir(path_case)
|
||||
|
||||
popen_pytest = subprocess.Popen(["py.test", "-s"], shell=True)
|
||||
@@ -126,7 +74,7 @@ def test_intermediate_1(tmpdir):
|
||||
'''
|
||||
os.chdir(os.path.dirname(__file__))
|
||||
tmpdir = str(tmpdir)
|
||||
path_case = create_temp_case("case_3", tmpdir)
|
||||
path_case = create_temp_case("case_3", CASE_BASEDIR, tmpdir)
|
||||
os.chdir(path_case)
|
||||
|
||||
popen_pytest = subprocess.Popen(["py.test", "-s"], shell=True)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import os
|
||||
import tempfile
|
||||
import shutil
|
||||
|
||||
|
||||
class patched_chdir(object):
|
||||
|
||||
"""Just like chdir, but can be used in `with` - sets back the old path at exit"""
|
||||
|
||||
def __init__(self, new_path):
|
||||
self.old_path = os.getcwd()
|
||||
self.new_path = new_path
|
||||
|
||||
def __enter__(self):
|
||||
os.chdir(self.new_path)
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
os.chdir(self.old_path)
|
||||
|
||||
|
||||
def create_temp_case(case_name, case_basedir=None, path_tmpdir=None):
|
||||
case_basedir = case_basedir or __file__
|
||||
with patched_chdir(os.path.dirname(case_basedir)):
|
||||
path_test_cases = os.path.abspath("../testcases/")
|
||||
path_case = os.path.join(path_test_cases, case_name)
|
||||
|
||||
path_tmpdir = path_tmpdir or tempfile.mkdtemp()
|
||||
path_tmpdir_case = os.path.join(path_tmpdir, case_name)
|
||||
shutil.copytree(path_case, path_tmpdir_case)
|
||||
|
||||
return path_tmpdir_case
|
||||
|
||||
|
||||
def reload_2_3(module_name):
|
||||
'''
|
||||
reload that works in all versions of Python.
|
||||
Uses builtin reload in py2, imp.reload for <=py3.3,
|
||||
importlib.reload for >=3.4
|
||||
|
||||
The module six has similar functionalities,
|
||||
but would be too huge a dependency for this simple case.
|
||||
'''
|
||||
|
||||
from sys import version_info
|
||||
major_ver, minor_ver = version_info[:2]
|
||||
|
||||
if major_ver == 2:
|
||||
reload(module_name)
|
||||
elif major_ver == 3 and minor_ver <= 3:
|
||||
import imp
|
||||
imp.reload(module_name)
|
||||
elif major_ver == 3 and minor_ver >= 4:
|
||||
import importlib
|
||||
importlib.reload(module_name)
|
||||
else:
|
||||
raise NotImplementedError("Not sure how to reload in "
|
||||
"this version of Python, supported upto 3.x")
|
||||
Reference in New Issue
Block a user