Skip to content

Commit

Permalink
Catch errors if some non-important libs aren't installed
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Turov committed Dec 15, 2021
1 parent 838e514 commit 21cf7d3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 29 deletions.
11 changes: 3 additions & 8 deletions hstest/common/reflection_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import inspect
import os
import sys
import traceback
from typing import List

from hstest.exception.failure_handler import get_traceback_stack


def is_tests(stage):
package = inspect.getmodule(stage).__package__
Expand All @@ -21,12 +21,7 @@ def setup_cwd(stage):


def get_stacktrace(ex: BaseException, hide_internals=False) -> str:
exc_tb = ex.__traceback__

if sys.version_info >= (3, 10):
traceback_stack = traceback.format_exception(ex)
else:
traceback_stack = traceback.format_exception(etype=type(ex), value=ex, tb=exc_tb)
traceback_stack = get_traceback_stack(ex)

if not hide_internals:
return ''.join(traceback_stack)
Expand Down
16 changes: 13 additions & 3 deletions hstest/dynamic/output/output_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ def print(obj):

lines = obj.strip().split('\n')

prepend = f'[{ThreadGroup.curr_group().name}] '
group = ThreadGroup.curr_group()

if group:
name = group.name
else:
name = "Root"

prepend = f'[{name}] '

output = prepend + ('\n' + prepend).join(lines)
full = BLUE + output + '\n' + RESET

OutputHandler.get_real_out().write(full)
OutputHandler.get_real_out().flush()
if group:
OutputHandler.get_real_out().write(full)
OutputHandler.get_real_out().flush()
else:
print(full, end='')

@staticmethod
def get_real_out() -> io.TextIOWrapper:
Expand Down
14 changes: 11 additions & 3 deletions hstest/exception/failure_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import platform
import sys
import traceback
from typing import List

from hstest.testing.execution_options import inside_docker

Expand All @@ -20,7 +22,13 @@ def get_report():
return 'Submitted via web'


def get_traceback_stack(ex: BaseException) -> List[str]:
if sys.version_info >= (3, 10):
return traceback.format_exception(ex)
else:
exc_tb = ex.__traceback__
return traceback.format_exception(etype=type(ex), value=ex, tb=exc_tb)


def get_exception_text(ex: BaseException) -> str:
exc_tb = ex.__traceback__
traceback_stack = traceback.format_exception(etype=type(ex), value=ex, tb=exc_tb)
return ''.join(traceback_stack)
return ''.join(get_traceback_stack(ex))
2 changes: 1 addition & 1 deletion hstest/stage/stage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def run_tests(self, *, debug=False) -> Tuple[int, str]:
if isinstance(new_ex, OutcomeError):
ex = new_ex

build = 'hs-test-python version build 2021.11.10.2'
build = 'hs-test-python'

try:
report = build + "\n\n" + get_report()
Expand Down
2 changes: 2 additions & 0 deletions hstest/testing/execution/main_module_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Optional

from hstest.common.process_utils import DaemonThreadPoolExecutor
from hstest.dynamic.output.output_handler import OutputHandler
from hstest.dynamic.security.exit_exception import ExitException
from hstest.dynamic.security.thread_group import ThreadGroup
from hstest.dynamic.system_handler import SystemHandler
Expand All @@ -16,6 +17,7 @@
class MainModuleExecutor(ProgramExecutor):
def __init__(self, source_name: str = None):
super().__init__()
OutputHandler.print(f'MainModuleExecutor instantiating, source = {source_name}')
self.runnable = PythonSearcher().find(source_name)
self.__executor: Optional[DaemonThreadPoolExecutor] = None
self.__task: Optional[Future] = None
Expand Down
4 changes: 4 additions & 0 deletions hstest/testing/execution/searcher/python_searcher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import re
from typing import Optional

from hstest.dynamic.output.output_handler import OutputHandler
from hstest.testing.execution.filtering.file_filter import FileFilter, Folder, Sources
from hstest.testing.execution.filtering.main_filter import MainFilter
from hstest.testing.execution.runnable.python_runnable_file import PythonRunnableFile
Expand Down Expand Up @@ -48,5 +50,7 @@ def init_regexes(_: Folder, sources: Sources):
)

