# Install and run unit tests with busted
# Docs: http://olivinelabs.com/busted/

name: Nginx Lua Unit Tests

on:
  push:
    branches:
      - master
    paths:
      - docker/nginx/libs/**
  pull_request:
    paths:
      - docker/nginx/libs/**

jobs:
  test:
    runs-on: ubuntu-latest
    container: openresty/openresty:1.19.9.1-focal
    steps:
      - uses: actions/checkout@v3

      - name: Install Dependencies
        run: |
          luarocks install lua-resty-http
          luarocks install hasher
          luarocks install busted
          luarocks install luacov
          luarocks install luacheck

      - name: Lint Code With Luacheck
        run: luacheck docker/nginx/libs --std ngx_lua+busted

      - name: Run Tests With Busted
        # ran from root repo directory; produces luacov.stats.out file
        run: docker/nginx/testing/rbusted --lpath='docker/nginx/libs/?.lua;docker/nginx/libs/?/?.lua' --verbose --coverage --pattern=spec docker/nginx/libs

      - name: Generate Code Coverage Report With Luacov
        # requires config file in cwd; produces luacov.report.out file
        run: cp docker/nginx/testing/.luacov . && luacov && rm .luacov

      - uses: codecov/codecov-action@v3
        with:
          root_dir: ${GITHUB_WORKSPACE}
          files: ./luacov.report.out
          flags: nginx-lua
