diff --git a/README.md b/README.md index 3839013..083da2e 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,3 @@ # exercises -## Git-Mastery exercise structure - -When you download an exercise, you will get the following folder structure: - - -## `.gitmastery-exercise.json` - -Configuration fields for each exercise: - -1. `exercise_name`: name of exercise -2. `tags`: list of tags for exercise -3. `requires_git`: downloading the exercise will check that Git is installed and that `git config` is already done -4. `requires_github`: downloading the exercise will check that Github and Github CLI is installed -5. `base_files`: provides the files to be included outside of the repository, along with `.gitmastery-exercise.json` and `README.md`, most often used for `answers.txt` -6. `exercise_repo`: configuration for what the exercise repository would look like - 1. `repo_type`: `local` (creates and initializes the folder as a Git repository) or `remote` (reference a repository on Github) - 2. `repo_name`: name of repository during cloning - 3. `repo_title`: (only read if `link` is present) link of repository on Github - 4. `create_fork`: (only read if `link` is present) flag to determine if we need to fork the repository to the student's machine, otherwise it just clones the repository - 5. `init`: (only read if `custom` is present) flag to determine if we will call `git init` on the exercise repository (useful if we don't want to start out with a Git repository) - - -## TODOs - -- [X] Add validation for exercise configuration (e.g. cannot fork + not require Github) - to run as CI +For more details, refer to the official developer documentation: https://git-mastery.github.io/developers diff --git a/hands_on/add_remote.py b/hands_on/add_remote.py new file mode 100644 index 0000000..3c46a58 --- /dev/null +++ b/hands_on/add_remote.py @@ -0,0 +1,37 @@ +import os + +from exercise_utils.git import add, init, commit +from exercise_utils.file import create_or_update_file, append_to_file + +__requires_git__ = True +__requires_github__ = False + + +def download(verbose: bool): + os.makedirs("things") + os.chdir("things") + init(verbose) + create_or_update_file( + "fruits.txt", + """ + apples + bananas + cherries + dragon fruits + figs + """, + ) + add(["fruits.txt"], verbose) + append_to_file("fruits.txt", "figs") + add(["fruits.txt"], verbose) + commit("Insert figs into fruits.txt", verbose) + create_or_update_file("colours.txt", """ + a file for colours + """, + ) + create_or_update_file("shapes.txt", """ + a file for shapes + """, + ) + add(["colours.txt", "shapes.txt"], verbose) + commit("Add colours.txt, shapes.txt", verbose) diff --git a/hands_on/list_commits.py b/hands_on/list_commits.py new file mode 100644 index 0000000..422213f --- /dev/null +++ b/hands_on/list_commits.py @@ -0,0 +1,27 @@ +import os + +from exercise_utils.file import append_to_file, create_or_update_file +from exercise_utils.git import add, commit, init + +__requires_git__ = True +__requires_github__ = False + + +def download(verbose: bool): + os.makedirs("things") + os.chdir("things") + + init(verbose) + + create_or_update_file( + "fruits.txt", + """ + apples + bananas + cherries + dragon fruits + """ + ) + + add(["fruits.txt"], verbose) + commit("Add fruits.txt", verbose) \ No newline at end of file diff --git a/hands_on/update_remote.py b/hands_on/update_remote.py new file mode 100644 index 0000000..9c53e86 --- /dev/null +++ b/hands_on/update_remote.py @@ -0,0 +1,51 @@ +import os +import subprocess + +from exercise_utils.cli import run_command +from exercise_utils.file import create_or_update_file, append_to_file +from exercise_utils.git import add, init, commit + +__requires_git__ = True +__requires_github__ = True + +REPO_NAME = "gitmastery-things" + +def download(verbose: bool): + username = run_command(["gh", "api", "user", "-q", ".login"], verbose).strip() + os.makedirs("things") + os.chdir("things") + init(verbose) + create_or_update_file("fruits.txt", """ + apples + bananas + cherries + dragon fruits + """, + ) + add(["fruits.txt"], verbose) + append_to_file("fruits.txt", "figs") + add(["fruits.txt"], verbose) + commit("Insert figs into fruits.txt", verbose) + create_or_update_file("colours.txt", """ + a file for colours + """, + ) + create_or_update_file("shapes.txt", """ + a file for shapes + """, + ) + add(["colours.txt", "shapes.txt"], verbose) + commit("Add colours.txt, shapes.txt", verbose) + repo_check = subprocess.run( + ["gh", "repo", "view", f"{username}/{REPO_NAME}"], + capture_output=True, + text=True + ) + + if repo_check.returncode == 0: + run_command(["gh", "repo", "delete", REPO_NAME, "--yes"], verbose) + + run_command(["gh", "repo", "create", REPO_NAME, "--public"], verbose) + run_command(["git", "remote", "add", "origin", f"https://github.com/{username}/{REPO_NAME}"], verbose) + + run_command(["git", "push", "-u", "origin", "main"], verbose) \ No newline at end of file diff --git a/test.sh b/test.sh index 20bf59b..513228c 100755 --- a/test.sh +++ b/test.sh @@ -1,5 +1,16 @@ #!/bin/bash +set -e -source venv/bin/activate +if [ -f "venv/bin/activate" ]; then + # Linux / macOS + source venv/bin/activate +elif [ -f "venv/Scripts/activate" ]; then + # Windows (Git Bash, Cygwin, or WSL) + source venv/Scripts/activate +else + echo "Error: Could not find Python virtual environment activation script." + echo "Please create one with: python -m venv venv" + exit 1 +fi python -m pytest $1/tests/test_verify.py -s -vv