git_find_releases_test.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 io import StringIO
  8. import logging
  9. import os
  10. import sys
  11. import unittest
  12. from unittest import mock
  13. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  14. import gclient_utils
  15. import git_find_releases
  16. @unittest.skipIf(gclient_utils.IsEnvCog(),
  17. 'not supported in non-git environment')
  18. class TestGitFindReleases(unittest.TestCase):
  19. @mock.patch('sys.stdout', StringIO())
  20. @mock.patch('git_common.run', return_value='')
  21. def test_invalid_commit(self, git_run):
  22. result = git_find_releases.main(['foo'])
  23. self.assertEqual(1, result)
  24. self.assertEqual('foo not found', sys.stdout.getvalue().strip())
  25. git_run.assert_called_once_with('name-rev', '--tags', '--name-only',
  26. 'foo')
  27. @mock.patch('sys.stdout', StringIO())
  28. @mock.patch('git_common.run')
  29. def test_no_merge(self, git_run):
  30. def git_run_function(*args):
  31. assert len(args) > 1
  32. if args[0] == 'name-rev' and args[1] == '--tags':
  33. return 'undefined'
  34. if args[0] == 'name-rev' and args[1] == '--refs':
  35. return '1.0.0'
  36. if args[0] == 'log':
  37. return ''
  38. assert False, "Unexpected arguments for git.run"
  39. git_run.side_effect = git_run_function
  40. result = git_find_releases.main(['foo'])
  41. self.assertEqual(0, result)
  42. stdout = sys.stdout.getvalue().strip()
  43. self.assertIn('commit foo was', stdout)
  44. self.assertIn('No merges found', stdout)
  45. self.assertEqual(3, git_run.call_count)
  46. if __name__ == '__main__':
  47. logging.basicConfig(
  48. level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
  49. unittest.main()