charujain | 9893e25 | 2017-09-14 11:33:22 | [diff] [blame] | 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 | |
Mirko Bonadei | a730c1c | 2017-09-18 09:33:13 | [diff] [blame] | 9 | # This file is inspired to [1]. |
| 10 | # [1] - https://cs.chromium.org/chromium/src/PRESUBMIT_test_mocks.py |
| 11 | |
charujain | 9893e25 | 2017-09-14 11:33:22 | [diff] [blame] | 12 | |
| 13 | class MockInputApi(object): |
| 14 | """Mock class for the InputApi class. |
| 15 | |
| 16 | This class can be used for unittests for presubmit by initializing the files |
| 17 | attribute as the list of changed files. |
| 18 | """ |
| 19 | |
| 20 | def __init__(self): |
Mirko Bonadei | 6188018 | 2017-10-12 13:12:35 | [diff] [blame] | 21 | self.change = MockChange([], []) |
Mirko Bonadei | a730c1c | 2017-09-18 09:33:13 | [diff] [blame] | 22 | self.files = [] |
| 23 | |
| 24 | def AffectedSourceFiles(self, file_filter=None): |
| 25 | # pylint: disable=unused-argument |
| 26 | return self.files |
charujain | 9893e25 | 2017-09-14 11:33:22 | [diff] [blame] | 27 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 11:49:16 | [diff] [blame] | 28 | def ReadFile(self, affected_file, mode='rU'): |
| 29 | filename = affected_file.AbsoluteLocalPath() |
| 30 | for f in self.files: |
| 31 | if f.LocalPath() == filename: |
| 32 | with open(filename, mode) as f: |
| 33 | return f.read() |
| 34 | # Otherwise, file is not in our mock API. |
| 35 | raise IOError, "No such file or directory: '%s'" % filename |
| 36 | |
charujain | 9893e25 | 2017-09-14 11:33:22 | [diff] [blame] | 37 | |
| 38 | class MockOutputApi(object): |
| 39 | """Mock class for the OutputApi class. |
| 40 | |
| 41 | An instance of this class can be passed to presubmit unittests for outputing |
| 42 | various types of results. |
| 43 | """ |
| 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 | |
| 51 | def __repr__(self): |
| 52 | return self.message |
| 53 | |
| 54 | class PresubmitError(PresubmitResult): |
| 55 | def __init__(self, message, items=None, long_text=''): |
| 56 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 57 | self.type = 'error' |
| 58 | |
Mirko Bonadei | a730c1c | 2017-09-18 09:33:13 | [diff] [blame] | 59 | |
charujain | 9893e25 | 2017-09-14 11:33:22 | [diff] [blame] | 60 | class MockChange(object): |
| 61 | """Mock class for Change class. |
| 62 | |
| 63 | This class can be used in presubmit unittests to mock the query of the |
| 64 | current change. |
| 65 | """ |
| 66 | |
Mirko Bonadei | 6188018 | 2017-10-12 13:12:35 | [diff] [blame] | 67 | def __init__(self, changed_files, bugs_from_description): |
charujain | 9893e25 | 2017-09-14 11:33:22 | [diff] [blame] | 68 | self._changed_files = changed_files |
Mirko Bonadei | 6188018 | 2017-10-12 13:12:35 | [diff] [blame] | 69 | self._bugs_from_description = bugs_from_description |
| 70 | |
| 71 | def BugsFromDescription(self): |
| 72 | return self._bugs_from_description |
Mirko Bonadei | a730c1c | 2017-09-18 09:33:13 | [diff] [blame] | 73 | |
| 74 | |
| 75 | class MockFile(object): |
| 76 | """Mock class for the File class. |
| 77 | |
| 78 | This class can be used to form the mock list of changed files in |
| 79 | MockInputApi for presubmit unittests. |
| 80 | """ |
| 81 | |
| 82 | def __init__(self, local_path): |
| 83 | self._local_path = local_path |
| 84 | |
| 85 | def LocalPath(self): |
| 86 | return self._local_path |
Mirko Bonadei | 4dc4e25 | 2017-09-19 11:49:16 | [diff] [blame] | 87 | |
| 88 | def AbsoluteLocalPath(self): |
| 89 | return self._local_path |