-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'zeid/bug-1944562-merge-lando-api' into zeid/bug-1944562…
…-lando-api-fixes
- Loading branch information
Showing
4 changed files
with
167 additions
and
29 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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import subprocess | ||
from collections.abc import Callable | ||
from pathlib import Path | ||
from typing import Optional | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
@@ -48,8 +49,13 @@ def test_GitSCM_repo_is_supported(repo_path: str, expected: bool, git_repo: Path | |
), f"{scm} did not correctly report support for {repo_path}" | ||
|
||
|
||
def test_GitSCM_clone(git_repo: Path, tmp_path: Path, monkeypatch): | ||
clone_path = tmp_path / "repo_test_GitSCM_clone" | ||
def test_GitSCM_clone( | ||
git_repo: Path, | ||
monkeypatch: pytest.MonkeyPatch, | ||
request: pytest.FixtureRequest, | ||
tmp_path: Path, | ||
): | ||
clone_path = tmp_path / request.node.name | ||
scm = GitSCM(str(clone_path)) | ||
|
||
mock_git_run = _monkeypatch_scm(monkeypatch, scm, "_git_run") | ||
|
@@ -65,25 +71,23 @@ def test_GitSCM_clone(git_repo: Path, tmp_path: Path, monkeypatch): | |
|
||
@pytest.mark.parametrize( | ||
"strip_non_public_commits", | ||
(True, False), | ||
[True, False], | ||
) | ||
def test_GitSCM_clean_repo( | ||
git_repo: Path, | ||
tmp_path: Path, | ||
git_setup_user: Callable, | ||
monkeypatch: pytest.MonkeyPatch, | ||
request: pytest.FixtureRequest, | ||
strip_non_public_commits: bool, | ||
monkeypatch, | ||
tmp_path: Path, | ||
): | ||
clone_path = tmp_path / "repo_test_GitSCM_clean_repo" | ||
clone_path = tmp_path / request.node.name | ||
clone_path.mkdir() | ||
scm = GitSCM(str(clone_path)) | ||
scm.clone(str(git_repo)) | ||
|
||
git_setup_user(str(clone_path)) | ||
|
||
new_file = clone_path / "new_file" | ||
new_file.write_text("test", encoding="utf-8") | ||
|
||
original_commit = subprocess.run( | ||
["git", "rev-parse", "HEAD"], cwd=str(clone_path), capture_output=True | ||
).stdout | ||
|
@@ -101,19 +105,7 @@ def test_GitSCM_clean_repo( | |
check=True, | ||
) | ||
# Those two command should not raise exceptions | ||
subprocess.run(["git", "add", new_file.name], cwd=str(clone_path), check=True) | ||
subprocess.run( | ||
[ | ||
"git", | ||
"commit", | ||
"-m", | ||
"adding new_file", | ||
"--author", | ||
"Lando <[email protected]>", | ||
], | ||
cwd=str(clone_path), | ||
check=True, | ||
) | ||
new_file = _create_git_commit(request, clone_path) | ||
|
||
new_untracked_file = clone_path / "new_untracked_file" | ||
new_untracked_file.write_text("test", encoding="utf-8") | ||
|
@@ -139,6 +131,101 @@ def test_GitSCM_clean_repo( | |
), f"strip_non_public_commits not honoured for {new_file}" | ||
|
||
|
||
@pytest.mark.parametrize("target_cs", [None, "main", "dev", "git-ref"]) | ||
def test_GitSCM_update_repo( | ||
git_repo: Path, | ||
git_setup_user: Callable, | ||
request: pytest.FixtureRequest, | ||
target_cs: str, | ||
tmp_path: Path, | ||
): | ||
clone_path = tmp_path / request.node.name | ||
clone_path.mkdir() | ||
scm = GitSCM(str(clone_path)) | ||
scm.clone(str(git_repo)) | ||
|
||
git_setup_user(str(clone_path)) | ||
|
||
original_commit = subprocess.run( | ||
["git", "rev-parse", "HEAD"], | ||
cwd=str(clone_path), | ||
capture_output=True, | ||
).stdout | ||
|
||
if target_cs == "git-ref": | ||
# Special case for a naked git reference | ||
target_cs = original_commit.decode("utf-8").strip() | ||
elif target_cs: | ||
original_commit = subprocess.run( | ||
["git", "rev-parse", f"origin/{target_cs}"], | ||
cwd=str(clone_path), | ||
capture_output=True, | ||
).stdout | ||
|
||
# Create an empty commit that we expect to see rewound, too | ||
subprocess.run( | ||
[ | ||
"git", | ||
"commit", | ||
"--fixup", | ||
"reword:HEAD", | ||
"--no-edit", | ||
], | ||
cwd=str(clone_path), | ||
check=True, | ||
) | ||
_create_git_commit(request, clone_path) | ||
|
||
scm.update_repo(str(git_repo), target_cs) | ||
|
||
current_commit = subprocess.run( | ||
["git", "rev-parse", "--branch", "HEAD"], | ||
cwd=str(clone_path), | ||
capture_output=True, | ||
).stdout | ||
current_commit = subprocess.run( | ||
["git", "rev-parse", "HEAD"], cwd=str(clone_path), capture_output=True | ||
).stdout | ||
assert ( | ||
current_commit == original_commit | ||
), f"Not on original_commit {original_commit} updating repo: {current_commit}" | ||
|
||
|
||
@pytest.mark.parametrize("push_target", [None, "main", "dev"]) | ||
def test_GitSCM_push( | ||
git_repo: Path, | ||
git_setup_user: Callable, | ||
monkeypatch: pytest.MonkeyPatch, | ||
push_target: Optional[str], | ||
request: pytest.FixtureRequest, | ||
tmp_path: Path, | ||
): | ||
clone_path = tmp_path / request.node.name | ||
clone_path.mkdir() | ||
|
||
default_branch = "dev" | ||
scm = GitSCM(str(clone_path), default_branch=default_branch) | ||
scm.clone(str(git_repo)) | ||
|
||
git_setup_user(str(clone_path)) | ||
|
||
_create_git_commit(request, clone_path) | ||
|
||
new_untracked_file = clone_path / "new_untracked_file" | ||
new_untracked_file.write_text("test", encoding="utf-8") | ||
|
||
mock_git_run = _monkeypatch_scm(monkeypatch, scm, "_git_run") | ||
|
||
# breakpoint() | ||
scm.push(str(git_repo), push_target) | ||
|
||
if not push_target: | ||
push_target = default_branch | ||
mock_git_run.assert_called_with( | ||
"push", str(git_repo), f"HEAD:{push_target}", cwd=str(clone_path) | ||
) | ||
|
||
|
||
def test_GitSCM_push_get_github_token(git_repo: Path): | ||
scm = GitSCM(str(git_repo)) | ||
scm._git_run = MagicMock() | ||
|
@@ -168,7 +255,28 @@ def test_GitSCM_git_run_redact_url_userinfo(git_repo: Path): | |
assert userinfo not in exc.value.err | ||
assert userinfo not in str(exc.value) | ||
assert userinfo not in repr(exc.value) | ||
assert "[REDACTED]" in exc.value.err | ||
assert "[REDACTED]" in str(exc.value) | ||
|
||
|
||
def _create_git_commit(request: pytest.FixtureRequest, clone_path: Path): | ||
new_file = clone_path / "new_file" | ||
new_file.write_text(request.node.name, encoding="utf-8") | ||
|
||
subprocess.run(["git", "add", new_file.name], cwd=str(clone_path), check=True) | ||
subprocess.run( | ||
[ | ||
"git", | ||
"commit", | ||
"-m", | ||
"adding new_file", | ||
"--author", | ||
f"{request.node.name} <pytest@lando>", | ||
], | ||
cwd=str(clone_path), | ||
check=True, | ||
) | ||
|
||
return new_file | ||
|
||
|
||
def _monkeypatch_scm(monkeypatch, scm: GitSCM, method: str) -> MagicMock: | ||
|