new_variant: clean-up on failure

If the build (launched by new_variant.py) fails, clean up the new
variant before exiting. Previous implementation had the clean up
for ~/.new_variant.yaml in the wrong place, and it wouldn't get
called from the exit handler.

BUG=b:167305316
TEST=See testdata/README.md for full details.
Use `&&` to run a second command if the first one passes.
`cd testdata ; ./new_variant_fulltest.sh hatch && echo PASS`
`cd testdata ; ./new_variant_fulltest.sh waddledoo && echo PASS`
The case with hatch will show "PASS" but because waddledoo has an
issue right now, that line won't show the "PASS".
Near the end, you'll see a call to new_variant.py --abort as the
script cleans up and exits. ~/.new_variant.yaml (in the chroot)
will not exist, because it got cleaned up by the --abort.

Change-Id: I11a572c6b4e0729bc65f45987dce4395eb1fa661
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2422446
Tested-by: Paul Fagerburg <pfagerburg@chromium.org>
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Commit-Queue: Paul Fagerburg <pfagerburg@chromium.org>
diff --git a/contrib/variant/testdata/new_variant_fulltest.sh b/contrib/variant/testdata/new_variant_fulltest.sh
index ffe051b..0b357a6 100755
--- a/contrib/variant/testdata/new_variant_fulltest.sh
+++ b/contrib/variant/testdata/new_variant_fulltest.sh
@@ -4,7 +4,7 @@
 # found in the LICENSE file.
 
 # End-to-end test of creating firmware for a new variant of a reference board
-VERSION="1.0.0"
+VERSION="1.0.1"
 SCRIPT=$(basename -- "${0}")
 set -e
 
@@ -145,6 +145,12 @@
     fi
     popd
   fi
+  # If new_variant didn't clean up after itself, the build must have failed.
+  # Clean up with --abort and exit this script with an error code.
+  if [[ -e "${HOME}/.new_variant.yaml" ]] ; then
+    ./new_variant.py --abort --verbose
+    exit 1
+  fi
 }
 trap 'cleanup' EXIT
 
@@ -199,11 +205,6 @@
 # Now create the new variant. Output will be captured as a side-effect of
 # running in CQ, or it will be in the scrollback buffer on the user's terminal
 # when executed locally.
+# If the build fails, the cleanup handler will call new_variant.py --abort
+# to clean up.
 ./new_variant.py --board="${REFERENCE}" --variant="${NEW}" --verbose
-
-# If new_variant didn't clean up after itself, the build must have failed.
-# Clean up with --abort and exit this script with an error code.
-if [[ ! "${HOME}/.new_variant.yaml" ]] ; then
-  ./new_variant.py --abort --verbose
-  exit 1
-fi