Repository: conda/conda-build

Issue:
conda-build -t is broken in 3.0.9
@msarahan I think in 3.0.9 the flag `-t` broke again :/

recipe for th test... It also happens if I specify a test section...
```
package:
  name: "testpkg"
  version: "1.0.0"
```

build only with `--no-test`
```
gabm@pc-klosi /t/testpkg  $ conda build . --no-test
Attempting to finalize metadata for testpkg
INFO:conda_build.metadata:Attempting to finalize metadata for testpkg
BUILD START: ['testpkg-1.0.0-h36134e3_0.tar.bz2']
source tree in: /home/gabm/miniconda3/conda-bld/testpkg_1501944800516/work
Packaging testpkg
INFO:conda_build.build:Packaging testpkg
Packaging testpkg-1.0.0-h36134e3_0
INFO:conda_build.build:Packaging testpkg-1.0.0-h36134e3_0
number of files: 0
Fixing permissions
Fixing permissions
updating: testpkg-1.0.0-h36134e3_0.tar.bz2
# Automatic uploading is disabled
# If you want to upload package(s) to anaconda.org later, type:

anaconda upload /home/gabm/miniconda3/conda-bld/linux-64/testpkg-1.0.0-h36134e3_0.tar.bz2

# To have conda build upload to anaconda.org automatically, use
# $ conda config --set anaconda_upload yes

anaconda_upload is not set.  Not uploading wheels: []



####################################################################################
Source and build intermediates have been left in /home/gabm/miniconda3/conda-bld.
There are currently 57 accumulated.
To remove them, you can run the ```conda build purge``` command
```


The test stage fails:
```
 gabm@pc-klosi /t/testpkg  $ conda build . -t

Leaving build/test directories:
  Work:  ../../home/gabm/miniconda3/conda-bld/work 
  Test:  ../../home/gabm/miniconda3/conda-bld/test_tmp 
Leaving build/test environments:
  Test: source activate  ../../home/gabm/miniconda3/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_pla 
  Build:        source activate  ../../home/gabm/miniconda3/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_placeh 


Traceback (most recent call last):
  File "/home/gabm/miniconda3/bin/conda-build", line 6, in <module>
    sys.exit(conda_build.cli.main_build.main())
  File "/home/gabm/miniconda3/lib/python3.5/site-packages/conda_build/cli/main_build.py", line 387, in main
    execute(sys.argv[1:])
  File "/home/gabm/miniconda3/lib/python3.5/site-packages/conda_build/cli/main_build.py", line 357, in execute
    action(recipe, config)
  File "/home/gabm/miniconda3/lib/python3.5/site-packages/conda_build/cli/main_build.py", line 297, in test_action
    return api.test(recipe, move_broken=False, config=config)
  File "/home/gabm/miniconda3/lib/python3.5/site-packages/conda_build/api.py", line 204, in test
    test_result = test(recipedir_or_package_or_metadata, config=config, move_broken=move_broken)
  File "/home/gabm/miniconda3/lib/python3.5/site-packages/conda_build/build.py", line 1400, in test
    config)
  File "/home/gabm/miniconda3/lib/python3.5/site-packages/conda_build/build.py", line 1315, in construct_metadata_for_test
    with open(os.path.join(info_dir, 'index.json')) as f:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/testpkg/info/index.json'
```




Discussion:
Sorry.  Testing really works better with passing the package to test,
rather than passing the recipe to test.

```
conda build -t
/Users/msarahan/miniconda3/conda-bld/osx-64/testpkg-1.0.0-h36134e3_0.tar.bz2
Nothing to test for:
/Users/msarahan/miniconda3/conda-bld/osx-64/testpkg-1.0.0-h36134e3_0.tar.bz2
--dirty flag and --keep-old-work not specified.Removing build/test folder
after successful build/test.

INFO conda_build.config:__exit__(609): --dirty flag and --keep-old-work not
specified.Removing build/test folder after successful build/test.

All tests passed
```

This said, it is a bug that testing using the recipe doesn't work, and I'll
fix it for the next bug-fix release.  Thanks for the report.

I likely had a hand in breaking this, sorry.
Adding the test files to packages so that any package, even those without
recipes embedded, can be tested in airgapped environments is a big
improvement. Minor egg to break here to make that omelette.  Besides, I
think we should deprecate the behavior here. Testing based on a recipe is
terribly indirect and not intuitive.  We should instead allow URL arguments
to the packages list, or provide some behavior to look for packages on
provided anaconda.org channels, since people may not have package files
locally.

On Aug 6, 2017 07:14, "Ray Donnelly" <notifications@github.com> wrote:

> I likely had a hand in breaking this, sorry.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/conda/conda-build/issues/2260#issuecomment-320503271>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AACV-ZMjcs7CI8kZia7_8qj0ocUsa25pks5sVa4dgaJpZM4Ouer1>
> .
>


