Skip to content

Commit

Permalink
add exception for InvalidMeasurement, fix up coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kratsg committed Sep 20, 2018
1 parent 1c5d25e commit 96b161e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pyhf/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import os
import jsonpatch
import sys

from . import readxml
from . import writexml
Expand All @@ -31,6 +32,7 @@ def xml2json(entrypoint_xml, basedir, output_file, track_progress):
with open(output_file, 'w+') as out_file:
json.dump(spec, out_file, indent=4, sort_keys=True)
log.debug("Written to {0:s}".format(output_file))
sys.exit(0)

@pyhf.command()
@click.argument('workspace', default='-')
Expand All @@ -42,6 +44,7 @@ def json2xml(workspace, xmlfile, specroot, dataroot):
d = json.load(specstream)
with click.open_file(xmlfile, 'w') as outstream:
outstream.write(writexml.writexml(d, specroot, dataroot,'').decode('utf-8'))
sys.exit(0)

@pyhf.command()
@click.argument('workspace', default='-')
Expand All @@ -58,6 +61,7 @@ def cls(workspace, output_file, measurement, qualify_names, patch):
log.debug('measurements defined:\n\t{0:s}'.format('\n\t'.join(measurement_names)))
if measurement and measurement not in measurement_names:
log.error('no measurement by name \'{0:s}\' exists, pick from one of the valid ones above'.format(measurement))
sys.exit(1)
else:
if not measurement and len(measurements) > 1:
log.warning('multiple measurements defined. Taking the first measurement.')
Expand All @@ -80,3 +84,4 @@ def cls(workspace, output_file, measurement, qualify_names, patch):
with open(output_file, 'w+') as out_file:
json.dump(result, out_file, indent=4, sort_keys=True)
log.debug("Written to {0:s}".format(output_file))
sys.exit(0)
5 changes: 5 additions & 0 deletions pyhf/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import sys

class InvalidMeasurement(Exception):
"""
InvalidMeasurement is raised when a specified measurement is invalid given the specification.
"""

class InvalidNameReuse(Exception):
pass

Expand Down
10 changes: 10 additions & 0 deletions tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,13 @@ def test_patch_fail(tmpdir, script_runner):
command = 'pyhf cls {0:s} --patch {1:s}'.format(temp.strpath,patch.strpath)
ret = script_runner.run(*shlex.split(command))
assert not ret.success

def test_bad_measurement_name(tmpdir, script_runner):
temp = tmpdir.join("parsed_output.json")
command = 'pyhf xml2json validation/xmlimport_input/config/example.xml --basedir validation/xmlimport_input/ --output-file {0:s}'.format(temp.strpath)
ret = script_runner.run(*shlex.split(command))

command = 'pyhf cls {0:s} --measurement "a-fake-measurement-name"'.format(temp.strpath)
ret = script_runner.run(*shlex.split(command))
assert not ret.success
#assert 'no measurement by name' in ret.stderr # numpy swallows the log.error() here, dunno why

0 comments on commit 96b161e

Please sign in to comment.