diff --git a/tests/test_conda.py b/tests/test_conda.py new file mode 100644 index 0000000..fe0f737 --- /dev/null +++ b/tests/test_conda.py @@ -0,0 +1,53 @@ +""" +Test conda commandline wrappers +""" +from tljh import conda +import os +import pytest +import subprocess +import tempfile + + +@pytest.fixture +def prefix(): + """ + Provide a temporary directory to make environments in. + """ + with tempfile.TemporaryDirectory() as tmpdir: + yield tmpdir + + +def test_create_environment(prefix): + """ + Test conda environment creation + + An empty conda environment doesn't seem to have anything in it, + so we just check for directory existence. + """ + conda.ensure_conda_env(prefix) + assert os.path.exists(prefix) + + +def test_ensure_environment(prefix): + """ + Test second call to ensure_conda_env works as expected + + A conda environment already exists, so we it should just do nothing + """ + conda.ensure_conda_env(prefix) + assert os.path.exists(prefix) + conda.ensure_conda_env(prefix) + + +def test_ensure_packages(prefix): + """ + Test installing packages in conda environment + """ + conda.ensure_conda_env(prefix) + conda.ensure_conda_packages(prefix, ['numpy']) + # Throws an error if this fails + subprocess.check_call([ + os.path.join(prefix, 'bin', 'python'), + '-c', + 'import numpy' + ]) diff --git a/tljh/conda.py b/tljh/conda.py index 19427a8..a85280d 100644 --- a/tljh/conda.py +++ b/tljh/conda.py @@ -26,7 +26,7 @@ def ensure_conda_env(prefix): def ensure_conda_packages(prefix, packages): """ - Ensure packages are installed in the conda prefix + Ensure packages are installed in the conda prefix. """ abspath = os.path.abspath(prefix) # Let subprocess errors propagate @@ -41,7 +41,8 @@ def ensure_conda_packages(prefix, packages): # parse this outside of this kludge. filtered_output = '\n'.join([ l for l in raw_output.split('\n') - if not l.startswith('{"fetch"') + # Sometimes the JSON messages start with a \00. The lstrip removes these. + if not l.lstrip().startswith('{"fetch"') ]) output = json.loads(filtered_output) if 'success' in output and output['success'] == True: