version: 2
jobs:
  build:
    branches:
      ignore:
        - deploy-doc
        - gh-pages
    docker:
      - image: ocaml/opam:debian-ocaml-5.1
        environment:
          TERM: xterm
      - image: cimg/postgres:14.1
        environment:
          POSTGRES_USER: test
          POSTGRES_DB: test
          POSTGRES_PASSWORD: ""

    steps:
      - checkout
      - run:
          name: Pin packages
          command: |
            version=`egrep '^## v[0-9]' CHANGES.md | head -1 | sed 's/^## v\(.*\) - .*/\1/'`
            for f in *.opam; do
              # FIXME: Restore when packages becomes available.
              if [ "$f" != caqti-driver-mariadb.opam ]; then
                echo >>.ci-tmp-packages "${f%.opam}"
                opam pin add -yn "${f%.opam}.${version}.dev" .
              fi
            done
      - run:
          name: Install system dependencies
          command: |
            opam depext -y $(cat .ci-tmp-packages)
      - run:
          name: Install OCaml dependencies
          command: opam install --deps-only -ty $(cat .ci-tmp-packages)
      - run:
          name: Build
          command: opam config exec -- dune build @install
      - run:
          name: Test
          command: |
            echo "
                sqlite3:test.db?busy_timeout=60000
                postgresql://test@localhost/test
            " > testsuite/uris.conf
            opam config exec -- dune runtest
