Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tags_add/.gitmastery-exercise.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"exercise_name": "tags-add",
"tags": ["git-tag"],
"requires_git": true,
"requires_github": true,
"base_files": {},
"exercise_repo": {
"repo_type": "remote",
"repo_name": "duty-roster",
"create_fork": false,
"repo_title": "gm-duty-roster",
"init": false
}
}
1 change: 1 addition & 0 deletions tags_add/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See https://git-mastery.github.io/lessons/tag/exercise-tags-add.html
Empty file added tags_add/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tags_add/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def setup(verbose: bool = False): ...
Empty file added tags_add/tests/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions tags_add/tests/specs/base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
initialization:
steps:
- type: commit
empty: true
message: Add January duty roster
id: start
- type: tag
tag-name: first-pilot
- type: commit
empty: true
message: Update duty roster for February
- type: commit
empty: true
message: Update roster for March
- type: tag
tag-name: v1.0
tag-message: first full duty roster
- type: commit
empty: true
message: Update duty roster for April
- type: commit
empty: true
message: Update roster for May
21 changes: 21 additions & 0 deletions tags_add/tests/specs/missing_first_pilot_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
initialization:
steps:
- type: commit
empty: true
message: Add January duty roster
id: start
- type: commit
empty: true
message: Update duty roster for February
- type: commit
empty: true
message: Update roster for March
- type: tag
tag-name: v1.0
tag-message: first full duty roster
- type: commit
empty: true
message: Update duty roster for April
- type: commit
empty: true
message: Update roster for May
20 changes: 20 additions & 0 deletions tags_add/tests/specs/missing_v1_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
initialization:
steps:
- type: commit
empty: true
message: Add January duty roster
id: start
- type: tag
tag-name: first-pilot
- type: commit
empty: true
message: Update duty roster for February
- type: commit
empty: true
message: Update roster for March
- type: commit
empty: true
message: Update duty roster for April
- type: commit
empty: true
message: Update roster for May
23 changes: 23 additions & 0 deletions tags_add/tests/specs/wrong_commit_first_pilot_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
initialization:
steps:
- type: commit
empty: true
message: Add January duty roster
id: start
- type: commit
empty: true
message: Update duty roster for February
- type: tag
tag-name: first-pilot
- type: commit
empty: true
message: Update roster for March
- type: tag
tag-name: v1.0
tag-message: first full duty roster
- type: commit
empty: true
message: Update duty roster for April
- type: commit
empty: true
message: Update roster for May
23 changes: 23 additions & 0 deletions tags_add/tests/specs/wrong_commit_v1_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
initialization:
steps:
- type: commit
empty: true
message: Add January duty roster
id: start
- type: tag
tag-name: first-pilot
- type: commit
empty: true
message: Update duty roster for February
- type: commit
empty: true
message: Update roster for March
- type: commit
empty: true
message: Update duty roster for April
- type: commit
empty: true
message: Update roster for May
- type: tag
tag-name: v1.0
tag-message: first full duty roster
23 changes: 23 additions & 0 deletions tags_add/tests/specs/wrong_message_v1_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
initialization:
steps:
- type: commit
empty: true
message: Add January duty roster
id: start
- type: tag
tag-name: first-pilot
- type: commit
empty: true
message: Update duty roster for February
- type: commit
empty: true
message: Update roster for March
- type: tag
tag-name: v1.0
tag-message: wrong message
- type: commit
empty: true
message: Update duty roster for April
- type: commit
empty: true
message: Update roster for May
24 changes: 24 additions & 0 deletions tags_add/tests/specs/wrong_tag_type_first_pilot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
initialization:
steps:
- type: commit
empty: true
message: Add January duty roster
id: start
- type: tag
tag-name: first-pilot
tag-message: test message
- type: commit
empty: true
message: Update duty roster for February
- type: commit
empty: true
message: Update roster for March
- type: tag
tag-name: v1.0
tag-message: first full duty roster
- type: commit
empty: true
message: Update duty roster for April
- type: commit
empty: true
message: Update roster for May
22 changes: 22 additions & 0 deletions tags_add/tests/specs/wrong_tag_type_v1_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
initialization:
steps:
- type: commit
empty: true
message: Add January duty roster
id: start
- type: tag
tag-name: first-pilot
- type: commit
empty: true
message: Update duty roster for February
- type: commit
empty: true
message: Update roster for March
- type: tag
tag-name: v1.0
- type: commit
empty: true
message: Update duty roster for April
- type: commit
empty: true
message: Update roster for May
70 changes: 70 additions & 0 deletions tags_add/tests/test_verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from git_autograder import GitAutograderStatus, GitAutograderTestLoader, assert_output

from ..verify import (
FIRST_TAG_NOT_LIGHTWEIGHT,
SECOND_TAG_NOT_ANNOTATED,
verify,
FIRST_TAG_WRONG_COMMIT,
MISSING_FIRST_TAG,
MISSING_SECOND_TAG,
SECOND_TAG_WRONG_COMMIT,
WRONG_SECOND_TAG_MESSAGE,
)

REPOSITORY_NAME = "tags-add"

loader = GitAutograderTestLoader(__file__, REPOSITORY_NAME, verify)


def test_base():
with loader.load("specs/base.yml", "start") as output:
assert_output(output, GitAutograderStatus.SUCCESSFUL)


