You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  2. #
  3. # Use of this source code is governed by a BSD-style license
  4. # that can be found in the LICENSE file in the root of the source
  5. # tree. An additional intellectual property rights grant can be found
  6. # in the file PATENTS. All contributing project authors may
  7. # be found in the AUTHORS file in the root of the source tree.
  8. # This file is inspired to [1].
  9. # [1] - https://cs.chromium.org/chromium/src/PRESUBMIT_test_mocks.py
  10. import os.path
  11. import re
  12. class MockInputApi(object):
  13. """Mock class for the InputApi class.
  14. This class can be used for unittests for presubmit by initializing the files
  15. attribute as the list of changed files.
  16. """
  17. def __init__(self):
  18. self.change = MockChange([], [])
  19. self.files = []
  20. self.presubmit_local_path = os.path.dirname(__file__)
  21. def AffectedSourceFiles(self, file_filter=None):
  22. return self.AffectedFiles(file_filter=file_filter)
  23. def AffectedFiles(self, file_filter=None, include_deletes=False):
  24. # pylint: disable=unused-argument
  25. return self.files
  26. @classmethod
  27. def FilterSourceFile(cls, affected_file, white_list=(), black_list=()):
  28. # pylint: disable=unused-argument
  29. return True
  30. def PresubmitLocalPath(self):
  31. return self.presubmit_local_path
  32. def ReadFile(self, affected_file, mode='rU'):
  33. filename = affected_file.AbsoluteLocalPath()
  34. for f in self.files:
  35. if f.LocalPath() == filename:
  36. with open(filename, mode) as f:
  37. return f.read()
  38. # Otherwise, file is not in our mock API.
  39. raise IOError, "No such file or directory: '%s'" % filename
  40. class MockOutputApi(object):
  41. """Mock class for the OutputApi class.
  42. An instance of this class can be passed to presubmit unittests for outputing
  43. various types of results.
  44. """
  45. class PresubmitResult(object):
  46. def __init__(self, message, items=None, long_text=''):
  47. self.message = message
  48. self.items = items
  49. self.long_text = long_text
  50. def __repr__(self):
  51. return self.message
  52. class PresubmitError(PresubmitResult):
  53. def __init__(self, message, items=None, long_text=''):
  54. MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
  55. self.type = 'error'
  56. class MockChange(object):
  57. """Mock class for Change class.
  58. This class can be used in presubmit unittests to mock the query of the
  59. current change.
  60. """
  61. def __init__(self, changed_files, bugs_from_description, tags=None):
  62. self._changed_files = changed_files
  63. self._bugs_from_description = bugs_from_description
  64. self.tags = dict() if not tags else tags
  65. def BugsFromDescription(self):
  66. return self._bugs_from_description
  67. def __getattr__(self, attr):
  68. """Return tags directly as attributes on the object."""
  69. if not re.match(r"^[A-Z_]*$", attr):
  70. raise AttributeError(self, attr)
  71. return self.tags.get(attr)
  72. class MockFile(object):
  73. """Mock class for the File class.
  74. This class can be used to form the mock list of changed files in
  75. MockInputApi for presubmit unittests.
  76. """
  77. def __init__(self, local_path, new_contents=None, old_contents=None,
  78. action='A'):
  79. if new_contents is None:
  80. new_contents = ["Data"]
  81. self._local_path = local_path
  82. self._new_contents = new_contents
  83. self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
  84. self._action = action
  85. self._old_contents = old_contents
  86. def Action(self):
  87. return self._action
  88. def ChangedContents(self):
  89. return self._changed_contents
  90. def NewContents(self):
  91. return self._new_contents
  92. def LocalPath(self):
  93. return self._local_path
  94. def AbsoluteLocalPath(self):
  95. return self._local_path
  96. def OldContents(self):
  97. return self._old_contents