Skip to content

Commit

Permalink
[MERGE]
Browse files Browse the repository at this point in the history
* from PR #78 (77-refactor-error-suppression):
  [CONFIGURATION] Added support for setting the default error logging path in the main Makefile (- WIP #77 -)

* From PR #82 patch-145-fix-setup-cov:
  [PATCH] Minor improvements to 'check_legacy_setup_coverage' (- WIP #77 -)

* From PR #83 (patch-150-81-test):
  [TESTS] Testing possible fix for PYL-C0414 (- WIP #80 -)

Changes in file Makefile:
 - New variable 'ERROR_LOG_PATH' allows for improved debugging when configured.

Changes in file tests/__init__.py:
 - Possible fix for PYL-C0414

Changes in file tests/check_legacy_setup_coverage:
 - Improved variable use allows for improved debugging when configured.

Changes in file tests/context.py:
 - Possible fix for PYL-C0414

Changes in file tests/profiling.py:
 - Possible fix for PYL-C0414
 - Improved documentation slightly

Changes in file tests/test_basic.py:
 - Possible fix for PYL-C0414

Changes in file tests/test_usage.py:
 - Possible fix for PYL-C0414
  • Loading branch information
reactive-firewall committed Sep 12, 2024
3 parents 21e0ab1 + 82462df + ff5a77e commit 690aeed
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 64 deletions.
3 changes: 2 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@


__module__ = """tests"""
"""This is multicast testing module."""

try:
import sys
Expand Down Expand Up @@ -86,7 +87,7 @@
sys.path.insert(0, os.path.abspath(os.path.join(_BASE_NAME, _PARENT_DIR_NAME)))
if 'tests' in __file__:
sys.path.insert(0, os.path.abspath(os.path.join(_BASE_NAME, _DIR_NAME)))
from tests import profiling as profiling
from tests import profiling as profiling # skipcq: PYL-C0414
from tests import test_basic
from tests import test_usage

Expand Down
62 changes: 35 additions & 27 deletions tests/check_legacy_setup_coverage
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ test -x "$(command -v shlock)" || exit 126 ;
test -x $(command -v make) || exit 126 ;
test -x $(command -v tee) || exit 126 ;

LOG_FILE="test_log_${PPID}.log"
ERR_FILE="test_log_errors_${PPID}.log"
LOG_FILE="test_setup_log_${PPID}.log"
ERR_FILE="test_setup_log_errors_${PPID}.log"

function cleanup() {
rm -f ./${LOG_FILE} 2>/dev/null || : ;
Expand All @@ -99,52 +99,60 @@ if [[ ( $(shlock -f ${LOCK_FILE} -p $$ ) -eq 0 ) ]] ; then
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || true || true ; wait ; exit 137 ;' SIGABRT || EXIT_CODE=137
trap 'cleanup 2>/dev/null || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || true ; wait ; exit ${EXIT_CODE} ;' EXIT || EXIT_CODE=1
else
printf "%s\n" "Check Setup Scripts Tests Coverage already in progress by "`head ${LOCK_FILE}` ;
# shellcheck disable=SC2046
printf "\t%s\n" "Check Setup Scripts Tests Coverage already in progress by "$(head "${LOCK_FILE}") >&2 ;
exit 126 ;
fi

# this is how test files are found:

# THIS IS THE ACTUAL TEST
_TEST_ROOT_DIR="./" ;
if [[ -d ./multicast ]] ; then
_TEST_ROOT_DIR="./" ;
elif [[ -d ./tests ]] ; then
_TEST_ROOT_DIR="./" ;
_TEST_ROOT_DIR=$(git rev-parse --show-toplevel 2>/dev/null) ;
if [[ -d ../.git ]] ; then
_TEST_ROOT_DIR="../" ;
elif [[ -d ./.git ]] ; then
_TEST_ROOT_DIR=$(pwd) ;
elif [[ ( -d $(git rev-parse --show-toplevel 2>/dev/null) ) ]] ; then
_TEST_ROOT_DIR=$(git rev-parse --show-toplevel 2>/dev/null) ;
else
printf "%s\n" "FAIL: missing valid folder or file"
EXIT_CODE=1
printf "\t%s\n" "FAIL: missing valid repository or source structure" >&2 ;
EXIT_CODE=40
fi

export COVERAGE_CMD="$(command -v python3) -m coverage run --include=setup.py -p"
if [[ ( -x $(command -v python3) ) ]] ; then
export COVERAGE_CMD="$(command -v python3) -m coverage run --include=setup.py -p"
elif [[ ( -x $(command -v coverage) ) ]] ; then
export COVERAGE_CMD="$(command -v coverage) run --include=setup.py -p"
fi

make -j1 -f Makefile test-reports || EXIT_CODE=3 ;
printf "%s\n" "Start of Log:" > ./${LOG_FILE} ; wait ;
printf "%s\n" "" >> ./${LOG_FILE} ; wait ;
printf "%s\n" "Start of Setup.py Test one-shot:" >> ./${LOG_FILE} ; wait ;
printf "%s\n" "" >> ./${LOG_FILE} ; wait ;
make -C ${_TEST_ROOT_DIR} -j1 -f Makefile test-reports || EXIT_CODE=3 ;
printf "%s\n" "Start of Log:" > ${_TEST_ROOT_DIR}/${LOG_FILE} ; wait ;
printf "%s\n" "" >> ${_TEST_ROOT_DIR}/${LOG_FILE} ; wait ;
printf "%s\n" "Start of Setup.py Test one-shot:" >> ${_TEST_ROOT_DIR}/${LOG_FILE} ; wait ;
printf "%s\n" "" >> ${_TEST_ROOT_DIR}/${LOG_FILE} ; wait ;

{ ${COVERAGE_CMD} ./setup.py --version 2>./${ERR_FILE} | grep --count -oE "^([0-9].){2}[0-9]$" 2>./${ERR_FILE} || echo "FAIL: Setup.py version check failed" 1>&2 ;} 2>./${ERR_FILE} >> ./${LOG_FILE} & true ;
{ ${COVERAGE_CMD} setup.py --name --version --license 2>${_TEST_ROOT_DIR}/${ERR_FILE} | grep --count -oE "^([0-9].){2}[0-9]$" 2>${_TEST_ROOT_DIR}/${ERR_FILE} || printf "%s\n" "FAIL: Setup.py version check failed" 1>&2 ;} 2>${_TEST_ROOT_DIR}/${ERR_FILE} >> ${_TEST_ROOT_DIR}/${LOG_FILE} & : ;

wait ;
printf "%s\n" "End of Setup.py Test one-shot" >> ./${LOG_FILE} ; wait ;
printf "%s\n" "" >> ./${LOG_FILE} ; wait ;
printf "%s\n" "End of Setup.py Test one-shot" >> ${_TEST_ROOT_DIR}/${LOG_FILE} ; wait ;
printf "%s\n" "" >> ${_TEST_ROOT_DIR}/${LOG_FILE} ; wait ;

$(command -v python3) -m coverage combine 2>/dev/null || EXIT_CODE=2 ;
$(command -v python3) -m coverage xml --include=setup.py,multicast -o ./test-reports/coverage_setup.xml || EXIT_CODE=2 ;
${COVERAGE_CMD} combine 2>${_TEST_ROOT_DIR}/${ERR_FILE} || EXIT_CODE=2 ;
${COVERAGE_CMD} xml --include=setup.py,multicast -o ${_TEST_ROOT_DIR}/test-reports/coverage_setup.xml || EXIT_CODE=2 ;

wait ;
cp -f ./${LOG_FILE} ./test-reports/legacy_setup_version_log.log 2>/dev/null ; wait ;
printf "%s\n" "" | tee -a ./test-reports/legacy_setup_version_log.log ; wait ;
cp -f ./${LOG_FILE} ${_TEST_ROOT_DIR}/test-reports/legacy_setup_version_log.log 2>./${ERR_FILE} ; wait ;
printf "%s\n" "" | tee -a ${_TEST_ROOT_DIR}/test-reports/legacy_setup_version_log.log ; wait ;

if [[ ( -r ./${ERR_FILE} ) ]] ; then
printf "%s\n" "Errors: " | tee -a ./test-reports/legacy_setup_version_log.log ; wait ;
cat <./${ERR_FILE} | tee -a ./test-reports/legacy_setup_version_log.log ; wait ;
if [[ ( -r ${_TEST_ROOT_DIR}/${ERR_FILE} ) ]] ; then
printf "%s\n" "Errors: " | tee -a ${_TEST_ROOT_DIR}/test-reports/legacy_setup_version_log.log ; wait ;
cat <${_TEST_ROOT_DIR}/${ERR_FILE} | tee -a ${_TEST_ROOT_DIR}/test-reports/legacy_setup_version_log.log ; wait ;
fi

unset COVERAGE_CMD 2>/dev/null || : ;
unset _TEST_ROOT_DIR 2>/dev/null || : ;

cleanup || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || true ; wait ;
cleanup || rm -f ${LOCK_FILE} 2>/dev/null > /dev/null || : ; wait ;

# goodbye
exit ${EXIT_CODE:-255} ;
4 changes: 2 additions & 2 deletions tests/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

try:
if 'Process' not in sys.modules:
from multiprocessing import Process as Process
from multiprocessing import Process as Process # skipcq: PYL-C0414
else: # pragma: no branch
Process = sys.modules["""Process"""]
except Exception: # pragma: no branch
Expand All @@ -100,7 +100,7 @@

try:
if 'tests.profiling' not in sys.modules:
import profiling as profiling
import tests.profiling as profiling
else: # pragma: no branch
profiling = sys.modules["""tests.profiling"""]
except Exception: # pragma: no branch
Expand Down
88 changes: 59 additions & 29 deletions tests/profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,60 +29,90 @@
# NO ASSOCIATION


__module__ = """tests.profiling"""
"""This is pythonrepo testing module Template."""


try:
import sys
if sys.__name__ is None: # pragma: no branch
raise ImportError("[CWE-758] OMG! we could not import sys! ABORT. ABORT.")
except Exception as err: # pragma: no branch
raise ImportError(err)
except Exception as badErr: # pragma: no branch
baton = ImportError(badErr, str("[CWE-758] Test module failed completely."))
baton.module = __module__
baton.path = __file__
baton.__cause__ = badErr
raise baton


try:
if 'os' not in sys.modules:
import os
else: # pragma: no branch
os = sys.modules["""os"""]
except Exception: # pragma: no branch
raise ImportError("[CWE-758] OS Failed to import.")
except Exception as badErr: # pragma: no branch
baton = ImportError(badErr, str("[CWE-758] Test module failed completely."))
baton.module = __module__
baton.path = __file__
baton.__cause__ = badErr
raise baton


try:
if 'functools' not in sys.modules:
import functools
else: # pragma: no branch
functools = sys.modules["""functools"""]
except Exception as badErr: # pragma: no branch
baton = ImportError(badErr, str("[CWE-758] Test module failed completely."))
baton.module = __module__
baton.path = __file__
baton.__cause__ = badErr
raise baton


try:
import time
if time.__name__ is None: # pragma: no branch
raise NotImplementedError("[CWE-440] We could not import time. Are we in the speed-force!")
except Exception as err:
raise ImportError(err)
exit(3)
except Exception as badErr: # pragma: no branch
baton = ImportError(badErr, str("[CWE-758] Test module failed completely."))
baton.module = __module__
baton.path = __file__
baton.__cause__ = badErr
raise baton


try:
import cProfile
if cProfile.__name__ is None: # pragma: no branch
raise NotImplementedError("[CWE-440] We could not import cProfile. ABORT!")
except Exception as err: # pragma: no branch
raise ImportError(err)
exit(3)
if 'cProfile' not in sys.modules:
import cProfile
else: # pragma: no branch
cProfile = sys.modules["""cProfile"""]
except Exception as badErr: # pragma: no branch
baton = ImportError(badErr, str("[CWE-758] Test module failed completely."))
baton.module = __module__
baton.path = __file__
baton.__cause__ = badErr
raise baton


try:
try:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), str('..'))))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), str('.'))))
except Exception as ImportErr: # pragma: no branch
print(str(''))
print(str(type(ImportErr)))
print(str(ImportErr))
print(str((ImportErr.args)))
print(str(''))
ImportErr = None
del ImportErr
raise ImportError(str("[CWE-758] Profile module failed completely."))
except Exception: # pragma: no branch
raise ImportError("[CWE-440] Failed to import test profiling")
raise ImportError(ImportErr, str("[CWE-758] Profile module failed completely."))
except Exception as badErr: # pragma: no branch
baton = ImportError(badErr, str("[CWE-758] Test module failed completely."))
baton.module = __module__
baton.path = __file__
baton.__cause__ = badErr
raise baton


