-
Notifications
You must be signed in to change notification settings - Fork 26
Implement exercise T4L2/tags-push #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
575c071
Add tags push
VikramGoyal23 4143aff
Add download.py
VikramGoyal23 639fe62
Add verify.py
VikramGoyal23 437319a
Add unit tests and clean verify.py
VikramGoyal23 951d019
Implement feedback
VikramGoyal23 0098d22
Fix formatting
VikramGoyal23 5041b1d
Fix whitespace
VikramGoyal23 d9bf436
Refactor download.py to use tag_with_options
VikramGoyal23 9d361ed
[tags-push] Run ruff format
jovnc 36cf657
[tags-push] Add null check in get_remote_tags
jovnc 289c080
[tags-push] Update README
jovnc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,16 @@ | ||
| { | ||
| "exercise_name": "tags-push", | ||
| "tags": [ | ||
| "git-tag" | ||
| ], | ||
| "requires_git": true, | ||
| "requires_github": true, | ||
| "base_files": {}, | ||
| "exercise_repo": { | ||
| "repo_type": "remote", | ||
| "repo_name": "duty-roster", | ||
| "repo_title": "gm-duty-roster", | ||
| "create_fork": true, | ||
| "init": null | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| See https://git-mastery.github.io/lessons/tag/exercise-tags-push.html |
Empty file.
This file contains hidden or 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,12 @@ | ||
| from exercise_utils.cli import run_command | ||
| from exercise_utils.git import tag, tag_with_options | ||
|
|
||
|
|
||
| def setup(verbose: bool = False): | ||
jovnc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| run_command(["git", "remote", "rename", "origin", "production"], verbose) | ||
| tag("beta", verbose) | ||
| run_command(["git", "push", "production", "--tags"], verbose) | ||
| run_command(["git", "tag", "-d", "beta"], verbose) | ||
|
|
||
| tag_with_options("v1.0", ["HEAD~4"], verbose) | ||
| tag_with_options("v2.0", ["-a", "HEAD~1", "-m", "First stable roster"], verbose) | ||
Empty file.
This file contains hidden or 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 @@ | ||
| initialization: | ||
| steps: | ||
| - type: commit | ||
| empty: true | ||
| message: Empty commit | ||
| id: start |
jovnc marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,135 @@ | ||
| import json | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from git.repo import Repo | ||
|
|
||
| from git_autograder import ( | ||
| GitAutograderExercise, | ||
| GitAutograderStatus, | ||
| GitAutograderTestLoader, | ||
| GitAutograderWrongAnswerException, | ||
| assert_output, | ||
| ) | ||
|
|
||
| from ..verify import ( | ||
| IMPROPER_GH_CLI_SETUP, | ||
| TAG_1_NAME, | ||
| TAG_2_NAME, | ||
| TAG_DELETE_NAME, | ||
| TAG_1_MISSING, | ||
| TAG_2_MISSING, | ||
| TAG_DELETE_NOT_REMOVED, | ||
| verify, | ||
| ) | ||
|
|
||
| REPOSITORY_NAME = "tags-push" | ||
|
|
||
| loader = GitAutograderTestLoader(__file__, REPOSITORY_NAME, verify) | ||
|
|
||
|
|
||
| # NOTE: This exercise is a special case where we do not require repo-smith. Instead, | ||
| # we directly mock function calls to verify that all branches are covered for us. | ||
|
|
||
|
|
||
| # TODO: The current tooling isn't mature enough to handle mock GitAutograderExercise in | ||
| # cases like these. We would ideally need some abstraction rather than creating our own. | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def exercise(tmp_path: Path) -> GitAutograderExercise: | ||
| repo_dir = tmp_path / "ignore-me" | ||
| repo_dir.mkdir() | ||
|
|
||
| Repo.init(repo_dir) | ||
| with open(tmp_path / ".gitmastery-exercise.json", "a") as config_file: | ||
| config_file.write( | ||
| json.dumps( | ||
| { | ||
| "exercise_name": "tags-push", | ||
| "tags": [], | ||
| "requires_git": True, | ||
| "requires_github": True, | ||
| "base_files": {}, | ||
| "exercise_repo": { | ||
| "repo_type": "local", | ||
| "repo_name": "ignore-me", | ||
| "init": True, | ||
| "create_fork": None, | ||
| "repo_title": None, | ||
| }, | ||
| "downloaded_at": None, | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| exercise = GitAutograderExercise(exercise_path=tmp_path) | ||
| return exercise | ||
|
|
||
|
|
||
| def test_pass(exercise: GitAutograderExercise): | ||
| with ( | ||
| patch("tags_push.verify.get_username", return_value="dummy"), | ||
| patch( | ||
| "tags_push.verify.get_remote_tags", return_value=[TAG_1_NAME, TAG_2_NAME] | ||
| ), | ||
| ): | ||
| output = verify(exercise) | ||
| assert_output(output, GitAutograderStatus.SUCCESSFUL) | ||
|
|
||
jovnc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def test_improper_gh_setup(exercise: GitAutograderExercise): | ||
| with ( | ||
| patch("tags_push.verify.get_username", return_value=None), | ||
| patch( | ||
| "tags_push.verify.get_remote_tags", return_value=[TAG_1_NAME, TAG_2_NAME] | ||
| ), | ||
| pytest.raises(GitAutograderWrongAnswerException, match=IMPROPER_GH_CLI_SETUP), | ||
| ): | ||
| verify(exercise) | ||
|
|
||
|
|
||
| def test_beta_present(exercise: GitAutograderExercise): | ||
| with ( | ||
| patch("tags_push.verify.get_username", return_value="dummy"), | ||
| patch( | ||
| "tags_push.verify.get_remote_tags", | ||
| return_value=[TAG_1_NAME, TAG_2_NAME, TAG_DELETE_NAME], | ||
| ), | ||
| pytest.raises(GitAutograderWrongAnswerException, match=TAG_DELETE_NOT_REMOVED), | ||
| ): | ||
| verify(exercise) | ||
|
|
||
|
|
||
| def test_tag_1_absent(exercise: GitAutograderExercise): | ||
| with ( | ||
| patch("tags_push.verify.get_username", return_value="dummy"), | ||
| patch("tags_push.verify.get_remote_tags", return_value=[TAG_2_NAME]), | ||
| pytest.raises(GitAutograderWrongAnswerException, match=TAG_1_MISSING), | ||
| ): | ||
| verify(exercise) | ||
|
|
||
|
|
||
| def test_tag_2_absent(exercise: GitAutograderExercise): | ||
| with ( | ||
| patch("tags_push.verify.get_username", return_value="dummy"), | ||
| patch("tags_push.verify.get_remote_tags", return_value=[TAG_1_NAME]), | ||
| pytest.raises(GitAutograderWrongAnswerException, match=TAG_2_MISSING), | ||
| ): | ||
| verify(exercise) | ||
|
|
||
|
|
||
| def test_all_wrong(exercise: GitAutograderExercise): | ||
| with ( | ||
| patch("tags_push.verify.get_username", return_value="dummy"), | ||
| patch("tags_push.verify.get_remote_tags", return_value=[TAG_DELETE_NAME]), | ||
| pytest.raises(GitAutograderWrongAnswerException) as exception, | ||
| ): | ||
| verify(exercise) | ||
jovnc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| assert exception.value.message == [ | ||
| TAG_1_MISSING, | ||
| TAG_2_MISSING, | ||
| TAG_DELETE_NOT_REMOVED, | ||
| ] | ||
This file contains hidden or 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,73 @@ | ||
| import os | ||
| import subprocess | ||
| from typing import List, Optional | ||
|
|
||
| from git_autograder import ( | ||
| GitAutograderOutput, | ||
| GitAutograderExercise, | ||
| GitAutograderStatus, | ||
| ) | ||
|
|
||
| IMPROPER_GH_CLI_SETUP = "Your Github CLI is not setup correctly" | ||
|
|
||
| TAG_1_NAME = "v1.0" | ||
| TAG_2_NAME = "v2.0" | ||
| TAG_DELETE_NAME = "beta" | ||
|
|
||
| TAG_1_MISSING = f"Tag {TAG_1_NAME} is missing, did you push it to the remote?" | ||
| TAG_2_MISSING = f"Tag {TAG_2_NAME} is missing, did you push it to the remote?" | ||
| TAG_DELETE_NOT_REMOVED = f"Tag {TAG_DELETE_NAME} is still on the remote!" | ||
|
|
||
|
|
||
| def run_command(command: List[str]) -> Optional[str]: | ||
jovnc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try: | ||
| result = subprocess.run( | ||
| command, | ||
| capture_output=True, | ||
| text=True, | ||
| check=True, | ||
| env=dict(os.environ, **{"GH_PAGER": "cat"}), | ||
| ) | ||
| return result.stdout.strip() | ||
| except subprocess.CalledProcessError: | ||
| return None | ||
|
|
||
|
|
||
| def get_username() -> Optional[str]: | ||
jovnc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return run_command(["gh", "api", "user", "-q", ".login"]) | ||
|
|
||
|
|
||
| def get_remote_tags(username: str) -> List[str]: | ||
| raw_tags = run_command(["git", "ls-remote", "--tags"]) | ||
| if raw_tags is None: | ||
| return [] | ||
| return [line.split("/")[2] for line in raw_tags.strip().splitlines()] | ||
|
|
||
|
|
||
| def verify(exercise: GitAutograderExercise) -> GitAutograderOutput: | ||
| username = get_username() | ||
| if username is None: | ||
| raise exercise.wrong_answer([IMPROPER_GH_CLI_SETUP]) | ||
jovnc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| tag_names = get_remote_tags(username) | ||
|
|
||
| comments = [] | ||
|
|
||
| if TAG_1_NAME not in tag_names: | ||
| comments.append(TAG_1_MISSING) | ||
|
|
||
| if TAG_2_NAME not in tag_names: | ||
| comments.append(TAG_2_MISSING) | ||
|
|
||
| if TAG_DELETE_NAME in tag_names: | ||
| comments.append(TAG_DELETE_NOT_REMOVED) | ||
|
|
||
| if comments: | ||
| raise exercise.wrong_answer(comments) | ||
|
|
||
| return exercise.to_output( | ||
| [ | ||
| "Wonderful! You have successfully synced the local tags with the remote tags!" | ||
| ], | ||
| GitAutograderStatus.SUCCESSFUL, | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.