test_api.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright 2014 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import base64
  5. import hashlib
  6. from recipe_engine import recipe_test_api
  7. class GitilesTestApi(recipe_test_api.RecipeTestApi):
  8. def _make_gitiles_response_json(self, data):
  9. return self.m.json.output(data)
  10. def make_refs_test_data(self, *refs):
  11. return self._make_gitiles_response_json({ref: None for ref in refs})
  12. def make_log_test_data(self, s, n=3, cursor=None):
  13. result = {
  14. 'log': [
  15. self.make_commit_gitiles_dict(
  16. commit='fake %s hash %d' % (s, i),
  17. msg='fake %s msg %d' % (s, i),
  18. new_files=['%s.py' % (chr(i + ord('a')))],
  19. email='fake_%s@fake_%i.email.com' % (s, i),
  20. )
  21. for i in range(n)
  22. ],
  23. }
  24. if cursor:
  25. result['next'] = cursor
  26. return self._make_gitiles_response_json(result)
  27. def make_commit_test_data(self, commit, msg, new_files=None, email=None):
  28. """Constructs fake Gitiles commit JSON test output.
  29. This data structure conforms to the JSON response that Gitiles provides when
  30. a commit is queried. For example:
  31. https://chromium.googlesource.com/chromium/src/+/875b896a3256c5b86c8725e81489e99ea6c2b4c9?format=json
  32. Args:
  33. commit (str): The fake commit hash.
  34. msg (str): The commit message.
  35. new_files (list): If not None, a list of filenames (str) to simulate being
  36. added in this commit.
  37. email: if not None, a proper email with '@' in it to be used for
  38. committer's and author's emails.
  39. Returns: (raw_io.Output) A simulated Gitiles fetch 'json' output.
  40. """
  41. commit = self.make_commit_gitiles_dict(commit, msg, new_files, email)
  42. return self._make_gitiles_response_json(commit)
  43. def make_commit_gitiles_dict(self, commit, msg, new_files, email=None):
  44. if email is None:
  45. name = 'Test Author'
  46. email = 'testauthor@fake.chromium.org'
  47. else:
  48. assert '@' in email
  49. name = email.split('@')[0]
  50. d = {
  51. 'commit': self.make_hash(commit),
  52. 'tree': self.make_hash('tree', commit),
  53. 'parents': [self.make_hash('parent', commit)],
  54. 'author': {
  55. 'name': name,
  56. 'email': email,
  57. 'time': 'Mon Jan 01 00:00:00 2015',
  58. },
  59. 'committer': {
  60. 'name': name,
  61. 'email': email,
  62. 'time': 'Mon Jan 01 00:00:00 2015',
  63. },
  64. 'message': msg,
  65. 'tree_diff': [],
  66. }
  67. if new_files:
  68. d['tree_diff'].extend({
  69. 'type': 'add',
  70. 'old_id': 40 * '0',
  71. 'old_mode': 0,
  72. 'new_id': self.make_hash('file', f, commit),
  73. 'new_mode': 33188,
  74. 'new_path': f,
  75. } for f in new_files)
  76. return d
  77. def make_hash(self, *bases):
  78. return hashlib.sha1(':'.join(bases)).hexdigest()
  79. def make_encoded_file(self, data):
  80. return self.m.json.output({
  81. 'value': base64.b64encode(data),
  82. })