#!/bin/bash
# This is a little helper script that is used to run unit
# tests that may be defined in .lua files.
# The "protocol" is that the module defines a mod:test() method
# that will be called to perform regression tests
CARGO_TARGET_DIR=${CARGO_TARGET_DIR:-${PWD}/target}

for candidate in "${CARGO_TARGET_DIR}/debug/kumod" "${CARGO_TARGET_DIR}/release/kumod" /opt/kumomta/sbin/kumod ;  do
  if test -x "${candidate}" ; then
    KUMOD="${candidate}"
    break;
  fi
done

if ! test -x "${KUMOD}" ; then
  echo "Couldn't find kumod"
  exit 1
fi

script=$(mktemp)
trap "rm -f -- '$script'" EXIT
cat >${script} <<-EOT
local kumo = require 'kumo'
package.path = 'assets/?.lua;' .. package.path

kumo.on('main', function()

local function test_module(name)
  local mod = require(name)
  mod:test()
end

test_module 'policy-extras.listener_domains'
test_module 'policy-extras.queue'
test_module 'policy-extras.sources'
test_module 'policy-extras.typing'

end)
EOT

${KUMOD} --user `id -un` --policy $script --script

