From c029e44dc801397da196d79c849795926d05b59b Mon Sep 17 00:00:00 2001
From: Ed Morley <edmorley@users.noreply.github.com>
Date: Tue, 8 Aug 2017 03:23:23 +0200
Subject: [PATCH] Improve UX when clearing cache due to the stack changing
 (#442)

* Test that the cache is invalidated when the stack changes

* Improve UX when clearing cache due to the stack changing

Now outputs a message informing that the cache was cleared, and
clears the cache first to avoid a redundant message about removing
an old Python version.
---
 bin/steps/python | 12 +++++-------
 test/run         |  9 +++++++++
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/bin/steps/python b/bin/steps/python
index 32a25903..908d1b27 100755
--- a/bin/steps/python
+++ b/bin/steps/python
@@ -4,7 +4,11 @@ set +e
 runtime-fixer runtime.txt
 PYTHON_VERSION=$(cat runtime.txt)
 
-# Install Python.
+if [[ "$STACK" != "$CACHED_PYTHON_STACK" ]]; then
+    puts-step "Stack has changed from $CACHED_PYTHON_STACK to $STACK, clearing cache"
+    rm -fr .heroku/python-stack .heroku/python-version .heroku/python .heroku/vendor
+fi
+
 if [ -f .heroku/python-version ]; then
   if [ ! "$(cat .heroku/python-version)" = "$PYTHON_VERSION" ]; then
       puts-step "Found $(cat .heroku/python-version), removing"
@@ -14,12 +18,6 @@ if [ -f .heroku/python-version ]; then
   fi
 fi
 
-if [ ! "$STACK" = "$CACHED_PYTHON_STACK" ]; then
-    rm -fr .heroku/python .heroku/python-stack .heroku/vendor
-    unset SKIP_INSTALL
-fi
-
-
 if [ ! "$SKIP_INSTALL" ]; then
     puts-step "Installing $PYTHON_VERSION"
 
diff --git a/test/run b/test/run
index e125c13f..130b0e7b 100755
--- a/test/run
+++ b/test/run
@@ -76,6 +76,15 @@ testSmartRequirements() {
   assertCapturedSuccess
 }
 
+testStackChange() {
+  local cache_dir="$(mktmpdir)"
+  mkdir -p "${cache_dir}/.heroku"
+  echo "different-stack" > "${cache_dir}/.heroku/python-stack"
+  compile "requirements-standard" "$cache_dir"
+  assertCaptured "clearing cache"
+  assertFile "$STACK" ".heroku/python-stack"
+  assertCapturedSuccess
+}
 
 
 pushd $(dirname 0) >/dev/null
-- 
GitLab