Skip to content

Commit

Permalink
Merge 5181b15 into d7b9aae
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Dec 27, 2020
2 parents d7b9aae + 5181b15 commit e2c3389
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
9 changes: 5 additions & 4 deletions coveragespace/cache.py
Expand Up @@ -17,13 +17,13 @@ def _store(self):
os.makedirs(directory)

text = pickle.dumps(self._data)
with open(self.PATH, 'wb') as fout:
fout.write(text)
with open(self.PATH, 'wb') as f:
f.write(text)

def _load(self):
try:
with open(self.PATH, 'rb') as fin:
text = fin.read()
with open(self.PATH, 'rb') as f:
text = f.read()
except IOError as e:
log.debug("Unable to read cache: %s", e)
return
Expand Down Expand Up @@ -55,6 +55,7 @@ def get(self, key, default=None):
return value

def clear(self):
log.debug("Clearing cache")
self._data = {}
self._store()

Expand Down
45 changes: 26 additions & 19 deletions coveragespace/cli.py
Expand Up @@ -3,6 +3,7 @@
Usage:
coveragespace <owner/repo> <metric> [<value>] [--verbose] [--exit-code]
coveragespace <owner/repo> --reset [--verbose]
coveragespace view
coveragespace (-h | --help)
coveragespace (-V | --version)
Expand All @@ -23,51 +24,51 @@
import log
from docopt import DocoptExit, docopt

from . import API, VERSION, client, services
from .plugins import get_coverage, launch_report
from . import API, VERSION, client, plugins, services


def main():
"""Parse command-line arguments, configure logging, and run the program."""
if services.detected():
log.info("Coverage check skipped when running on CI service")
sys.exit()

colorama.init(autoreset=True)
arguments = docopt(__doc__, version=VERSION)

slug = arguments['<owner/repo>']
metric = arguments['<metric>']
reset = arguments['--reset']
value = arguments['<value>']
verbose = arguments['--verbose']
hardfail = arguments['--exit-code']

log.reset()
log.init(level=log.DEBUG if verbose else log.WARNING)

if '/' not in slug:
if arguments['view']:
success = view()
elif '/' in slug:
success = call(
slug,
arguments['<metric>'],
arguments['<value>'],
arguments['--reset'],
verbose,
hardfail,
)
else:
raise DocoptExit("<owner/repo> slug must contain a slash" + '\n')

success = run(slug, metric, value, reset, verbose, hardfail)

if not success and hardfail:
sys.exit(1)


def run(*args, **kwargs):
"""Run the program."""
if services.detected():
log.info("Coverage check skipped when running on CI service")
return True

return call(*args, **kwargs)


def call(slug, metric, value, reset=False, verbose=False, hardfail=False):
"""Call the API and display errors."""
url = "{}/{}".format(API, slug)
if reset:
data = {metric: None}
response = client.delete(url, data)
else:
data = {metric: value or get_coverage()}
data = {metric: value or plugins.get_coverage()}
response = client.get(url, data)

if response.status_code == 200:
Expand All @@ -85,7 +86,7 @@ def call(slug, metric, value, reset=False, verbose=False, hardfail=False):
message = "To reset metrics, run: ^coveragespace {} --reset$".format(slug)
data['help'] = message # type: ignore
display("coverage decreased", data, color)
launch_report()
plugins.launch_report()
return False

try:
Expand All @@ -107,3 +108,9 @@ def display(title, data, color=""):
message = message.replace('$', colorama.Style.RESET_ALL)
print(message)
print(color + '=' * width)


def view():
"""View the local coverage report."""
plugins.launch_report()
return True
3 changes: 1 addition & 2 deletions coveragespace/tests/test_cache.py
Expand Up @@ -15,8 +15,7 @@ def cache():

@pytest.fixture
def cache_empty(cache):
# pylint: disable=protected-access
cache._data.clear()
cache.clear()
return cache

@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
@@ -1,7 +1,7 @@
[tool.poetry]

name = "coveragespace"
version = "3.2b1"
version = "4.0a1"
description = "A place to track your code coverage metrics."

license = "MIT"
Expand Down Expand Up @@ -77,7 +77,7 @@ pync = { version = "*", platform = "darwin" }

[tool.poetry.scripts]

"coveragespace" = "coveragespace.cli:main"
"coverage.space" = "coveragespace.cli:main"

[tool.black]

Expand Down
8 changes: 8 additions & 0 deletions tests/test_cli.py → tests/test_program.py
Expand Up @@ -101,3 +101,11 @@ def it_can_reset_metrics(env, slug):
expect(cmd.returncode) == 0
expect(cmd.stderr) == ""
expect(cmd.stdout).contains("coverage reset")

def describe_view():
def it_launches_the_local_coverage_report(env):
cmd = cli(env, 'view')

expect(cmd.returncode) == 0
expect(cmd.stderr) == ""
expect(cmd.stdout).contains("")

0 comments on commit e2c3389

Please sign in to comment.