diff options
-rw-r--r-- | git_trace2_event_log.py | 44 | ||||
-rw-r--r-- | tests/test_git_trace2_event_log.py | 29 |
2 files changed, 52 insertions, 21 deletions
diff --git a/git_trace2_event_log.py b/git_trace2_event_log.py index 4a8e0347..dfbded15 100644 --- a/git_trace2_event_log.py +++ b/git_trace2_event_log.py | |||
@@ -132,6 +132,30 @@ class EventLog(object): | |||
132 | exit_event['code'] = result | 132 | exit_event['code'] = result |
133 | self._log.append(exit_event) | 133 | self._log.append(exit_event) |
134 | 134 | ||
135 | def _GetEventTargetPath(self): | ||
136 | """Get the 'trace2.eventtarget' path from git configuration. | ||
137 | |||
138 | Returns: | ||
139 | path: git config's 'trace2.eventtarget' path if it exists, or None | ||
140 | """ | ||
141 | path = None | ||
142 | cmd = ['config', '--get', 'trace2.eventtarget'] | ||
143 | # TODO(https://crbug.com/gerrit/13706): Use GitConfig when it supports | ||
144 | # system git config variables. | ||
145 | p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True, | ||
146 | bare=True) | ||
147 | retval = p.Wait() | ||
148 | if retval == 0: | ||
149 | # Strip trailing carriage-return in path. | ||
150 | path = p.stdout.rstrip('\n') | ||
151 | elif retval != 1: | ||
152 | # `git config --get` is documented to produce an exit status of `1` if | ||
153 | # the requested variable is not present in the configuration. Report any | ||
154 | # other return value as an error. | ||
155 | print("repo: error: 'git config --get' call failed with return code: %r, stderr: %r" % ( | ||
156 | retval, p.stderr), file=sys.stderr) | ||
157 | return path | ||
158 | |||
135 | def Write(self, path=None): | 159 | def Write(self, path=None): |
136 | """Writes the log out to a file. | 160 | """Writes the log out to a file. |
137 | 161 | ||
@@ -150,21 +174,11 @@ class EventLog(object): | |||
150 | log_path = None | 174 | log_path = None |
151 | # If no logging path is specified, get the path from 'trace2.eventtarget'. | 175 | # If no logging path is specified, get the path from 'trace2.eventtarget'. |
152 | if path is None: | 176 | if path is None: |
153 | cmd = ['config', '--get', 'trace2.eventtarget'] | 177 | path = self._GetEventTargetPath() |
154 | # TODO(https://crbug.com/gerrit/13706): Use GitConfig when it supports | 178 | |
155 | # system git config variables. | 179 | # If no logging path is specified, exit. |
156 | p = GitCommand(None, cmd, capture_stdout=True, capture_stderr=True, | 180 | if path is None: |
157 | bare=True) | 181 | return None |
158 | retval = p.Wait() | ||
159 | if retval == 0: | ||
160 | # Strip trailing carriage-return in path. | ||
161 | path = p.stdout.rstrip('\n') | ||
162 | elif retval != 1: | ||
163 | # `git config --get` is documented to produce an exit status of `1` if | ||
164 | # the requested variable is not present in the configuration. Report any | ||
165 | # other return value as an error. | ||
166 | print("repo: error: 'git config --get' call failed with return code: %r, stderr: %r" % ( | ||
167 | retval, p.stderr), file=sys.stderr) | ||
168 | 182 | ||
169 | if isinstance(path, str): | 183 | if isinstance(path, str): |
170 | # Get absolute path. | 184 | # Get absolute path. |
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 @@ | |||
15 | """Unittests for the git_trace2_event_log.py module.""" | 15 | """Unittests for the git_trace2_event_log.py module.""" |
16 | 16 | ||
17 | import json | 17 | import json |
18 | import os | ||
18 | import tempfile | 19 | import tempfile |
19 | import unittest | 20 | import unittest |
21 | from unittest import mock | ||
20 | 22 | ||
21 | import git_trace2_event_log | 23 | import git_trace2_event_log |
22 | 24 | ||
@@ -157,12 +159,27 @@ class EventLogTestCase(unittest.TestCase): | |||
157 | self.assertIn('code', exit_event) | 159 | self.assertIn('code', exit_event) |
158 | self.assertEqual(exit_event['code'], 2) | 160 | self.assertEqual(exit_event['code'], 2) |
159 | 161 | ||
160 | # TODO(https://crbug.com/gerrit/13706): Add additional test coverage for | 162 | def test_write_with_filename(self): |
161 | # Write() where: | 163 | """Test Write() with a path to a file exits with None.""" |
162 | # - path=None (using git config call) | 164 | self.assertIsNone(self._event_log_module.Write(path='path/to/file')) |
163 | # - path=<Non-String type> (raises TypeError) | 165 | |
164 | # - path=<Non-Directory> (should return None) | 166 | def test_write_with_git_config(self): |
165 | # - tempfile.NamedTemporaryFile errors with FileExistsError (should return None) | 167 | """Test Write() uses the git config path when 'git config' call succeeds.""" |
168 | with tempfile.TemporaryDirectory(prefix='event_log_tests') as tempdir: | ||
169 | with mock.patch.object(self._event_log_module, | ||
170 | '_GetEventTargetPath', return_value=tempdir): | ||
171 | self.assertEqual(os.path.dirname(self._event_log_module.Write()), tempdir) | ||
172 | |||
173 | def test_write_no_git_config(self): | ||
174 | """Test Write() with no git config variable present exits with None.""" | ||
175 | with mock.patch.object(self._event_log_module, | ||
176 | '_GetEventTargetPath', return_value=None): | ||
177 | self.assertIsNone(self._event_log_module.Write()) | ||
178 | |||
179 | def test_write_non_string(self): | ||
180 | """Test Write() with non-string type for |path| throws TypeError.""" | ||
181 | with self.assertRaises(TypeError): | ||
182 | self._event_log_module.Write(path=1234) | ||
166 | 183 | ||
167 | 184 | ||
168 | if __name__ == '__main__': | 185 | if __name__ == '__main__': |