Repository: conda/conda-build

Issue:
Testing a built package fails with "Could not find environment"
Hi,

I'm trying to run the tests of an already built package, but I'm getting the following error:

```bash
$ conda build -t dist/linux-64/PACKAGE.tar.bz2
TEST START: dist/linux-64/PACKAGE.tar.bz2
Renaming work directory,  /opt/conda/conda-bld/PACKAGE_1500375065863/work  to  /opt/conda/conda-bld/PACKAGE_1500375065863/work_moved_PACKAGE

The following NEW packages will be INSTALLED:

    PACKAGE file:///recipe/dist

+ set -x -e
+ source /opt/conda/bin/activate /opt/conda/conda-bld/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac
++ [[ -n 4.1.2(2)-release ]]
++ _SCRIPT_LOCATION=/opt/conda/bin/activate
++ _SHELL=bash
+++ dirname /opt/conda/bin/activate
++ _CONDA_DIR=/opt/conda/bin
++ '[' 1 -gt 1 ']'
++ case "$(uname -s)" in
+++ uname -s
++ EXT=
++ export _CONDA_PYTHON=/opt/conda/bin/python
++ _CONDA_PYTHON=/opt/conda/bin/python
++ [[ -n 4.1.2(2)-release ]]
+++ basename /opt/conda/conda-bld/test_tmp/conda_test_runner.sh
++ [[ conda_test_runner.sh == \a\c\t\i\v\a\t\e ]]
++ '[' 1 -eq 0 ']'
++ args=/opt/conda/conda-bld/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac
++ /opt/conda/bin/conda ..checkenv bash /opt/conda/conda-bld/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac

CondaValueError: Could not find environment: /opt/conda/conda-bld/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac


Leaving build/test directories:
  Work:	 ../opt/conda/conda-bld/work 
  Test:	 ../opt/conda/conda-bld/test_tmp 
Leaving build/test environments:
  Test:	source activate  ../opt/conda/conda-bld/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac 
  Build:	source activate  ../opt/conda/conda-bld/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho 


TESTS FAILED: PACKAGE.tar.bz2
```

I'm using ``conda-build=3.0.6`` and ``conda=4.3.22``. The problem does not occur when I use ``conda-build=2.1.17``, but I would like to use the new features introduced in ``3.x``. The package I'm building is a pure C package without any dependencies except GCC.

Any help is very much appreciated!


Discussion:
I can reproduce.  Seems like a bug.  I'll add a test and fix it for the next bugfix release.
Perfect, thanks a lot!

Resolving patch:
diff --git a/conda_build/build.py b/conda_build/build.py
--- a/conda_build/build.py
+++ b/conda_build/build.py
@@ -1335,11 +1335,13 @@ def test(recipedir_or_package_or_metadata, config, move_broken=True):
         print("TEST START:", test_package_name)
 
         if metadata.config.remove_work_dir:
-            dest = os.path.join(os.path.dirname(metadata.config.work_dir),
-                                'work_moved_' + metadata.dist())
-            # Needs to come after create_files in case there's test/source_files
-            print("Renaming work directory, ", metadata.config.work_dir, " to ", dest)
-            os.rename(config.work_dir, dest)
+            # nested if so that there's no warning when we just leave the empty workdir in place
+            if os.listdir(metadata.config.work_dir):
+                dest = os.path.join(os.path.dirname(metadata.config.work_dir),
+                                    'work_moved_' + metadata.dist())
+                # Needs to come after create_files in case there's test/source_files
+                print("Renaming work directory, ", metadata.config.work_dir, " to ", dest)
+                os.rename(config.work_dir, dest)
         else:
             log.warn("Not moving work directory after build.  Your package may depend on files "
                      "in the work directory that are not included with your package")
@@ -1427,50 +1429,51 @@ def test(recipedir_or_package_or_metadata, config, move_broken=True):
         # Python 2 Windows requires that envs variables be string, not unicode
         env = {str(key): str(value) for key, value in env.items()}
         suffix = "bat" if utils.on_win else "sh"
-        test_script = join(config.test_dir, "conda_test_runner.{suffix}".format(suffix=suffix))
+        test_script = join(metadata.config.test_dir,
+                           "conda_test_runner.{suffix}".format(suffix=suffix))
 
         with open(test_script, 'w') as tf:
             if not utils.on_win:
                 tf.write('set -x -e\n')
-            if config.activate:
+            if metadata.config.activate:
                 ext = ".bat" if utils.on_win else ""
                 tf.write('{source} "{conda_root}activate{ext}" "{test_env}"\n'.format(
                     conda_root=utils.root_script_dir + os.path.sep,
                     source="call" if utils.on_win else "source",
                     ext=ext,
-                    test_env=config.test_prefix))
+                    test_env=metadata.config.test_prefix))
                 if utils.on_win:
                     tf.write("if errorlevel 1 exit 1\n")
             if py_files:
-                test_python = config.test_python
+                test_python = metadata.config.test_python
                 # use pythonw for import tests when osx_is_app is set
                 if metadata.get_value('build/osx_is_app') and sys.platform == 'darwin':
                     test_python = test_python + 'w'
                 tf.write('"{python}" -s "{test_file}"\n'.format(
                     python=config.test_python,
-                    test_file=join(config.test_dir, 'run_test.py')))
+                    test_file=join(metadata.config.test_dir, 'run_test.py')))
                 if utils.on_win:
                     tf.write("if errorlevel 1 exit 1\n")
             if pl_files:
                 tf.write('"{perl}" "{test_file}"\n'.format(
-                    perl=config.perl_bin(config.test_prefix),
-                    test_file=join(config.test_dir, 'run_test.pl')))
+                    perl=config.perl_bin(metadata.config.test_prefix),
+                    test_file=join(metadata.config.test_dir, 'run_test.pl')))
                 if utils.on_win:
                     tf.write("if errorlevel 1 exit 1\n")
             if lua_files:
                 tf.write('"{lua}" "{test_file}"\n'.format(
-                    lua=config.lua_bin(config.test_prefix),
-                    test_file=join(config.test_dir, 'run_test.lua')))
+                    lua=metadata.config.lua_bin(config.test_prefix),
+                    test_file=join(metadata.config.test_dir, 'run_test.lua')))
                 if utils.on_win:
                     tf.write("if errorlevel 1 exit 1\n")
             if r_files:
                 tf.write('"{r}" CMD BATCH "{test_file}"\n'.format(
-                    r=config.r_bin(config.test_prefix),
-                    test_file=join(config.test_dir, 'run_test.r')))
+                    r=metadata.config.r_bin(metadata.config.test_prefix),
+                    test_file=join(metadata.config.test_dir, 'run_test.r')))
                 if utils.on_win:
                     tf.write("if errorlevel 1 exit 1\n")
             if shell_files:
-                test_file = join(config.test_dir, 'run_test.' + suffix)
+                test_file = join(metadata.config.test_dir, 'run_test.' + suffix)
                 if utils.on_win:
                     tf.write('call "{test_file}"\n'.format(test_file=test_file))
                     if utils.on_win:


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -509,3 +509,12 @@ def test_build_skip_existing_croot(testing_workdir, capfd):
     main_build.execute(args)
     output, error = capfd.readouterr()
     assert "are already built" in output
+
+
+@pytest.mark.serial
+def test_package_test(testing_workdir, testing_metadata):
+    """Test calling conda build -t <package file> - rather than <recipe dir>"""
+    api.output_yaml(testing_metadata, 'recipe/meta.yaml')
+    output = api.build(testing_workdir, config=testing_metadata.config, notest=True)[0]
+    args = ['-t', output]
+    main_build.execute(args)