#!/bin/zsh
#function t_setup {
  emulate -L zsh
  setopt local_options extended_glob glob_dots

  0=${(%):-%x}
  export T_PRJDIR="${0:A:h:h:h}"
  export T_TESTDATA=$T_PRJDIR/tests/testdata
  local testdir="$T_PRJDIR/tests"

  # Generate fixtures if needed. Use a container-local path so container
  # fixtures never bleed onto the host mount (or vice versa).
  local fixture_dir="$testdir/fixtures"
  local fixture_git_ver="$fixture_dir/.git_version"
  if [[ ! -d $fixture_dir/bare ]] || \
     [[ ! -f $fixture_git_ver ]] || \
     [[ "$(cat $fixture_git_ver)" != "$(git --version)" ]]; then
    fixture_dir="/tmp/antidote-fixtures"
    fixture_git_ver="$fixture_dir/.git_version"
    if [[ ! -d $fixture_dir/bare ]] || \
       [[ ! -f $fixture_git_ver ]] || \
       [[ "$(cat $fixture_git_ver)" != "$(git --version)" ]]; then
      rm -rf "$fixture_dir"
      FIXTURE_DIR="$fixture_dir" zsh $testdir/bin/init_fixtures.zsh &>/dev/null
    fi
  fi

  # setup test functions
  fpath+=( $testdir/functions )
  autoload -Uz $testdir/functions/*

  # tests/bin holds executables shared with bats-side helpers (subenv)
  path=( $testdir/bin $path )

  # gitconfig insteadOf rules handle URL rewriting for fake URLs

  # works with BSD and GNU gmktemp
  T_TEMPDIR=${$(mktemp -d -t t_antidote.XXXXXXXX):A}

  export HOME=$T_TEMPDIR
  export ZDOTDIR=$HOME/.zsh
  export ANTIDOTE_HOME=$HOME/.cache/antidote
  export ANTIDOTE_CONFIG=$HOME/.config/antidote/test_config.zsh

  # copy tmp_home contents
  cp -rf $testdir/tmp_home/* $T_TEMPDIR

  # our mock plugins use this
  typeset -ga plugins=()
  typeset -ga libs=()

  # use fixture gitconfig so fetch/pull resolve fake URLs to bare fixtures
  # sed rewrites stored paths to match current fixture location
  sed "s|/[^ \"]*/tests/fixtures/|${fixture_dir}/|g" "$fixture_dir/gitconfig" > "$HOME/.gitconfig"
  # background maintenance off since detached git gc processes race
  # teardown's rm -rf of the test home.
  printf '[gc]\n\tauto = 0\n[maintenance]\n\tauto = false\n' >> "$HOME/.gitconfig"

  # start from tmp home, keeping the caller's cwd for t_teardown to
  # restore. Bare pushd happens to land here too, but only because HOME
  # was reassigned above.
  pushd $T_TEMPDIR

  # source antidote
  source $T_PRJDIR/antidote.zsh
#}
