PRESUBMIT_test.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2021 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import time
  6. import unittest
  7. import PRESUBMIT
  8. class PresubmitError:
  9. def __init__(self, message):
  10. self.message = message
  11. def __eq__(self, other):
  12. return isinstance(other, PresubmitError) and self.message == other.message
  13. def __repr__(self):
  14. return 'PresubmitError({!r})'.format(self.message)
  15. class TestCheckFreeze(unittest.TestCase):
  16. def get_input_api(self, current_time, footers=None):
  17. """Get an input API to use for tests.
  18. Args:
  19. current_time - Current time expressed as seconds since the epoch.
  20. """
  21. class FakeTime:
  22. localtime = time.localtime
  23. strftime = time.strftime
  24. def time(self):
  25. return float(current_time)
  26. class FakeChange:
  27. def GitFootersFromDescription(self):
  28. return footers or []
  29. class FakeInputApi:
  30. time = FakeTime()
  31. change = FakeChange()
  32. return FakeInputApi()
  33. def get_output_api(self):
  34. class FakeOutputApi:
  35. PresubmitError = PresubmitError
  36. return FakeOutputApi
  37. def test_before_freeze(self):
  38. input_api = self.get_input_api(1639641599) # 2021/12/15 23:59:59 -0800
  39. output_api = self.get_output_api()
  40. errors = PRESUBMIT.CheckFreeze(input_api, output_api)
  41. self.assertEqual(errors, [])
  42. def test_start_of_freeze(self):
  43. input_api = self.get_input_api(1639641600) # 2021/12/16 00:00:00 -0800
  44. output_api = self.get_output_api()
  45. errors = PRESUBMIT.CheckFreeze(input_api, output_api)
  46. self.assertEqual(len(errors), 1)
  47. self.assertTrue(
  48. errors[0].message.startswith('There is a prod freeze in effect'))
  49. def test_end_of_freeze(self):
  50. input_api = self.get_input_api(1641196799) # 2022/01/02 23:59:59 -0800
  51. output_api = self.get_output_api()
  52. errors = PRESUBMIT.CheckFreeze(input_api, output_api)
  53. self.assertEqual(len(errors), 1)
  54. self.assertTrue(
  55. errors[0].message.startswith('There is a prod freeze in effect'))
  56. def test_after_freeze(self):
  57. input_api = self.get_input_api(1641196800) # 2022/01/03 00:00:00 -0800')
  58. output_api = self.get_output_api()
  59. errors = PRESUBMIT.CheckFreeze(input_api, output_api)
  60. self.assertEqual(errors, [])
  61. def test_ignore_freeze(self):
  62. input_api = self.get_input_api(
  63. 1639641600, # 2021/12/16 00:00:00 -0800
  64. footers={'Ignore-Freeze': 'testing'})
  65. output_api = self.get_output_api()
  66. errors = PRESUBMIT.CheckFreeze(input_api, output_api)
  67. self.assertEqual(errors, [])
  68. if __name__ == '__main__':
  69. unittest.main()