def test_missing_first_pilot_tag():
with loader.load("specs/missing_first_pilot_tag.yml", "start") as output:
assert_output(output, GitAutograderStatus.UNSUCCESSFUL, [MISSING_FIRST_TAG])


def test_missing_v1_tag():
with loader.load("specs/missing_v1_tag.yml", "start") as output:
assert_output(output, GitAutograderStatus.UNSUCCESSFUL, [MISSING_SECOND_TAG])


def test_wrong_message_v1_tag():
with loader.load("specs/wrong_message_v1_tag.yml", "start") as output:
assert_output(
output, GitAutograderStatus.UNSUCCESSFUL, [WRONG_SECOND_TAG_MESSAGE]
)


def test_wrong_commit_first_pilot_tag():
with loader.load("specs/wrong_commit_first_pilot_tag.yml", "start") as output:
assert_output(
output, GitAutograderStatus.UNSUCCESSFUL, [FIRST_TAG_WRONG_COMMIT]
)


def test_wrong_commit_v1_tag():
with loader.load("specs/wrong_commit_v1_tag.yml", "start") as output:
assert_output(
output, GitAutograderStatus.UNSUCCESSFUL, [SECOND_TAG_WRONG_COMMIT]
)


def test_wrong_tag_type_first_pilot():
with loader.load("specs/wrong_tag_type_first_pilot.yml", "start") as output:
assert_output(
output,
GitAutograderStatus.UNSUCCESSFUL,
[FIRST_TAG_NOT_LIGHTWEIGHT],
)


def test_wrong_tag_type_v1_tag():
with loader.load("specs/wrong_tag_type_v1_tag.yml", "start") as output:
assert_output(
output,
GitAutograderStatus.UNSUCCESSFUL,
[SECOND_TAG_NOT_ANNOTATED],
)
81 changes: 81 additions & 0 deletions tags_add/verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from typing import List, Optional
from git_autograder import (
GitAutograderCommit,
GitAutograderExercise,
GitAutograderOutput,
GitAutograderStatus,
)

FIRST_TAG_NOT_LIGHTWEIGHT = (
'"first-pilot" should be a lightweight tag, not an annotated tag.'
)
SECOND_TAG_NOT_ANNOTATED = '"v1.0" should be an annotated tag, not a lightweight tag.'
MISSING_FIRST_TAG = 'Missing lightweight tag "first-pilot".'
MISSING_SECOND_TAG = 'Missing annotated tag "v1.0".'
WRONG_SECOND_TAG_MESSAGE = '"v1.0" message must be exactly "first full duty roster".'
FIRST_TAG_WRONG_COMMIT = '"first-pilot" should point to the first commit.'
SECOND_TAG_WRONG_COMMIT = (
'"v1.0" should point to the commit that updates March duty roster.'
)
MISSING_FIRST_COMMIT = "Missing commit that adds January duty roster."
MISSING_MARCH_COMMIT = "Missing commit that updates March duty roster."


def get_commit_from_message(
commits: List[GitAutograderCommit], message: str
) -> Optional[GitAutograderCommit]:
"""Find a commit with the given message from a list of commits."""
for commit in commits:
if message.strip() == commit.commit.message.strip():
return commit
return None


def verify(exercise: GitAutograderExercise) -> GitAutograderOutput:
# Task 1: Verify lightweight tag "first-pilot" on the first commit
tags = exercise.repo.repo.tags
if "first-pilot" not in tags:
raise exercise.wrong_answer([MISSING_FIRST_TAG])

# Verify that "first-pilot" is a lightweight tag
first_pilot_tag = tags["first-pilot"]
if first_pilot_tag.tag is not None:
raise exercise.wrong_answer([FIRST_TAG_NOT_LIGHTWEIGHT])

main_branch = exercise.repo.branches.branch("main")
main_branch_commits = main_branch.commits
if len(main_branch_commits) == 0:
raise exercise.wrong_answer([MISSING_FIRST_COMMIT])

first_commit = main_branch_commits[-1]
first_pilot_tag_commit = first_pilot_tag.commit
if first_pilot_tag_commit.hexsha != first_commit.hexsha:
raise exercise.wrong_answer([FIRST_TAG_WRONG_COMMIT])

# Task 2: Verify annotated tag "v1.0" on March commit with correct message
if "v1.0" not in tags:
raise exercise.wrong_answer([MISSING_SECOND_TAG])

# Verify that "v1.0" is an annotated tag
v1_tag = tags["v1.0"]
if v1_tag.tag is None:
raise exercise.wrong_answer([SECOND_TAG_NOT_ANNOTATED])

march_commit = get_commit_from_message(
main_branch_commits, "Update roster for March"
)
if march_commit is None:
raise exercise.wrong_answer([MISSING_MARCH_COMMIT])

v1_tag_commit = v1_tag.commit
if v1_tag_commit.hexsha != march_commit.hexsha:
raise exercise.wrong_answer([SECOND_TAG_WRONG_COMMIT])

# Use strip() and lower() for less strict comparison of tag message
if v1_tag.tag.message.strip().lower() != "first full duty roster":
raise exercise.wrong_answer([WRONG_SECOND_TAG_MESSAGE])

return exercise.to_output(
["Great work using git tag to annotate various commits in the repository!"],
GitAutograderStatus.SUCCESSFUL,
)