class timewith():
"""Basic timer for do_time_profile."""

def __init__(self, name=''):
self.name = name
self.start = time.time()
Expand All @@ -103,13 +133,12 @@ def checkpoint(self, name=''):
def __enter__(self):
return self

def __exit__(self, type, value, traceback):
def __exit__(self, type, value, traceback): # skipcq: PYL-W0622
self.checkpoint(str("finished"))
pass


def do_time_profile(func, timer_name="time_profile"):
"""Runs a function with a timer.
"""Run a function with a timer.
Time Testing:
Expand All @@ -136,12 +165,11 @@ def do_time_profile(func, timer_name="time_profile"):
work...took ... seconds
>>>
"""
import functools
"""
@functools.wraps(func)
def timer_profile_func(*args, **kwargs):
"""Wraps a function in timewith()"""
"""Wraps a function in timewith() function."""
theOutput = None
with timewith(timer_name) as timer:
timer.checkpoint(str("Start Timer"))
Expand Down Expand Up @@ -183,7 +211,9 @@ def do_cprofile(func):
"""
@functools.wraps(func)
def profiled_func(*args, **kwargs):
"""Wraps a function in profile.enable/disable() functions."""
profile = cProfile.Profile()
try:
profile.enable()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
if context.__name__ is None:
raise ImportError("[CWE-758] Failed to import context")
else:
from context import unittest as unittest
from context import unittest
from context import sys as _sys
except Exception: # pragma: no branch
raise ImportError("[CWE-758] Failed to import test context")
Expand Down
8 changes: 4 additions & 4 deletions tests/test_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
if context.__name__ is None:
raise ImportError("[CWE-758] Failed to import context")
else:
from context import multicast as multicast
from context import unittest as unittest
from context import subprocess as subprocess
from context import Process as Process
from context import multicast as multicast # skipcq: PYL-C0414
from context import unittest
from context import subprocess
from context import Process
except Exception:
raise ImportError("[CWE-758] Failed to import test context")

Expand Down

0 comments on commit 690aeed

Please sign in to comment.