def find(self, source: Optional[str]) -> PythonRunnableFile:
OutputHandler.print(f'PythonSearcher source = {source}, cwd = {os.getcwd()}')
runnable = super().find(source)
OutputHandler.print(f'PythonSearcher found runnable: {runnable.folder}/{runnable.file}')
return PythonRunnableFile(runnable.folder, runnable.file, runnable.file[:-len(self.extension)])
18 changes: 13 additions & 5 deletions hstest/testing/plotting/matplotlib_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from importlib import reload
from typing import TYPE_CHECKING

import pandas as pd
try:
import pandas as pd
except ImportError:
pass

from hstest.testing.plotting.drawing.drawing import Drawing
from hstest.testing.plotting.drawing.drawing_builder import DrawingBuilder
Expand Down Expand Up @@ -49,10 +52,15 @@ def hist(x, *args, data=None, **kw):
x = data[x]
except: pass

if type(x) == pd.DataFrame:
for col in x.columns:
hist(x[col], *args, **kw)
return
# TODO potential unresolved reference if pandas is not installed, but matplotlib is
# for now wrapped with bare try-except, but need better solution
try:
if type(x) == pd.DataFrame:
for col in x.columns:
hist(x[col], *args, **kw)
return
except:
pass

drawings.append(
DrawingBuilder.get_hist_drawing(
Expand Down
21 changes: 14 additions & 7 deletions hstest/testing/plotting/pandas_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from typing import TYPE_CHECKING

import numpy as np
import pandas as pd
from pandas.api.types import is_numeric_dtype
try:
import numpy as np
except ImportError:
pass

try:
import pandas as pd
from pandas.api.types import is_numeric_dtype
except ImportError:
pass

from hstest.testing.plotting.drawing.drawing import Drawing
from hstest.testing.plotting.drawing.drawing_builder import DrawingBuilder
Expand Down Expand Up @@ -42,7 +49,7 @@ class PandasHandler:
}

@staticmethod
def get_hist_drawings_with_normalized_data(data: pd.DataFrame, x, y):
def get_hist_drawings_with_normalized_data(data: 'pd.DataFrame', x, y):
drawings = []

if y is not None:
Expand Down Expand Up @@ -122,7 +129,7 @@ def get_scatter_drawings_with_normalized_data(data, x, y):
]

@staticmethod
def get_pie_drawings_with_normalized_data(data: pd.DataFrame, x, y):
def get_pie_drawings_with_normalized_data(data: 'pd.DataFrame', x, y):

if type(data) == pd.Series:
drawing = DrawingBuilder.get_pie_drawing(
Expand Down Expand Up @@ -154,7 +161,7 @@ def get_pie_drawings_with_normalized_data(data: pd.DataFrame, x, y):
return drawings

@staticmethod
def get_bar_drawings_with_normalized_data(data: pd.DataFrame, x, y):
def get_bar_drawings_with_normalized_data(data: 'pd.DataFrame', x, y):
drawings = []

if x is not None:
Expand Down Expand Up @@ -183,7 +190,7 @@ def get_bar_drawings_with_normalized_data(data: pd.DataFrame, x, y):
return drawings

@staticmethod
def get_box_drawings_with_normalized_data(data: pd.DataFrame, x, y):
def get_box_drawings_with_normalized_data(data: 'pd.DataFrame', x, y):

drawings = []

Expand Down
7 changes: 5 additions & 2 deletions hstest/testing/plotting/seaborn_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from importlib import reload
from typing import TYPE_CHECKING

import pandas as pd
from pandas.api.types import is_numeric_dtype
try:
import pandas as pd
from pandas.api.types import is_numeric_dtype
except ImportError:
pass

from hstest.testing.plotting.drawing.drawing import Drawing
from hstest.testing.plotting.drawing.drawing_builder import DrawingBuilder
Expand Down

0 comments on commit 21cf7d3

Please sign in to comment.