network/tests/setup_module_utils.sh
Rich Megginson dec163940e support testing/linting module_utils code
This does not directly affect network because network has its own
solution for dealing with testing/linting module_utils code, but
it gets network to feature/code parity with the template and
other repos.

Defines a shell function in utils.sh - lsr_setup_module_utils -
that allows configuring a tox venv installed ansible to have
the repo module_utils code in the default pythonpath for the
venv, which allows testing and linting the module_utils code
without having to otherwise munge or mock the pythonpath.

Projects needing this functionality can set in `.travis/config.sh`
the variable `RUN_PYTEST_SETUP_MODULE_UTILS` to setup
module_utils/ for pytest, and `RUN_PYLINT_SETUP_MODULE_UTILS`
to setup module_utils/ for pylint.
2020-04-06 19:14:42 +02:00

41 lines
844 B
Bash
Executable file

#!/bin/bash
# SPDX-License-Identifier: MIT
set -euo pipefail
if [ -n "${DEBUG:-}" ] ; then
set -x
fi
if [ ! -d "${1:-}" ] ; then
echo Either ansible is not installed, or there is no ansible/module_utils
echo in $1 - Skipping
exit 0
fi
if [ ! -d "${2:-}" ] ; then
echo Role has no module_utils - Skipping
exit 0
fi
# we need absolute path for $2
absmoddir=$( readlink -f "$2" )
# clean up old links to module_utils
for item in "$1"/* ; do
if lnitem=$( readlink "$item" ) && test -n "$lnitem" ; then
case "$lnitem" in
*"${2}"*) rm -f "$item" ;;
esac
fi
done
# add new links to module_utils
for item in "$absmoddir"/* ; do
case "$item" in
*__pycache__) continue;;
*.pyc) continue;;
esac
bnitem=$( basename "$item" )
ln -s "$item" "$1/$bnitem"
done