language: go
sudo: required

go:
  - 1.11.x
  - stable
  - master

matrix:
  # Failing on the development version of Go isn't too bad.
  allow_failures:
    - go: master

env:
  - GO111MODULE=on

# Override Travis's default 'go get' step, since we use Go modules
install:
  - go mod download

before_script:
  # Install interface generator
  - GO111MODULE=off go get github.com/vburenin/ifacemaker

  # Fetch refs for linter
  - git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
  - git fetch

  # Install golangci-lint
  - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.12.2

  # Download test data
  - pushd cs-demos && git lfs pull -I '*' && popd

script:
  - go build ./...

  # Make sure generated code is up-to-date
  - go generate
  - diff_output=$(git diff)
  - if [[ "$diff_output" != "" ]]; then echo "Generated code is not up-to-date" && echo "$diff_output" && exit 1; fi

  # Lint changed code
  - if [[ "$TRAVIS_COMMIT_RANGE" != "" ]]; then commit_range=${TRAVIS_COMMIT_RANGE/.../..} && git diff $commit_range > /dev/null && base_rev=$commit_range || true; fi
  - if [[ "$base_rev" = "" ]]; then base_rev='origin/master'; fi && echo "Linting changes between/since $base_rev"
  - golangci-lint run --new-from-rev $base_rev

  # Get list of tests to run with race detection.
  # We don't want to run the entire demo set with it because it's large and race tests take a lot longer.
  - race_tests=$(go test -list . | grep -v "TestDemoSet\|Benchmark" | head -n -1 | awk -vORS=\| '{ print $1 }' | sed 's/|$/\n/')
  # Run race tests
  - go test -v -race -run "$race_tests" -timeout 15m

  # We run all tests again to get full coverage, technically unncecessary tho
  - go test -v -timeout 30m -coverprofile=coverage.txt -coverpkg=$(go list ./... | grep -v msg | awk -vORS=, '{ print $1 }' | sed 's/,$/\n/') ./...

after_success:
  - bash <(curl -s https://codecov.io/bash)

notifications:
  webhooks:
    urls:
      - https://webhooks.gitter.im/e/511da3cf50cdf951116d
    on_success: change
    on_failure: always
