version: 2.1
orbs:
  codecov: codecov/codecov@1.1.3
  win: circleci/windows@2.2.0
jobs:
  build_rock:
    description: "Build with luarocks for a specific Lua version"
    parameters:
      lua:
        type: string
    docker:
    - image: circleci/python
    steps:
    - checkout
    - run:
        name: Install debian dependencies
        command: |
          sudo apt -y update
          sudo apt -y install libldap2-dev
    - run:
        name: Install hererocks
        command: |
          pip3 install hererocks
          hererocks bed --<<parameters.lua>> --no-readline --luarocks latest --verbose
          hererocks bed --show
    - run:
        name: "Build (with install)"
        command: |
          source bed/bin/activate
          luarocks make rockspec/lualdap-dev-2.rockspec
    - run:
        name: "Smoke"
        command: |
          source bed/bin/activate
          lua -v
          lua tests/smoke.lua
  test_with_coverage:
    description: "Test with coverage for a specific Lua version"
    parameters:
      lua:
        type: string
    docker:
    - image: circleci/python
    - image: docker.io/openshift/openldap-2441-centos7@sha256:b5619d4de8efec7973dbd280eb2cc462ebeb8ae6e457acef4b743b7374dd600d
    steps:
    - checkout
    - run:
        name: Install debian dependencies
        command: |
          sudo apt -y update
          sudo apt -y install libldap2-dev
          sudo apt -y install ldap-utils netcat
    - run:
        name: Install hererocks
        command: |
          pip3 install hererocks
          hererocks bed --<<parameters.lua>> --no-readline --luarocks latest --verbose
          hererocks bed --show
    - run:
        name: Install rocks dependencies
        command: |
          source bed/bin/activate
          luarocks install busted
          luarocks install cluacov
          luarocks install luacheck
          luarocks list
    - run:
        name: "Build (without install)"
        command: |
          source bed/bin/activate
          make LUA_INCDIR=bed/include COVERAGE=1 src/lualdap.so
    - run:
        name: "Luacheck"
        command: |
          source bed/bin/activate
          make luacheck
    - run:
        name: "Wait for OpenLDAP to spin up"
        command: while ! nc -z localhost 389 ; do sleep 1 ; done
    - run:
        name: Prime and test LDAP DB
        command: |
          make setup_slapd
    - run:
        name: "Test with coverage"
        command: |
          source bed/bin/activate
          make coverage
    - codecov/upload:
        file: "*.gcov"
        flags: default
        upload_name: <<parameters.lua>>
    - store_test_results:
        path: test-reports/
  build_deb:
    description: "Build Debian packages"
    docker:
    - image: circleci/python
    steps:
    - checkout
    - run:
        name: Install debian dependencies
        command: |
          sudo apt -y update
          sudo apt -y install libldap2-dev
          sudo apt -y install fakeroot dh-lua
    - run:
        name: "Build"
        command: |
          rm debian/lua5.4.dh-lua.conf
          make deb
  build_win:
    description: "Build rock on Windows"
    executor: win/default
    steps:
    - checkout
    - run:
        name: Install hererocks
        command: |
          pip install hererocks
          hererocks bed --lua latest --no-readline --luarocks latest --verbose
          hererocks bed --show
    - run:
        name: "Build (with install)"
        command: |
          bed/bin/activate.ps1
          luarocks make rockspec/lualdap-dev-2.rockspec
    - run:
        name: "Smoke"
        command: |
          bed/bin/activate.ps1
          lua -v
          lua tests/smoke.lua
workflows:
  version: 2
  build_rock:
    jobs:
    - build_rock:
        name: "rock <<matrix.lua>>"
        matrix:
           parameters:
            lua: ["lua 5.1", "lua 5.2", "lua 5.3", "lua 5.4", "luajit 2.0", "luajit 2.1"]
  test_with_coverage:
    jobs:
    - test_with_coverage:
        name: "coverage <<matrix.lua>>"
        matrix:
           parameters:
            lua: ["lua 5.1", "lua 5.2", "lua 5.3", "lua 5.4", "luajit 2.0", "luajit 2.1"]
  build_deb:
    jobs:
    - build_deb:
        name: "deb"
  build_win:
    jobs:
    - build_win:
        name: "windows"
