From 343d585ff99c5748d33823e92507fa06a037c608 Mon Sep 17 00:00:00 2001 From: Ian Kasprzak Date: Wed, 6 Jan 2021 16:26:31 -0800 Subject: Fix bug in git trace2 event Write() function when no config present. See https://bugs.chromium.org/p/gerrit/issues/detail?id=13706#c9 Added additional unit tests for Write() for additional test coverage. Testing: - Unit tests - Verified repo works with: - Valid trace2.eventtarget - Invalid trace2.eventtarget Bug: https://crbug.com/gerrit/13706 Tested-by: Ian Kasprzak Change-Id: I6b027cb2399bd03e453a132ad82e022a1f48476e Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/292762 Reviewed-by: Mike Frysinger --- tests/test_git_trace2_event_log.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'tests/test_git_trace2_event_log.py') diff --git a/tests/test_git_trace2_event_log.py b/tests/test_git_trace2_event_log.py index 3905630f..686802ea 100644 --- a/tests/test_git_trace2_event_log.py +++ b/tests/test_git_trace2_event_log.py @@ -15,8 +15,10 @@ """Unittests for the git_trace2_event_log.py module.""" import json +import os import tempfile import unittest +from unittest import mock import git_trace2_event_log @@ -157,12 +159,27 @@ class EventLogTestCase(unittest.TestCase): self.assertIn('code', exit_event) self.assertEqual(exit_event['code'], 2) - # TODO(https://crbug.com/gerrit/13706): Add additional test coverage for - # Write() where: - # - path=None (using git config call) - # - path= (raises TypeError) - # - path= (should return None) - # - tempfile.NamedTemporaryFile errors with FileExistsError (should return None) + def test_write_with_filename(self): + """Test Write() with a path to a file exits with None.""" + self.assertIsNone(self._event_log_module.Write(path='path/to/file')) + + def test_write_with_git_config(self): + """Test Write() uses the git config path when 'git config' call succeeds.""" + with tempfile.TemporaryDirectory(prefix='event_log_tests') as tempdir: + with mock.patch.object(self._event_log_module, + '_GetEventTargetPath', return_value=tempdir): + self.assertEqual(os.path.dirname(self._event_log_module.Write()), tempdir) + + def test_write_no_git_config(self): + """Test Write() with no git config variable present exits with None.""" + with mock.patch.object(self._event_log_module, + '_GetEventTargetPath', return_value=None): + self.assertIsNone(self._event_log_module.Write()) + + def test_write_non_string(self): + """Test Write() with non-string type for |path| throws TypeError.""" + with self.assertRaises(TypeError): + self._event_log_module.Write(path=1234) if __name__ == '__main__': -- cgit v1.2.3-54-g00ecf