Skip to content

Commit

Permalink
test-runner proof-of-concept: running simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfalcao committed Aug 14, 2023
1 parent 3d9ede2 commit 0869562
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
38 changes: 24 additions & 14 deletions sure/importer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
import importlib
import importlib.util
Expand Down Expand Up @@ -34,30 +35,39 @@ def get_root_python_module(path) -> Path:
class importer(object):
@classmethod
def load_recursive(cls, path, ignore_errors=True, glob_pattern='*.py'):
modules = []
path = Path(path)
if path.is_file():
return cls.load_python_file(path)

base_path = Path(path).expanduser().absolute()
targets = list(base_path.glob(glob_pattern))
for file in targets:
if file.is_dir():
logger.debug(f'ignoring directory {file}')
continue
modules.extend(cls.load_python_file(file))

if file.name.startswith('_') or file.name.endswith('_'):
continue
return modules

@classmethod
def load_python_file(cls, file):
if file.is_dir():
logger.debug(f'ignoring directory {file}')
return

module, root = cls.dig_to_root(file)
__ROOTS__[str(root)] = root
if file.name.startswith('_') or file.name.endswith('_'):
return

return list(__MODULES__.values())
module, root = cls.dig_to_root(file)
__ROOTS__[str(root)] = root
return [module]

@classmethod
def dig_to_root(cls, file):
root = get_root_python_module(file)
module_is_not_artificial = file.name != '__init__.py' and file.parent.joinpath('__init__.py').exists()
if module_is_not_artificial:
module_name = file.parent.name
import ipdb;ipdb.set_trace()
else:
module_name = file.parent.name
module_is_artificial = file.parent.joinpath('__init__.py').exists()
module_name = file.parent.name
if not module_is_artificial:
relative = str(file.relative_to(root.parent))
module_name = os.path.splitext(relative)[0].replace(os.sep, '.')

spec = importlib.util.spec_from_file_location(module_name, file)
module = importlib.util.module_from_spec(spec)
Expand Down
19 changes: 9 additions & 10 deletions sure/reporters/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@

sh = Shell()

checkmark = '\xe2\x9c\x94'
ballot = '\xe2\x9c\x99'
checkmark = ''
ballot = ''


class SpecReporter(Reporter):
name = 'spec'

def on_start(self):
self.indentation = 0
sh.bold_white("Running sure version ")
sh.bold_yellow(sure.version)
sh.bold_green(checkmark)
# sh.bold_white("Running sure version ")
# sh.bold_yellow(sure.version)
sh.reset("\n")

def on_suite(self, suite):
Expand All @@ -47,11 +46,11 @@ def on_suite(self, suite):
sh.reset("\n")

def on_suite_done(self, suite, result):
sh.reset(" " * self.indentation)
sh.bold_white("[")
sh.bold_black(suite.name)
sh.bold_white("]")
sh.bold_white(checkmark)
# sh.reset(" " * self.indentation)
# sh.bold_white("[")
# sh.bold_black(suite.name)
# sh.bold_white("]")
# sh.bold_white(checkmark)
sh.reset("\n\n")
self.indentation = 0

Expand Down
24 changes: 14 additions & 10 deletions sure/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class FinalTestSuiteResult(Result):

class TestSuite(object):
def __init__(self, module):
name = getattr(module, 'suite_name', module.__name__)
description = getattr(module, 'suite_description', "")
name = getattr(module, 'suite_name', getattr(module, 'scenario', getattr(module, 'name', module.__name__)))
description = getattr(module, 'suite_description', getattr(module, 'description', ""))

self.name = stripped(name)
self.description = stripped(description)
Expand All @@ -99,7 +99,7 @@ def __init__(self, module):
self.testcases = []

def load_cases(self, executables):
self.testcases = map((lambda e: TestCase(e, self)), executables)
self.testcases = list(map((lambda e: TestCase(e, self)), executables))
self.ready = True
return self.testcases

Expand Down Expand Up @@ -159,10 +159,13 @@ def run_object(self, context):

# maybe sure should have a `Callable` class that just takes a
# context and abstracts the way to call the callable.
if self.object.func_code.co_argcount == 1:

if self.object.__code__.co_argcount == 1:
return self.object(context)
else:
elif self.object.__code__.co_argcount == 0:
return self.object()
else:
raise RuntimeError(f'it appears that the test function {self.object} takes more than one argument')

def run(self, context):
try:
Expand Down Expand Up @@ -193,10 +196,11 @@ def get_reporter(self, name):

def find_candidates(self, lookup_paths):
candidate_modules = []
for path in map(os.path.abspath, lookup_paths):
modules = importer.load_recursive(path)
modules = importer.load_recursive(path)
# import ipdb;ipdb.set_trace()
for path in lookup_paths:
modules = importer.load_recursive(
path,
glob_pattern='test*.py'
)
candidate_modules.extend(modules)

return candidate_modules
Expand All @@ -211,7 +215,7 @@ def is_runnable_test(self, item):

def extract_members(self, candidate):
all_members = [m[1] for m in inspect.getmembers(candidate)]
members = filter(self.is_runnable_test, all_members)
members = list(filter(self.is_runnable_test, all_members))
return candidate, members

def load_suites(self, lookup_paths):
Expand Down
2 changes: 2 additions & 0 deletions tests/runner/test_eins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

scenario = "test one"


def test_assert():
"testing a simple assertion with sure runner"
Expand Down

0 comments on commit 0869562

Please sign in to comment.