config: refactor PreUploadSettingsTests
PreUploadFileTests and PreUploadSettingsTests share the same setup and
teardown for writing configuration files. Extract that behaviour into a
new class (FileTestsBase).
Test: pre-upload.py
Bug: 160223496
Change-Id: I8b572cdb0f43ae4557323a43a69e66ead67d87fe
diff --git a/rh/config_unittest.py b/rh/config_unittest.py
index 596e17b..c8aceb9 100755
--- a/rh/config_unittest.py
+++ b/rh/config_unittest.py
@@ -44,8 +44,8 @@
rh.config.PreUploadConfig()
-class PreUploadFileTests(unittest.TestCase):
- """Tests for the PreUploadFile class."""
+class FileTestCase(unittest.TestCase):
+ """Helper class for tests cases to setup configuration files."""
def setUp(self):
self.tempdir = tempfile.mkdtemp()
@@ -53,13 +53,31 @@
def tearDown(self):
shutil.rmtree(self.tempdir)
- def _write_config(self, data):
- """Helper to write out a config file for testing."""
- path = os.path.join(self.tempdir, 'temp.cfg')
+ def _write_config(self, data, filename='temp.cfg'):
+ """Helper to write out a config file for testing.
+
+ Returns:
+ Path to the file where the configuration was written.
+ """
+ path = os.path.join(self.tempdir, filename)
with open(path, 'w') as fp:
fp.write(data)
return path
+ def _write_local_config(self, data):
+ """Helper to write out a local config file for testing."""
+ return self._write_config(
+ data, filename=rh.config.LocalPreUploadFile.FILENAME)
+
+ def _write_global_config(self, data):
+ """Helper to write out a global config file for testing."""
+ return self._write_config(
+ data, filename=rh.config.GlobalPreUploadFile.FILENAME)
+
+
+class PreUploadFileTests(FileTestCase):
+ """Tests for the PreUploadFile class."""
+
def testEmpty(self):
"""Instantiating an empty config file should be fine."""
path = self._write_config('')
@@ -115,35 +133,16 @@
path)
-class PreUploadSettingsTests(unittest.TestCase):
+class PreUploadSettingsTests(FileTestCase):
"""Tests for the PreUploadSettings class."""
- def setUp(self):
- self.tempdir = tempfile.mkdtemp()
-
- def tearDown(self):
- shutil.rmtree(self.tempdir)
-
- def _write_config(self, data, filename=None):
- """Helper to write out a config file for testing."""
- if filename is None:
- filename = rh.config.LocalPreUploadFile.FILENAME
- path = os.path.join(self.tempdir, filename)
- with open(path, 'w') as fp:
- fp.write(data)
-
- def _write_global_config(self, data):
- """Helper to write out a global config file for testing."""
- self._write_config(
- data, filename=rh.config.GlobalPreUploadFile.FILENAME)
-
def testGlobalConfigs(self):
"""Verify global configs stack properly."""
self._write_global_config("""[Builtin Hooks]
commit_msg_bug_field = true
commit_msg_changeid_field = true
commit_msg_test_field = false""")
- self._write_config("""[Builtin Hooks]
+ self._write_local_config("""[Builtin Hooks]
commit_msg_bug_field = false
commit_msg_test_field = true""")
config = rh.config.PreUploadSettings(paths=(self.tempdir,),