From 565616dc753bb9d863d0e9c1ba4c05c0d9a16369 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Thu, 7 Mar 2019 15:04:37 -0500 Subject: [PATCH] Expand functionality of our base TestCase classes --- src/allmydata/test/common.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/allmydata/test/common.py b/src/allmydata/test/common.py index 30e6b6818..aac08f5bf 100644 --- a/src/allmydata/test/common.py +++ b/src/allmydata/test/common.py @@ -1,4 +1,9 @@ import os, random, struct +import tempfile +from tempfile import mktemp +from functools import partial +from unittest import case as _case + import treq from zope.interface import implementer @@ -845,11 +850,32 @@ class _TestCaseMixin(object): * Each test method will be run in a unique Eliot action context which identifies the test and collects all Eliot log messages emitted by that test (including setUp and tearDown messages). + * trial-compatible mktemp method + * unittest2-compatible assertRaises helper """ @eliot_logged_test def run(self, result): return super(TestCase, self).run(result) + def setUp(self): + # Restore the original temporary directory. Node ``init_tempdir`` + # mangles it and many tests manage to get that method called. + self.addCleanup( + partial(setattr, tempfile, "tempdir", tempfile.tempdir), + ) + return super(_TestCaseMixin, self).setUp() + + class _DummyCase(_case.TestCase): + def dummy(self): + pass + _dummyCase = _DummyCase("dummy") + + def mktemp(self): + return mktemp() + + def assertRaises(self, *a, **kw): + return self._dummyCase.assertRaises(*a, **kw) + class SyncTestCase(_TestCaseMixin, TestCase): """