Repository: conda/conda-build

Issue:
work directory not deleted for test stage
The work directory is not deleted when the recipe tests are run. This has caused a problem where paths from the source directory were hardcoded in the executable, but I did not notice because the test passed. See https://github.com/conda-forge/emacs-feedstock/issues/2#issuecomment-267542043.

I don't remember if this has always been this way, or if there was a good reason for it if it was. I agree that the work directory should stay intact if there are no tests, or if the build fails and the tests are not gotten to, but it should be deleted (or if you're worried about deletion, at least set aside) during the tests, for the same reason that the build environment is deleted for the tests. 


Discussion:
I don't remember if this has changed as a result of my work on this.  I do see that it would be helpful to catch errors this way.  It also makes the source_files feature more complicated, unfortunately - you have to keep track of some location where you move the source to for when source_files goes looking for things.

I'd call this a feature request, and will try to work it into a release soon.  If you have time, a PR would be appreciated.
We'd need to make sure that the split-packages (new feature) all get packaged before any tests are run for this to work.

Renaming the work folder would cause havoc on Windows too (virus scanners holding file handles), so please don't do it that way.
What needs to access the source files after the tests are run? 

My plan was to just add a call to the top of `test()` to (re)move the test directory. I don't know how split packages work but assumedly any building should be done by then. 
Oh I see what `source_files` is. So I guess it should just be (re)moved after those files are copied. 
(and yes, I'll be happy to contribute this, but I want to get a full idea of potential issues from you guys first)
@mingwandroid and I have agreed offline that removing the source folder after files are copied for ``source_files`` should be fine.  With the split packages concept, we should have everything done before we leave the build phase, anyway.

Resolving patch:
diff --git a/conda_build/build.py b/conda_build/build.py
--- a/conda_build/build.py
+++ b/conda_build/build.py
@@ -1168,6 +1168,10 @@ def test(recipedir_or_package_or_metadata, config, move_broken=True):
 
     print("TEST START:", metadata.dist())
 
+    # Needs to come after create_files in case there's test/source_files
+    print("Deleting work directory,", config.work_dir)
+    rm_rf(config.work_dir)
+
     get_build_metadata(metadata, config=config)
     specs = ['%s %s %s' % (metadata.name(), metadata.version(), metadata.build_id())]
 


Test patch:
diff --git a/tests/test-recipes/work-deleted/bld.bat b/tests/test-recipes/work-deleted/bld.bat
new file mode 100644
--- /dev/null
+++ b/tests/test-recipes/work-deleted/bld.bat
@@ -0,0 +1,2 @@
+python "%RECIPE_DIR%\build.py"
+if errorlevel 1 exit 1
diff --git a/tests/test-recipes/work-deleted/build.py b/tests/test-recipes/work-deleted/build.py
new file mode 100644
--- /dev/null
+++ b/tests/test-recipes/work-deleted/build.py
@@ -0,0 +1,7 @@
+import os
+
+PREFIX = os.environ["PREFIX"]
+SRC_DIR = os.environ["SRC_DIR"]
+
+with open(os.path.join(PREFIX, 'conda-build-test'), 'w') as f:
+    f.write(SRC_DIR)
diff --git a/tests/test-recipes/work-deleted/build.sh b/tests/test-recipes/work-deleted/build.sh
new file mode 100644
--- /dev/null
+++ b/tests/test-recipes/work-deleted/build.sh
@@ -0,0 +1 @@
+python $RECIPE_DIR/build.py
diff --git a/tests/test-recipes/work-deleted/meta.yaml b/tests/test-recipes/work-deleted/meta.yaml
new file mode 100644
--- /dev/null
+++ b/tests/test-recipes/work-deleted/meta.yaml
@@ -0,0 +1,18 @@
+package:
+  name: conda-build-test-work-deleted
+  version: 1.0
+
+requirements:
+  build:
+    - python
+
+source:
+  path: ../test-package
+
+test:
+  files:
+    - test.py
+  requirements:
+    - python
+  commands:
+    - python test.py
diff --git a/tests/test-recipes/work-deleted/test.py b/tests/test-recipes/work-deleted/test.py
new file mode 100644
--- /dev/null
+++ b/tests/test-recipes/work-deleted/test.py
@@ -0,0 +1,9 @@
+import os
+
+PREFIX = os.environ["PREFIX"]
+
+with open(os.path.join(PREFIX, 'conda-build-test')) as f:
+    SRC_DIR = f.read().strip()
+
+# The directory might be recreated, so just check that it is empty
+assert not os.path.exists(SRC_DIR) or not os.listdir(SRC_DIR), SRC_DIR