Skip to content

Commit

Permalink
Enable test coverage report according to README (#399)
Browse files Browse the repository at this point in the history
Signed-off-by: sschulz92 <bastie92_spam@gmx.de>
  • Loading branch information
sschulz92 authored Oct 2, 2024
1 parent 46e0873 commit e2a3ae7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 35 deletions.
12 changes: 8 additions & 4 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,17 @@ def copy(src, dest):
"""


def run_tests():
def run_tests() -> int:
pp = "PYTHONPATH"
os.environ[pp] = str(ROOT_DIR)
exit_code = 0
for file_name_path in glob.glob(f"{ROOT_DIR}/tests/**/test_*.py", recursive=True):
exit_code = call([sys.executable, file_name_path]
) if exit_code == 0 else exit_code
all_python_test_files = glob.glob(f"{ROOT_DIR}/tests/**/test_*.py", recursive=True)
for i, file_name_path in enumerate(all_python_test_files):
command = ["coverage", "run", file_name_path]
exit_code = call(command) if exit_code == 0 else exit_code
# Keep coverage files
os.rename(".coverage", f".coverage.{i}")
call(["coverage", "combine"])
return exit_code


Expand Down
4 changes: 4 additions & 0 deletions getgauge/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class MultipleImplementationFoundException(Exception):
def __init__(self, message):
super().__init__(message)
self.message = message
27 changes: 14 additions & 13 deletions getgauge/impl_loader.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import importlib
import inspect
import json
import os
import re
import shutil
import sys
import traceback
from os import path
from contextlib import contextmanager
from os import path

from getgauge import logger
from getgauge.registry import registry
from getgauge.util import *
from getgauge.util import get_project_root, get_step_impl_dirs

project_root = get_project_root()
impl_dirs = get_step_impl_dirs()
Expand Down Expand Up @@ -50,9 +51,8 @@ def copy_skel_files():
shutil.copytree(os.path.join(SKEL,path.basename(impl_dirs[0]) ), impl_dirs[0])
logger.info('create {}'.format(os.path.join(env_dir, PYTHON_PROPERTIES)))
shutil.copy(os.path.join(SKEL, PYTHON_PROPERTIES), env_dir)
f = open(requirements_file, 'w')
f.write('getgauge==' + _get_version())
f.close()
with open(requirements_file, 'w', encoding="utf-8") as f:
f.write('getgauge==' + _get_version())
except:
logger.fatal('Exception occurred while copying skel files.\n{}.'.format(traceback.format_exc()))

Expand Down Expand Up @@ -95,10 +95,10 @@ def _import_file(base_dir, file_path):
except:
logger.fatal('Exception occurred while loading step implementations from file: {}.\n{}'.format(rel_path, traceback.format_exc()))

# Inject instace in each class method (hook/step)
# Inject instance in each class method (hook/step)
def update_step_registry_with_class(instance, file_path):
# Resolve the absolute path from relative path
file_path = os.path.abspath(file_path) if '..' in file_path else file_path
file_path = os.path.abspath(file_path) if str(file_path).startswith("..") else file_path
method_list = registry.get_all_methods_in(file_path)
for info in method_list:
class_methods = [x[0] for x in inspect.getmembers(instance, inspect.ismethod)]
Expand All @@ -107,13 +107,14 @@ def update_step_registry_with_class(instance, file_path):
return method_list

def _get_version():
json_data = open(PLUGIN_JSON).read()
data = json.loads(json_data)
with open(PLUGIN_JSON, "r", encoding="utf-8") as json_data:
data = json.loads(json_data.read())
return data[VERSION]

def _has_methods_with_gauge_decoratores(klass):
foo = r"@(step|before_suite|after_suite|before_scenario|after_scenario|before_spec|after_spec|before_step|after_step|screenshot|custom_screen_grabber)"
def _has_methods_with_gauge_decoratores(klass) -> bool:
gauge_decorator_pattern = r"@(step|before_suite|after_suite|before_scenario|after_scenario|before_spec|after_spec|before_step|after_step|screenshot|custom_screen_grabber)"
sourcelines = inspect.getsourcelines(klass)[0]
for i,line in enumerate(sourcelines):
if re.match(foo, line.strip()) != None:
for line in sourcelines:
if re.match(gauge_decorator_pattern, line.strip()) is not None:
return True
return False
19 changes: 8 additions & 11 deletions getgauge/processor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import os
import traceback
from os import environ, path
from threading import Timer
from os import path

from getgauge import logger
from getgauge.executor import (create_execution_status_response,
execute_method, run_hook)
from getgauge.impl_loader import load_impls
Expand All @@ -17,6 +13,7 @@
read_file_contents)
from getgauge.validator import validate_step


def process_validate_step_request(request):
return validate_step(request)

Expand Down Expand Up @@ -47,7 +44,7 @@ def process_refactor_request(request):
refactor_step(request, response)
except Exception as e:
response.success = False
response.error = 'Reason: {}'.format(e.__str__())
response.error = f"Reason: {e}"
return response


Expand Down Expand Up @@ -175,7 +172,7 @@ def _load_from_disk(file_path):
def process_cache_file_request(request):
file = request.filePath
status = request.status
if status == CacheFileRequest.CHANGED or status == CacheFileRequest.OPENED:
if status in [CacheFileRequest.CHANGED, CacheFileRequest.OPENED]:
reload_steps(file, request.content)
elif status == CacheFileRequest.CREATED:
if not registry.is_file_cached(file):
Expand Down Expand Up @@ -225,15 +222,15 @@ def process_stub_impl_request(request):
prefix = "from getgauge.python import step\n"
span = Span(**{'start': 0, 'startChar': 0, 'end': 0, 'endChar': 0})
codes = [prefix] + codes[:]
textDiffs = [TextDiff(**{'span': span, 'content': '\n'.join(codes)})]
text_diffs = [TextDiff(**{'span': span, 'content': '\n'.join(codes)})]
response.filePath = file_name
response.textDiffs.extend(textDiffs)
response.textDiffs.extend(text_diffs)
return response


def process_glob_pattern_request(request):
res = ImplementationFileGlobPatternResponse()
globPatterns = [["{}/**/*.py".format(d)] for d in get_step_impl_dirs()]
glob_patterns = [[f"{d}/**/*.py"] for d in get_step_impl_dirs()]
res.globPatterns.extend(
[item for sublist in globPatterns for item in sublist])
[item for sublist in glob_patterns for item in sublist])
return res
9 changes: 5 additions & 4 deletions getgauge/refactor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from getgauge.messages.messages_pb2 import RefactorResponse, TextDiff
from getgauge.exceptions import MultipleImplementationFoundException
from getgauge.messages.messages_pb2 import TextDiff
from getgauge.messages.spec_pb2 import Span
from getgauge.parser import Parser
from getgauge.registry import registry


def refactor_step(request, response):
if registry.has_multiple_impls(request.oldStepValue.stepValue):
raise Exception('Multiple Implementation found for `{}`'.format(
raise MultipleImplementationFoundException('Multiple Implementation found for `{}`'.format(
request.oldStepValue.parameterizedStepValue
))
info = registry.get_info_for(request.oldStepValue.stepValue)
Expand All @@ -18,7 +19,7 @@ def refactor_step(request, response):
)
content = impl_file.get_code()
if request.saveChanges:
with open(info.file_name, 'w') as f:
with open(info.file_name, 'w', encoding="utf-8") as f:
f.write(content)
response.success = True
response.filesChanged.append(info.file_name)
Expand All @@ -31,6 +32,6 @@ def refactor_step(request, response):

def _new_parameter_positions(request):
moved_pos = list(range(len(request.paramPositions)))
for index, position in enumerate(request.paramPositions):
for position in request.paramPositions:
moved_pos[position.newPosition] = position.oldPosition
return moved_pos
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ twine==5.1.1
urllib3==2.2.3
webencodings==0.5.1
zipp==3.20.2
coverage==7.6.1
6 changes: 3 additions & 3 deletions tests/test_impl_loader.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
import unittest

from test_relative_import.relative_import_class import Sample
from getgauge.impl_loader import update_step_registry_with_class
from test_relative_import.relative_import_class import Sample


class ImplLoaderTest(unittest.TestCase):
def setUp(self):
self.relative_file_path = os.path.join('..', 'test_relative_import', 'relative_import_class.py')

def test_update_step_resgistry_with_class(self):
def test_update_step_registry_with_class(self):
curr_dir = os.getcwd()
os.chdir('tests')
method_list = update_step_registry_with_class(Sample(), self.relative_file_path)
Expand All @@ -18,6 +18,6 @@ def test_update_step_resgistry_with_class(self):
"Greet <name> from outside the class"],
[method.step_text for method in method_list])


if __name__ == '__main__':
unittest.main()

0 comments on commit e2a3ae7

Please sign in to comment.