Skip to content

Commit

Permalink
main: show an error for job on empty / new DB
Browse files Browse the repository at this point in the history
Instead of printing some ugly IndexError backtrace when running `job` on a fresh
install, just print an error message and exit with non-zero exit code.

For `job -g <arg>` it was failing to iterate None from db getActive, so return []
instead.
  • Loading branch information
wwade committed Jun 2, 2021
1 parent e547263 commit 8385418
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
7 changes: 4 additions & 3 deletions jobrunner/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ def recentGet(self):
try:
return json.loads(self.db[self.RECENT])
except json.JSONDecodeError:
return None
return []
else:
return None
return []

def recentSet(self, key):
recent = self.recent
Expand Down Expand Up @@ -462,7 +462,8 @@ def filterJobsWith(job, startTime=True, skipReminders=False):
return True

def getJobMatch(self, key, thisWs, skipReminders=False):
# pylint: disable=too-many-return-statements,too-many-branches
# pylint: disable=too-many-return-statements,too-many-branches,
# pylint: disable=too-many-statements
if key == '.':
lastJob = self.active.lastJob
if lastJob:
Expand Down
11 changes: 8 additions & 3 deletions jobrunner/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ def impl_main(args=None):
elif options.wait:
pass
else:
sprint(jobs.getLog(key=None, thisWs=options.tw, skipReminders=True))
try:
sprint(jobs.getLog(key=None, thisWs=options.tw, skipReminders=True))
except NoMatchingJobError as error:
jobs.unlock()
sprint("Error:", error)
sys.exit(1)
jobs.unlock()
sys.exit(0)

Expand Down Expand Up @@ -235,7 +240,7 @@ def main(args=None):
try:
impl_main(args=args)
except NoMatchingJobError as error:
print(error)
sprint("Error:", error)
sys.exit(1)


Expand Down Expand Up @@ -730,7 +735,7 @@ def maybeHandle(options, jobs, handler):
sys.exit(0)
except NoMatchingJobError as error:
jobs.unlock()
print("Error:", error)
sprint("Error:", error)
sys.exit(1)
except ExitCode as exitCode:
jobs.unlock()
Expand Down
3 changes: 2 additions & 1 deletion jobrunner/test/integration/integration_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def run(cmd, capture=False, env=None):
return check_call(cmd, env=env)
except CalledProcessError as error:
print(error.output)
LOG.debug("cmd %r => ERROR %s", cmd, autoDecode(error.output).strip())
errOut = autoDecode(error.output) if error.output else "n/a"
LOG.debug("cmd %r => ERROR %s", cmd, errOut.strip())
raise


Expand Down
15 changes: 15 additions & 0 deletions jobrunner/test/integration/smoke_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from unittest import TestCase

from pexpect import EOF
import pytest
import simplejson as json
import six

Expand Down Expand Up @@ -60,6 +61,20 @@ def testUnicodeSmoke2():
unicodeCase()


@pytest.mark.parametrize("cmd,expected", [
(['job'], r"^Error: Job database is empty$"),
(['job', '-g', '.'], r"^Error: No job for key .\..$"),
(['job', '-g', 'xxx'], r"^Error: No job for key .xxx.$"),
])
def testEmptyDb(cmd, expected):
with testEnv():
with pytest.raises(CalledProcessError) as error:
run(cmd, capture=True)
output = autoDecode(error.value.output)
print(output)
assert re.match(expected, output)


class SmokeTest(TestCase):
def testBasic(self):
with testEnv() as env:
Expand Down

0 comments on commit 8385418

Please sign in to comment.