Skip to content

Commit

Permalink
Filter tests relying on stack
Browse files Browse the repository at this point in the history
  • Loading branch information
AryazE committed Aug 27, 2022
1 parent 6a797fe commit ba8a799
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
11 changes: 7 additions & 4 deletions experiment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
[ -d venvs ] || mkdir venvs
[ -d under_test ] || mkdir under_test

alias python=python3.8
python -m pip install --user virtualenv

# Create virtual environtment
venv=$1_$3_env
virtualenv --python python3.8 venvs/$venv
virtualenv --python 3.9.0 venvs/$venv
source venvs/$venv/bin/activate

exactTime=`date +%y%m%d%H%M%S`
Expand All @@ -23,7 +22,11 @@ pip install .
# Install requirements for package under test
cp -r ./test/PythonRepos/$1 ./under_test/$1_$3
cd ./under_test/$1_$3
printf "import pytest\n\npytest.main(['-n', '8', '--import-mode=importlib', '$(pwd)/$2/'])\n" > run_all_tests.py
printf "import pytest\n\npytest.main(['-n', '8', '--import-mode=importlib'" > run_all_tests.py
while read line; do
printf ", '--deselect=$line'" >> run_all_tests.py
done < ../$1_tmp/filtered.txt
printf ", '$(pwd)/$2/'])\n" >> run_all_tests.py
[ -f requirements.txt ] && pip install -r requirements.txt

if [ $3 = "original" ]; then
Expand All @@ -41,5 +44,5 @@ else
fi

cd ../..
deactivate
source deactivate
rm -rf venvs/$venv
31 changes: 31 additions & 0 deletions extract_bad_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
# $1: repo name
# $2: test directory in repository root

[ -d venvs ] || mkdir venvs
[ -d under_test ] || mkdir under_test

python -m pip install --user virtualenv

# Create virtual environtment
venv=$1_tmp_env
virtualenv --python 3.9.0 venvs/$venv
source venvs/$venv/bin/activate

# Install requirements for package under test
cp -r ./test/PythonRepos/$1 ./under_test/$1_tmp
cp filter_tests.py ./under_test/$1_tmp
cd ./under_test/$1_tmp

pip install pytest

[ -f requirements.txt ] && pip install -r requirements.txt

# Install package
[ -f myInstall.sh ] && bash ./myInstall.sh || pip install .
# Run tests
python filter_tests.py $(pwd) $2

cd ../..
source deactivate
rm -rf venvs/$venv
41 changes: 41 additions & 0 deletions filter_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import linecache
import sys
import pytest

bad_tests = set()
results = []
print(' '.join(sys.argv))
test_dir = sys.argv[2]

def find_testfile(frame, package_test_dir):
while frame is not None:
if package_test_dir in frame.f_code.co_filename:
ln = frame.f_lineno
current_line = linecache.getline(frame.f_code.co_filename, ln)
current_indent = len(current_line) - len(current_line.lstrip())
while ln > 0:
ln -= 1
this_line = linecache.getline(frame.f_code.co_filename, ln)
if len(this_line) - len(this_line.lstrip()) < current_indent and this_line.lstrip().startswith('def '):
bad_tests.add((frame.f_code.co_filename + '::' + linecache.getline(frame.f_code.co_filename, ln).strip()[4:].split('(')[0]).split('/')[-1])
break
frame = frame.f_back

def uses_stack(frame, event, arg=None):
code = frame.f_code
access_path_names = code.co_names
file_name = code.co_filename

if '.'.join(access_path_names) in ['sys._getframe', 'sys.exc_info', 'inspect.stack']:
find_testfile(frame, test_dir)
elif file_name.endswith('inspect.py') and any(map(lambda x: x in linecache.getline(file_name, frame.f_lineno), ['trace', 'stack', 'currentframe', 'getinnerframes', 'getouterframes', 'getlineno', 'getframeinfo'])):
find_testfile(frame, test_dir)
return uses_stack

sys.settrace(uses_stack)

pytest.main([sys.argv[1] + '/' + test_dir])

print('\n'.join(bad_tests))
with open('filtered.txt', 'w') as f:
f.write('\n'.join(bad_tests))
2 changes: 0 additions & 2 deletions src/dynapyt/runtime.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from sys import exc_info
import libcst as cst
from copy import copy
from dynapyt.utils.Dynapyt_Unidefined import Dynapyt_Undefined
from dynapyt.utils.hooks import snake, get_name

analysis = None
Expand Down

0 comments on commit ba8a799

Please sign in to comment.