#!/bin/bash
#
# This is the place you can extend the funcitonality of the studio

hab pkg install chef/studio-common >/dev/null
source "$(hab pkg path chef/studio-common)/bin/studio-common"

# switch all go commands to use the vendor/ directory
#export GOFLAGS="-mod=vendor"

# Set GOPROXY to allow downloading dependencies
export GOPROXY="https://proxy.golang.org,direct"

# Specify where to put 'go installed' binaries
export GOBIN=/src/bin

# Make 'go installed' binaries available in the PATH
export PATH="$GOBIN:$PATH"

function build_cross_platform() {
  install_if_missing core/go24 go
  install_if_missing core/gcc gcc
  install_if_missing core/gox gox
  go mod tidy
  go mod vendor
  ( cd /src || exit 1
    gox -output="../../bin/chef_{{.OS}}_{{.Arch}}" \
        -os="darwin linux windows" \
        -arch="amd64"
  )
}

function update_deps() {
  install_if_missing core/go24 go
  install_if_missing core/git git
  ( cd /src || return 1
    GOFLAGS="" go get -u "$@"
    go mod vendor
  )
}

document "unit_tests" <<DOC
  Run unit tests (go-based)
DOC
function unit_tests() {
  install_if_missing core/go24 go
  install_if_missing core/gcc gcc

  log_line "Running unit tests of main.go"
  # Avoid running integration tests inside unit tests
  ( cd /src || return 1
    mkdir -p coverage/
    GO_PACKAGES=$(go list ./... | grep -v integration)
    go test \
    || return 1
  )
  log_line "Running unit tests (/cmd)"
  ( cd /src || return 1
   go test -tags=unit ./cmd -v -count=1 || return 1
  )
}


 # run integration tests
 function integration_tests() {
   install_if_missing core/go24 go
   install_if_missing core/gcc gcc

   log_line "Building cross-platform binaries"
   build_cross_platform || return 1

   log_line "Running integration tests (/integration)"
   ( cd /src || return 1
    go test -tags=integration ./integration -v -count=1 || return 1
   )
 }

