Skip to content

Commit

Permalink
Merge pull request #1687 from nicoddemus/monkeypatch-tests
Browse files Browse the repository at this point in the history
Use monkeypatch fixture in a few places of the test suite
  • Loading branch information
msarahan authored Jan 20, 2017
2 parents a7e3439 + 19793dc commit 55b482b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
29 changes: 14 additions & 15 deletions tests/test_api_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def recipe(request):


# This tests any of the folders in the test-recipes/metadata folder that don't start with _
def test_recipe_builds(recipe, test_config, testing_workdir):
def test_recipe_builds(recipe, test_config, testing_workdir, monkeypatch):
# These variables are defined solely for testing purposes,
# so they can be checked within build scripts
os.environ["CONDA_TEST_VAR"] = "conda_test"
os.environ["CONDA_TEST_VAR_2"] = "conda_test_2"
monkeypatch.setenv("CONDA_TEST_VAR", "conda_test")
monkeypatch.setenv("CONDA_TEST_VAR_2", "conda_test_2")
outputs = api.build(recipe, config=test_config)


Expand Down Expand Up @@ -241,12 +241,12 @@ def dummy_executable(folder, exename):
return exename


def test_checkout_tool_as_dependency(testing_workdir, test_config):
def test_checkout_tool_as_dependency(testing_workdir, test_config, monkeypatch):
# temporarily necessary because we have custom rebuilt svn for longer prefix here
test_config.channel_urls = ('conda_build_test', )
# "hide" svn by putting a known bad one on PATH
exename = dummy_executable(testing_workdir, "svn")
os.environ["PATH"] = os.pathsep.join([testing_workdir, os.environ["PATH"]])
monkeypatch.setenv("PATH", testing_workdir, prepend=os.pathsep)
FNULL = open(os.devnull, 'w')
with pytest.raises(subprocess.CalledProcessError, message="Dummy svn was not executed"):
check_call_env([exename, '--version'], stderr=FNULL)
Expand All @@ -268,16 +268,16 @@ def test_checkout_tool_as_dependency(testing_workdir, test_config):

@pytest.mark.skipif(sys.platform != "win32", reason="MSVC only on windows")
@pytest.mark.parametrize("msvc_ver", msvc_vers)
def test_build_msvc_compiler(msvc_ver):
def test_build_msvc_compiler(msvc_ver, monkeypatch):
# verify that the correct compiler is available
cl_versions = {"9.0": 15,
"10.0": 16,
"11.0": 17,
"12.0": 18,
"14.0": 19}

os.environ['CONDATEST_MSVC_VER'] = msvc_ver
os.environ['CL_EXE_VERSION'] = str(cl_versions[msvc_ver])
monkeypatch.setenv('CONDATEST_MSVC_VER', msvc_ver)
monkeypatch.setenv('CL_EXE_VERSION', str(cl_versions[msvc_ver]))

try:
# Always build Python 2.7 - but set MSVC version manually via Jinja template
Expand Down Expand Up @@ -450,7 +450,7 @@ def test_numpy_setup_py_data(test_config):
sys.version_info.major, sys.version_info.minor)


def test_relative_git_url_submodule_clone(testing_workdir):
def test_relative_git_url_submodule_clone(testing_workdir, monkeypatch):
"""
A multi-part test encompassing the following checks:
Expand Down Expand Up @@ -481,8 +481,7 @@ def test_relative_git_url_submodule_clone(testing_workdir):

# Put the broken git on os.environ["PATH"]
exename = dummy_executable(testing_workdir, 'git')
old_path = os.environ["PATH"]
os.environ["PATH"] = os.pathsep.join([testing_workdir, os.environ["PATH"]])
monkeypatch.setenv("PATH", testing_workdir, prepend=os.pathsep)
# .. and ensure it gets run (and fails).
FNULL = open(os.devnull, 'w')
# Strangely ..
Expand All @@ -493,7 +492,7 @@ def test_relative_git_url_submodule_clone(testing_workdir):
FNULL.close()

for tag in range(2):
os.chdir(absolute_sub)
monkeypatch.chdir(absolute_sub)
if tag == 0:
check_call_env([git, 'init'], env=sys_git_env)
with open('absolute', 'w') as f:
Expand All @@ -502,7 +501,7 @@ def test_relative_git_url_submodule_clone(testing_workdir):
check_call_env([git, 'commit', '-m', 'absolute{}'.format(tag)],
env=sys_git_env)

os.chdir(relative_sub)
monkeypatch.chdir(relative_sub)
if tag == 0:
check_call_env([git, 'init'], env=sys_git_env)
with open('relative', 'w') as f:
Expand All @@ -511,7 +510,7 @@ def test_relative_git_url_submodule_clone(testing_workdir):
check_call_env([git, 'commit', '-m', 'relative{}'.format(tag)],
env=sys_git_env)

os.chdir(toplevel)
monkeypatch.chdir(toplevel)
if tag == 0:
check_call_env([git, 'init'], env=sys_git_env)
with open('toplevel', 'w') as f:
Expand Down Expand Up @@ -573,7 +572,7 @@ def test_relative_git_url_submodule_clone(testing_workdir):
outfile.write(yaml.dump(data, default_flow_style=False, width=999999999))
# Reset the path because our broken, dummy `git` would cause `render_recipe`
# to fail, while no `git` will cause the build_dependencies to be installed.
os.environ["PATH"] = old_path
monkeypatch.undo()
# This will (after one spin round the loop) install and run 'git' with the
# build env prepended to os.environ[]
output = api.get_output_file_path(testing_workdir)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .utils import testing_workdir, test_config


def test_patch_strip_level(testing_workdir):
def test_patch_strip_level(testing_workdir, monkeypatch):
patchfiles = set(('some/common/prefix/one.txt',
'some/common/prefix/two.txt',
'some/common/prefix/three.txt'))
Expand All @@ -16,13 +16,13 @@ def test_patch_strip_level(testing_workdir):
with open(os.path.join(os.path.join(*folders), file), 'w') as f:
f.write('hello\n')
assert _guess_patch_strip_level(patchfiles, os.getcwd()) == 0
os.chdir(folders[0])
monkeypatch.chdir(folders[0])
assert _guess_patch_strip_level(patchfiles, os.getcwd()) == 1
os.chdir(folders[1])
monkeypatch.chdir(folders[1])
assert _guess_patch_strip_level(patchfiles, os.getcwd()) == 2
os.chdir(folders[2])
monkeypatch.chdir(folders[2])
assert _guess_patch_strip_level(patchfiles, os.getcwd()) == 3
os.chdir(testing_workdir)
monkeypatch.chdir(testing_workdir)


def test_patch(testing_workdir, test_config):
Expand Down
10 changes: 2 additions & 8 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,13 @@ def test_metadata(request, test_config):


@pytest.fixture(scope='function')
def testing_env(testing_workdir, request):
def testing_env(testing_workdir, request, monkeypatch):
env_path = os.path.join(testing_workdir, 'env')

check_call_env(['conda', 'create', '-yq', '-p', env_path,
'python={0}'.format(".".join(sys.version.split('.')[:2]))])
path_backup = os.environ['PATH']
os.environ['PATH'] = prepend_bin_path(os.environ.copy(), env_path, prepend_prefix=True)['PATH']

monkeypatch.setenv('PATH', prepend_bin_path(os.environ.copy(), env_path, prepend_prefix=True)['PATH'])
# cleanup is done by just cleaning up the testing_workdir
def reset_path():
os.environ['PATH'] = path_backup

request.addfinalizer(reset_path)
return env_path


Expand Down

0 comments on commit 55b482b

Please sign in to comment.