Add --always-clean flag to run-test.

This flag makes run-test remove the test-artifacts even if it fails. Also
enable this option by default when doing run-tests with make. Add
a ART_TEST_RUN_TEST_ALWAYS_CLEAN environment variable to control this
option.

Change-Id: I7867b400d570d8d679d9822d1fa65f49eb3522ae
diff --git a/test/run-test b/test/run-test
index ae613d9..aef7c52 100755
--- a/test/run-test
+++ b/test/run-test
@@ -73,6 +73,7 @@
 build_only="no"
 suffix64=""
 trace="false"
+always_clean="no"
 
 while true; do
     if [ "x$1" = "x--host" ]; then
@@ -179,6 +180,9 @@
     elif [ "x$1" = "x--trace" ]; then
         trace="true"
         shift
+    elif [ "x$1" = "x--always-clean" ]; then
+        always_clean="yes"
+        shift
     elif expr "x$1" : "x--" >/dev/null 2>&1; then
         echo "unknown $0 option: $1" 1>&2
         usage="yes"
@@ -325,6 +329,7 @@
              "files."
         echo "    --64                 Run the test in 64-bit mode"
         echo "    --trace              Run with method tracing"
+        echo "    --always-clean       Delete the test files even if the test fails."
     ) 1>&2
     exit 1
 fi
@@ -445,19 +450,8 @@
     fi
 fi
 
-# Clean up test files.
-if [ "$good" == "yes" ]; then
-    cd "$oldwd"
-    rm -rf "$tmp_dir"
-    if [ "$target_mode" = "yes" -a "$build_exit" = "0" ]; then
-        adb shell rm -rf $DEX_LOCATION
-    fi
-    exit 0
-fi
-
-
 (
-    if [ "$update_mode" != "yes" ]; then
+    if [ "$good" != "yes" -a "$update_mode" != "yes" ]; then
         echo "${test_dir}: FAILED!"
         echo ' '
         echo '#################### info'
@@ -467,9 +461,33 @@
         echo '####################'
         echo ' '
     fi
-    echo "${TEST_NAME} files left in ${tmp_dir} on host"
-    if [ "$target_mode" == "yes" ]; then
-        echo "and in ${DEX_LOCATION} on target"
+
+) 1>&2
+
+# Clean up test files.
+if [ "$always_clean" = "yes" -o "$good" = "yes" ]; then
+    cd "$oldwd"
+    rm -rf "$tmp_dir"
+    if [ "$target_mode" = "yes" -a "$build_exit" = "0" ]; then
+        adb shell rm -rf $DEX_LOCATION
+    fi
+    if [ "$good" = "yes" ]; then
+        exit 0
+    fi
+fi
+
+
+(
+    if [ "$always_clean" = "yes" ]; then
+        echo "${TEST_NAME} files deleted from host "
+        if [ "$target_mode" == "yes" ]; then
+            echo "and from target"
+        fi
+    else
+        echo "${TEST_NAME} files left in ${tmp_dir} on host"
+        if [ "$target_mode" == "yes" ]; then
+            echo "and in ${DEX_LOCATION} on target"
+        fi
     fi
 
 ) 1>&2