Skip to content

Commit

Permalink
presents introductory coverage support
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfalcao committed Dec 18, 2023
1 parent be52970 commit 9d5bf29
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ test tests: clean | $(VENV)/bin/pytest # $(VENV)/bin/nosetests # @$(VENV)/bin/no

# run main command-line tool
run: | $(MAIN_CLI_PATH)
$(MAIN_CLI_PATH) tests/runner/test_eins.py
$(MAIN_CLI_PATH) tests/runner/
$(MAIN_CLI_PATH) tests/
$(MAIN_CLI_PATH) --immediate
$(MAIN_CLI_PATH) --with-coverage --cover-branches tests/runner/test_eins.py
$(MAIN_CLI_PATH) --with-coverage --cover-branches tests/runner/
$(MAIN_CLI_PATH) --with-coverage --cover-branches tests/
$(MAIN_CLI_PATH) --with-coverage --cover-branches --immediate

# Pushes release of this package to pypi
push-release: dist # pushes distribution tarballs of the current version
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def read_readme():
return __doc__


install_requires = ["mock", "six"]
tests_require = ["nose"]
install_requires = ["mock", "six", "coverage"]
tests_require = []
version = read_version()

if __name__ == "__main__":
Expand All @@ -104,7 +104,6 @@ def read_readme():
"console_scripts": ["sure = sure.cli:entrypoint"],
},
tests_require=tests_require,
test_suite="nose.collector",
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
Expand Down
12 changes: 4 additions & 8 deletions sure/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,19 @@
@click.option("-i", "--immediate", is_flag=True)
@click.option("-l", "--log-level", type=click.Choice(['none', 'debug', 'info', 'warning', 'error']), help="default='none'")
@click.option("-F", "--log-file", help='path to a log file. Default to SURE_LOG_FILE')
@click.option("-v", "--verbose", is_flag=True, multiple=True)
@click.option("-q", "--quiet", is_flag=True, multiple=True)
def entrypoint(paths, reporter, reporters, immediate, log_level, log_file, verbose, quiet):
@click.option("-c", "--with-coverage", is_flag=True)
@click.option("--cover-branches", is_flag=True)
def entrypoint(paths, reporter, reporters, immediate, log_level, log_file, with_coverage, cover_branches):
if not paths:
paths = glob('test*/**')
else:
paths = flatten(*list(map(glob, paths)))

reporters = reporters and list(reporters) or None
verbosity_level = sum(verbose)
quietness_level = sum(quiet)
verbosity = verbosity_level - quietness_level
quietness = quietness_level - verbosity_level

configure_logging(log_level, log_file)
runner = Runner(resolve_path(os.getcwd()), reporter, reporters)
result = runner.run(paths, immediate=immediate)
result = runner.run(paths, immediate=immediate, with_coverage=with_coverage, cover_branches=cover_branches)

if result:
if result.is_failure:
Expand Down
11 changes: 6 additions & 5 deletions sure/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pathlib import Path

from typing import Dict
from sure.meta import MetaReporter, get_reporter, gather_reporter_names

__path__ = Path(__file__).absolute().parent
Expand Down Expand Up @@ -50,7 +50,7 @@ def __init__(self, runner, *args, **kw):
self.successes = []
self.failures = []
self.errors = []
self.initialize()
self.initialize(*args, **kw)

def initialize(self, *args, **kw):
pass
Expand All @@ -65,11 +65,11 @@ def on_start(self):
from sure.reporter import Reporter
class HelloReporter(Reporter):
class FeatureReporter(Reporter):
def on_start(self):
sys.stderr.write('Reporter.on_start works')
HelloReporter('a <sure.runner.Runner()>').on_start()
FeatureReporter('a <sure.runner.Runner()>').on_start()
"""
raise NotImplementedError

Expand Down Expand Up @@ -105,7 +105,7 @@ def on_feature_done(self, feature):
class feature_done:
name = 'a simple scenario'
Feature_doneReporter('a <sure.runner.Runner()>').on_feature_done(feature_done)
FeatureReporter('a <sure.runner.Runner()>').on_feature_done(feature_done)
"""
raise NotImplementedError

Expand Down Expand Up @@ -231,6 +231,7 @@ def from_name(cls, name):
"""
if not isinstance(name, str):
raise TypeError(f'name should be a {str.__name__} but got the {type(name).__name__} {name} instead')

found = get_reporter(name)
if not found:
raise RuntimeError(
Expand Down
30 changes: 29 additions & 1 deletion sure/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import unittest
import traceback

import coverage

from pathlib import Path
from typing import List, Optional
from functools import lru_cache, cached_property
Expand Down Expand Up @@ -101,10 +103,21 @@ def load_features(self, lookup_paths):

return features

def runin(self, lookup_paths, immediate: bool = False):
def runin(self, lookup_paths, immediate: bool = False, with_coverage: bool = False, cover_branches: bool = False):
results = []
self.reporter.on_start()

coverageopts = {
'auto_data': True,
'cover_pylib': False,
'branch': cover_branches,
'config_file': True,
}
cov = with_coverage and coverage.Coverage(**coverageopts) or None

if cov:
cov.start()

for feature in self.load_features(lookup_paths):
self.reporter.on_feature(feature)
runtime = RuntimeOptions(immediate=immediate)
Expand All @@ -113,15 +126,30 @@ def runin(self, lookup_paths, immediate: bool = False):
result = feature.run(self.reporter, runtime=runtime)
if runtime.immediate:
if result.is_failure:
if cov:
cov.stop()
cov.save()
cov.report()
raise ExitFailure(context, result)

if result.is_error:
if cov:
cov.stop()
cov.save()
cov.report()

raise ExitError(context, result)

results.append(result)
self.reporter.on_feature_done(feature, result)

if cov:
cov.stop()
cov.save()

self.reporter.on_finish()
if cov:
cov.report()

return FeatureResultSet(results)

Expand Down

0 comments on commit 9d5bf29

Please sign in to comment.