Skip to content

Commit

Permalink
bagit.validate.base: record a stack trace in issue if test raises exc…
Browse files Browse the repository at this point in the history
…eption
  • Loading branch information
RayPlante committed Jul 2, 2024
1 parent 0608623 commit 22019cb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
7 changes: 6 additions & 1 deletion python/nistoar/pdr/preserv/bagit/validate/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module provides the base validator class
"""
import sys, traceback
from abc import ABCMeta, abstractmethod, abstractproperty
from collections import Sequence, OrderedDict

Expand All @@ -11,6 +12,10 @@
PROB = 3
issuetypes = [ ERROR, WARN, REC ]

def _fmt_exc():
return "".join(traceback.format_exception(*sys.exc_info()))


class Validator(object):
"""
a class for validating a bag encapsulated in a directory.
Expand Down Expand Up @@ -433,7 +438,7 @@ def validate(self, bag, want=ALL, results=None, *kw):
out._err( ValidationIssue(self.profile[0], self.profile[1],
"validator failure", ERROR,
"test method, {0}, raised an exception: {1}"
.format(test, str(ex)), False),
.format(test, _fmt_exc()), False),
False )
return out

Expand Down
51 changes: 50 additions & 1 deletion python/tests/nistoar/pdr/preserv/bagit/validate/test_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import os, sys, pdb, json
import os, sys, pdb, json, re
import unittest as test

import nistoar.pdr.preserv.bagit.validate.base as base
import nistoar.pdr.preserv.bagit.bag as bag

datadir = os.path.join( os.path.dirname(os.path.dirname(
os.path.dirname(__file__))), "data" )
bagdir = os.path.join(datadir, "samplembag")

class TestValidationIssue(test.TestCase):

Expand Down Expand Up @@ -82,7 +87,51 @@ def test_description(self):
self.assertEqual(issue.description,
"ERROR: Life 3.1 A1.1: Life must self-replicate\n Little\n green")

class TestValidatorBase(test.TestCase):

def setUp(self):
self.bag = bag.NISTBag(bagdir)

class _TestBase(base.ValidatorBase):
def test_raise_except(self, bag, want=base.ALL, results=None):
out = results
if not out:
out = base.ValidationResults(bag.name, want)

t = self._issue("re-goob", "raise an exception")
re.search(r"goob[g", "gurn") # a deliberate fail via exception
out._rec(t, True)

return out

def test_catch_except(self):
tests = self._TestBase({})

try:
res = tests.test_raise_except(self.bag)
self.fail("failed to raise exception as necessary for test")
except Exception as ex:
pass

res = tests.validate(self.bag)
self.assertEqual(res.count_applied(), 1)
self.assertEqual(res.count_failed(), 1)
self.assertIn("Traceback", res.failed()[0].description)
self.assertIn(", line ", res.failed()[0].description)


def test_fmt_exc(self):
a = {}
try:
a['hello']
self.fail("failed to detect test KeyError")
except Exception as ex:
prob = base._fmt_exc()
self.assertIn('Traceback', prob)
self.assertIn(', line ', prob)
self.assertIn('KeyError', prob)




if __name__ == '__main__':
Expand Down

0 comments on commit 22019cb

Please sign in to comment.