git_find_releases_test.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env vpython3
  2. # coding=utf-8
  3. # Copyright 2020 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Unit tests for git_find_releases.py."""
  7. from __future__ import print_function
  8. from __future__ import unicode_literals
  9. import logging
  10. import os
  11. import sys
  12. import unittest
  13. if sys.version_info.major == 2:
  14. from StringIO import StringIO
  15. import mock
  16. else:
  17. from io import StringIO
  18. from unittest import mock
  19. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  20. import git_find_releases
  21. import git_common
  22. class TestGitFindReleases(unittest.TestCase):
  23. @mock.patch('sys.stdout', StringIO())
  24. @mock.patch('git_common.run', return_value='')
  25. def test_invalid_commit(self, git_run):
  26. result = git_find_releases.main(['foo'])
  27. self.assertEqual(1, result)
  28. self.assertEqual('foo not found', sys.stdout.getvalue().strip())
  29. git_run.assert_called_once_with('name-rev', '--tags', '--name-only', 'foo')
  30. @mock.patch('sys.stdout', StringIO())
  31. @mock.patch('git_common.run')
  32. def test_no_merge(self, git_run):
  33. def git_run_function(*args):
  34. assert len(args) > 1
  35. if args[0] == 'name-rev' and args[1] == '--tags':
  36. return 'undefined'
  37. if args[0] == 'name-rev' and args[1] == '--refs':
  38. return '1.0.0'
  39. if args[0] == 'log':
  40. return ''
  41. assert False, "Unexpected arguments for git.run"
  42. git_run.side_effect = git_run_function
  43. result = git_find_releases.main(['foo'])
  44. self.assertEqual(0, result)
  45. stdout = sys.stdout.getvalue().strip()
  46. self.assertIn('commit foo was', stdout)
  47. self.assertIn('No merges found', stdout)
  48. self.assertEqual(3, git_run.call_count)
  49. if __name__ == '__main__':
  50. logging.basicConfig(
  51. level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
  52. unittest.main()