diff options
author | ThiƩbaud Weksteen <tweek@google.com> | 2020-09-01 14:54:03 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-09-01 14:54:03 +0000 |
commit | 615f08ee34ad7db309ed6a19124010a78c86cc08 (patch) | |
tree | 3b024ccdaf14d6d09a6107ae52fa825e7057205c | |
parent | bbd6c03fa05b6950fcbb87635b9f21ad3e2c0946 (diff) | |
parent | 324296b1108e62440c43a45320b69c329ee0df31 (diff) | |
download | repohooks-615f08ee34ad7db309ed6a19124010a78c86cc08.tar.gz |
config: refactor PreUploadSettingsTests am: 324296b110
Original change: https://android-review.googlesource.com/c/platform/tools/repohooks/+/1413981
Change-Id: I929668c32fb13fa260cd12562f00b026fdff3d52
-rwxr-xr-x | rh/config_unittest.py | 51 |
1 files changed, 25 insertions, 26 deletions
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 @@ class PreUploadConfigTests(unittest.TestCase): 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 @@ class PreUploadFileTests(unittest.TestCase): 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 @@ name = script --'bad-quotes 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,), |