Resolving patch:
diff --git a/conda_build/build.py b/conda_build/build.py
--- a/conda_build/build.py
+++ b/conda_build/build.py
@@ -46,7 +46,7 @@
 from conda_build import __version__
 from conda_build import environ, source, tarcheck, utils
 from conda_build.index import get_build_index
-from conda_build.render import (output_yaml, bldpkg_path, render_recipe, reparse,
+from conda_build.render import (output_yaml, bldpkg_path, render_recipe, reparse, finalize_metadata,
                                 distribute_variants, expand_outputs, try_download)
 import conda_build.os_utils.external as external
 from conda_build.metadata import MetaData
@@ -1306,8 +1306,20 @@ def warn_on_use_of_SRC_DIR(metadata):
                              "or pass the --no-remove-work-dir flag.")
 
 
-def construct_metadata_for_test(recipedir_or_package, config):
-    recipe_dir, need_cleanup = utils.get_recipe_abspath(recipedir_or_package)
+def _construct_metadata_for_test_from_recipe(recipe_dir, config):
+    config.need_cleanup = False
+    hash_input = {}
+    metadata = render_recipe(recipe_dir, config=config, reset_build_id=False)[0][0]
+    if metadata.meta.get('test', {}).get('source_files'):
+        if not os.listdir(metadata.config.work_dir):
+            try_download(metadata, no_download_source=False)
+    if not metadata.final:
+        metadata = finalize_metadata(metadata)
+    return metadata, hash_input
+
+
+def _construct_metadata_for_test_from_package(package, config):
+    recipe_dir, need_cleanup = utils.get_recipe_abspath(package)
     config.need_cleanup = need_cleanup
     hash_input = {}
 
@@ -1327,10 +1339,7 @@ def construct_metadata_for_test(recipedir_or_package, config):
             config.filename_hashing = False
             hash_input = {}
 
-    if os.path.isdir(recipedir_or_package):
-        local_location = os.path.dirname(recipe_dir)
-    else:
-        local_location = os.path.dirname(recipedir_or_package)
+    local_location = os.path.dirname(package)
     # strip off extra subdir folders
     for platform in ('win', 'linux', 'osx'):
         if os.path.basename(local_location).startswith(platform + "-"):
@@ -1380,6 +1389,14 @@ def construct_metadata_for_test(recipedir_or_package, config):
     return metadata, hash_input
 
 
+def construct_metadata_for_test(recipedir_or_package, config):
+    if os.path.isdir(recipedir_or_package) or os.path.basename(recipedir_or_package) == 'meta.yaml':
+        m, hash_input = _construct_metadata_for_test_from_recipe(recipedir_or_package, config)
+    else:
+        m, hash_input = _construct_metadata_for_test_from_package(recipedir_or_package, config)
+    return m, hash_input
+
+
 def test(recipedir_or_package_or_metadata, config, move_broken=True):
     '''
     Execute any test scripts for the given package.


Test patch:
diff --git a/tests/test_api_test.py b/tests/test_api_test.py
--- a/tests/test_api_test.py
+++ b/tests/test_api_test.py
@@ -8,11 +8,19 @@
 from .utils import metadata_dir
 
 
+def test_recipe_test(testing_workdir, testing_config):
+    """Test calling conda build -t <recipe dir>"""
+    recipe = os.path.join(metadata_dir, 'has_prefix_files')
+    metadata = api.render(recipe, config=testing_config)[0][0]
+    api.build(metadata, notest=True, anaconda_upload=False)
+    api.test(recipe, config=metadata.config)
+
+
 def test_package_test(testing_workdir, testing_config):
     """Test calling conda build -t <package file> - rather than <recipe dir>"""
     recipe = os.path.join(metadata_dir, 'has_prefix_files')
     metadata = api.render(recipe, config=testing_config)[0][0]
-    outputs = api.build(metadata, notest=True)
+    outputs = api.build(metadata, notest=True, anaconda_upload=False)
     api.test(outputs[0], config=metadata.config)
 
 
@@ -26,7 +34,7 @@ def test_package_test_without_recipe_in_package(testing_workdir, testing_metadat
 def test_package_with_jinja2_does_not_redownload_source(testing_workdir, testing_config, mocker):
     recipe = os.path.join(metadata_dir, 'jinja2_build_str')
     metadata = api.render(recipe, config=testing_config, dirty=True)[0][0]
-    outputs = api.build(metadata, notest=True)
+    outputs = api.build(metadata, notest=True, anaconda_upload=False)
     # this recipe uses jinja2, which should trigger source download, except that source download
     #    will have already happened in the build stage.
     # https://github.com/conda/conda-build/issues/1451