-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metadata reporting for Git sources. (#132)
- Loading branch information
Showing
7 changed files
with
234 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[run] | ||
branch = True | ||
|
||
[report] | ||
exclude_lines = | ||
pragma: no cover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import atexit | ||
import functools | ||
import sys | ||
|
||
import google.protobuf.json_format | ||
|
||
from synthtool import log | ||
from synthtool.protos import metadata_pb2 | ||
|
||
|
||
_metadata = metadata_pb2.Metadata() | ||
|
||
|
||
def reset() -> None: | ||
"""Clear all metadata so far.""" | ||
global _metadata | ||
_metadata = metadata_pb2.Metadata() | ||
|
||
|
||
def get(): | ||
return _metadata | ||
|
||
|
||
def add_git_source(**kwargs) -> None: | ||
"""Adds a git source to the current metadata.""" | ||
_metadata.sources.add(git=metadata_pb2.GitSource(**kwargs)) | ||
|
||
|
||
def write(outfile: str = "synth.metadata") -> None: | ||
"""Writes out the metadata to a file.""" | ||
jsonified = google.protobuf.json_format.MessageToJson(_metadata) | ||
|
||
with open(outfile, "w") as fh: | ||
fh.write(jsonified) | ||
|
||
log.debug(f"Wrote metadata to {outfile}.") | ||
|
||
|
||
def register_exit_hook(**kwargs) -> None: | ||
atexit.register(functools.partial(write, **kwargs)) | ||
|
||
|
||
# Only register this hook if pytest is not active. | ||
if "pytest" not in sys.modules: # pragma: no cover | ||
register_exit_hook() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from synthtool.sources import git | ||
|
||
|
||
def test_make_repo_clone_url(monkeypatch): | ||
monkeypatch.setattr(git, "USE_SSH", True) | ||
assert ( | ||
git.make_repo_clone_url("theacodes/nox") == "git@github.com:theacodes/nox.git" | ||
) | ||
|
||
|
||
def test_make_repo_clone_url_https(monkeypatch): | ||
monkeypatch.setattr(git, "USE_SSH", False) | ||
assert ( | ||
git.make_repo_clone_url("theacodes/nox") | ||
== "https://github.com/theacodes/nox.git" | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("input, expected"), | ||
[ | ||
("git@github.com:theacodes/nox.git", {"owner": "theacodes", "name": "nox"}), | ||
("https://github.com/theacodes/nox.git", {"owner": "theacodes", "name": "nox"}), | ||
("theacodes/nox", {"owner": "theacodes", "name": "nox"}), | ||
("theacodes/nox.git", {"owner": "theacodes", "name": "nox"}), | ||
], | ||
) | ||
def test_parse_repo_url(input, expected): | ||
assert git.parse_repo_url(input) == expected | ||
|
||
|
||
@mock.patch("subprocess.check_output", autospec=True) | ||
def test_get_latest_commit(check_call): | ||
check_call.return_value = b"abc123\ncommit\nmessage." | ||
|
||
sha, message = git.get_latest_commit() | ||
|
||
assert sha == "abc123" | ||
assert message == "commit\nmessage." | ||
|
||
|
||
def test_extract_commit_message_metadata(): | ||
message = """\ | ||
Hello, world! | ||
One: Hello! | ||
Two: 1234 | ||
""" | ||
metadata = git.extract_commit_message_metadata(message) | ||
|
||
assert metadata == {"One": "Hello!", "Two": "1234"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
|
||
from synthtool import metadata | ||
|
||
|
||
def test_add_git_source(): | ||
metadata.reset() | ||
|
||
metadata.add_git_source(sha="sha", name="name", remote="remote") | ||
|
||
current = metadata.get() | ||
|
||
assert current.sources[0].git.sha == "sha" | ||
assert current.sources[0].git.name == "name" | ||
assert current.sources[0].git.remote == "remote" | ||
|
||
|
||
def test_write(tmpdir): | ||
metadata.reset() | ||
|
||
metadata.add_git_source(sha="sha", name="name", remote="remote") | ||
|
||
output_file = tmpdir / "synth.metadata" | ||
|
||
metadata.write(str(output_file)) | ||
|
||
data = output_file.read() | ||
|
||
# Ensure the file was written, that *some* metadata is in it, and that it | ||
# is valid JSON. | ||
assert data | ||
assert "sha" in data | ||
assert json.loads(data) |