#!/usr/bin/env bash

setup_file(){
  load "$PWD/tests/integration/helpers/common"
  _common_setup
  _add_server
  _new_app
}

teardown_file(){
  _rm_server
  _reset_recipe
}

setup(){
  load "$PWD/tests/integration/helpers/common"
  _common_setup
  _set_git_author
}

teardown() {
  _reset_recipe
}

@test "validate recipe argument" {
  run $ABRA recipe release --no-input
  assert_failure
  assert_output --partial 'no recipe name provided'

  run $ABRA recipe release DOESNTEXIST --no-input
  assert_failure
  assert_output --partial 'unable to clone DOESNTEXIST'
}

@test "release patch bump" {
  _ensure_latest_version "0.3.0+1.21.0"

  run $ABRA recipe upgrade "$TEST_RECIPE" --no-input --patch
  assert_success

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff
  assert_success
  assert_output --partial 'image: nginx:1.21.6'

  run $ABRA recipe sync "$TEST_RECIPE" --no-input --patch
  assert_success
  assert_output --partial 'synced label'

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff
  assert_success
  assert_output --partial 'coop-cloud.${STACK_NAME}.version=0.3.1+1.21.6'

  run $ABRA recipe release "$TEST_RECIPE" --no-input --patch
  assert_success
  assert_output --partial 'no -p/--publish passed, not publishing'

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag --list
  assert_success
  assert_output --partial '0.3.1+1.21.6'
}

@test "release minor bump" {
  _ensure_latest_version "0.3.0+1.21.0"

  run $ABRA recipe upgrade "$TEST_RECIPE" --no-input --minor
  assert_success

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff
  assert_success
  assert_output --regexp 'image: nginx:1.2.*'

  run $ABRA recipe sync "$TEST_RECIPE" --no-input --minor
  assert_success
  assert_output --partial 'synced label'

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" diff
  assert_success
  assert_output --regexp 'coop-cloud\.\$\{STACK_NAME\}\.version=0\.4\.0\+1\.2.*'

  run $ABRA recipe release "$TEST_RECIPE" --no-input --minor
  assert_success
  assert_output --partial 'no -p/--publish passed, not publishing'

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag --list
  assert_success
  assert_output --regexp '0\.4\.0\+1\.2.*'
}

@test "unknown files not committed" {
  run $ABRA recipe upgrade "$TEST_RECIPE" --no-input --patch
  assert_success

  run bash -c 'echo "unstaged changes" >> "$ABRA_DIR/recipes/$TEST_RECIPE/foo"'
  assert_success
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/foo"

  run $ABRA recipe release "$TEST_RECIPE" --no-input --patch
  assert_success
  assert_output --partial 'no -p/--publish passed, not publishing'

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" rm foo
  assert_failure
  assert_output --partial "fatal: pathspec 'foo' did not match any files"
}

@test "release with next release note" {
  _ensure_latest_version "0.3.0+1.21.0"

  _mkfile "$ABRA_DIR/recipes/$TEST_RECIPE/release/next" "those are some release notes for the next release"

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" add release/next
  assert_success

  run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" commit -m "added some release notes"
  assert_success

  run $ABRA recipe release  "$TEST_RECIPE" --no-input  --minor
  assert_success
  assert_output --partial 'no -p/--publish passed, not publishing'

  assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE/release/next"
  assert_exists "$ABRA_DIR/recipes/$TEST_RECIPE/release/0.4.0+1.21.0"
  assert_file_contains "$ABRA_DIR/recipes/$TEST_RECIPE/release/0.4.0+1.21.0" "those are some release notes for the next release"
}
