function init_git_repo {
  git init $BATS_TEST_DIRNAME
}

function nuke_git_repo {
  rm -fR $BATS_TEST_DIRNAME/.git
}

function cleanup_all {
  nuke_git_repo
  rm $BATS_TEST_DIRNAME/.gitattributes
  rm $BATS_TEST_DIRNAME/sensitive_file
}

function init_git_repo {
  # Warn and do nothing if test dir envvar is unset
  if [ -z $BATS_TEST_DIRNAME ]; then
    echo "WARNING: Required envvar \$BATS_TEST_DIRNAME is unset"
  # Warn and do nothing if test git repo path already exists
  elif [ -e $BATS_TEST_DIRNAME/.git ]; then
    echo "WARNING: Test repo already exists at $BATS_TEST_DIRNAME/.git"
  else
    # Initialise test git repo at the same path as the test files
    git init $BATS_TEST_DIRNAME
    # Flag test git repo as 100% the test one, for safety before later removal
    touch $BATS_TEST_DIRNAME/.git/repo-for-transcrypt-bats-tests
  fi
}

function nuke_git_repo {
  # Warn and do nothing if test dir envvar is unset
  if [ -z $BATS_TEST_DIRNAME ]; then
    echo "WARNING: Required envvar \$BATS_TEST_DIRNAME is unset"
  # Warn and do nothing if the test git repo is missing the flag file that
  # ensures it *really* is the test one, as set by the 'init_git_repo' function
  elif [ ! -e $BATS_TEST_DIRNAME/.git/repo-for-transcrypt-bats-tests ]; then
    echo "WARNING: Aborting delete of non-test Git repo at $BATS_TEST_DIRNAME/.git"
  else
    # Forcibly delete the test git repo
    rm -fR $BATS_TEST_DIRNAME/.git
  fi
}

function cleanup_all {
  nuke_git_repo
  rm $BATS_TEST_DIRNAME/.gitattributes
  rm $BATS_TEST_DIRNAME/sensitive_file
}

Add safeguards to tests to ensure only the TEST git repo gets deleted
Add safeguards to tests to ensure only the TEST git repo gets deleted

Add some safety checks before forcibly deleting what we expect to be
the Git respository used for testing, but which could potentially be
a real repo with valuable content.

Ensure we don't ever delete the wrong path by:

- avoid init or delete of the repo if a required envvar is unset
- require that a special flag file created by our init function is
  present in the .git/ dir before we delete it, so we will only ever
  delete our own test repo
- avoid re-init of an existing repo, mainly to avoid mistakenly
  flagging an existing repo as our own test repo.
