blob: 7491656e36d732bfc0fdae62a02729c078179fb1 [file] [log] [blame]
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import base64
import hashlib
import re
from recipe_engine import recipe_test_api
class GitilesTestApi(recipe_test_api.RecipeTestApi):
def hash(self, *bases):
return hashlib.sha1(":".join(bases).encode()).hexdigest()
def log(
self,
step_name,
s,
n=3,
add_change_id=False,
extra_footers=None,
committer_email=None,
):
commits = []
for i in range(n):
commit = f"fake {s} hash {int(i)}"
name = f"Fake {s}"
email = committer_email or f"fake_{s}@fake_{int(i)}.email.com"
message_lines = [f"fake {s} msg {int(i)}"]
footers = {}
if extra_footers:
footers.update(extra_footers)
if add_change_id:
# Using commit from above as Change-Id, but removing whitespace
# and possibly future punctuation.
alphanumeric_only_commit = re.sub(r"\W+", "", commit)
footers["Change-Id"] = f"I{alphanumeric_only_commit}"
for key, value in sorted(footers.items()):
message_lines.append(f"{key}: {value}")
commits.append(
{
"id": self.hash(commit),
"tree": self.hash("tree", commit),
"parents": [self.hash("parent", commit)],
"author": {
"name": name,
"email": email,
"time": "Mon Jan 01 00:00:00 2015",
},
"committer": {
"name": name,
"email": email,
"time": "Mon Jan 01 00:00:00 2015",
},
"message": "\n".join(message_lines),
"tree_diff": [
{
"type": "add",
"old_id": 40 * "0",
"old_mode": 0,
"new_id": self.hash("file", f, commit),
"new_mode": 33188,
"new_path": f,
}
for f in [f"{chr(i + ord('a'))}.py"]
],
}
)
return self.step_data(step_name, self.m.json.output(commits))
def fetch(self, step_name, data):
return self.m.url.text(step_name, base64.b64encode(data.encode()))