version: 2.1

jobs:
  build:
    working_directory: /go/src/github.com/qri-io/qri
    docker:
      - image: circleci/golang:1.14.6
        environment:
          GO111MODULE: "on"
    environment:
      TEST_RESULTS: /tmp/test-results
      GOFLAGS: -p=8
    # resource_class requires a paid circleci plan:
    resource_class: medium+
    steps:
      - checkout
      - run: mkdir -p $TEST_RESULTS
      - restore_cache:
          key: dependency-cache-{{ checksum "go.sum" }}
      - run: 
          name: Get CI Deps
          command: go get github.com/jstemmer/go-junit-report golang.org/x/lint/golint
      - run:
          name: Lint
          command: golint -set_exit_status ./...
      - run:
          name: Go Vet
          command: go vet ./...
      - run:
          name: Run Tests
          command: |
            trap "go-junit-report <${TEST_RESULTS}/go-test.out > ${TEST_RESULTS}/go-test-report.xml" EXIT
            make test | tee ${TEST_RESULTS}/go-test.out
      - save_cache:
          key: dependency-cache-{{ checksum "go.sum" }}
          paths:
            - /go/pkg/mod
      - run:
          name: Publish coverage info to codecov.io
          command: bash <(curl -s https://codecov.io/bash)
      - store_artifacts:
          path: /tmp/test-results
          destination: raw-test-output
      - store_test_results:
          path: /tmp/test-results
  qri_benchmarks:
    working_directory: ~/perf
    docker:
      - image: alpine:latest
    resource_class: medium+
    steps:
      - checkout:
          path: ~/perf
      - run: "apk add --update curl bash"
      - run:
          name: Install Qri
          command: curl -fsSL https://qri.io/install.sh | sh && qri setup
      - run:
          name: Run User Benchmarks
          command: .circleci/user_benchmarks.sh
      - run: head -n 20 perf.csv
      - store_artifacts:
          path: perf.csv
          destination: perf.csv
      - run:
          name: Publish Results to Qri
          command: |
            qri add qri_eng/qri_cli_user_benchmarks &&\
            qri save --body ./perf.csv qri_eng/qri_cli_user_benchmarks &&\
            qri publish qri_eng/qri_cli_user_benchmarks

workflows:
  version: 2
  test:
    jobs:
      - build
  perf:
    jobs:
    - qri_benchmarks:
        filters:
          branches:
            only: master
