From 02bd17ca10c1a16906a7188f602ae84d33270785 Mon Sep 17 00:00:00 2001 From: Kirk Byers Date: Tue, 21 May 2024 11:25:34 -0700 Subject: [PATCH] Adding GH Actions to gne_tests --- .github/workflows/std_tests.yaml | 70 ++++++++++++++++ gh_actions.sh | 23 ++++++ tests/TEST_CONSTANTS.py | 13 ++- tests/conftest.py | 8 +- tests/test_lesson1.py | 5 +- tests/test_lesson4.py | 6 +- tests/test_lesson6.py | 5 ++ tests/test_lesson7.py | 137 +++++++++++++++++++++++++++++++ 8 files changed, 253 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/std_tests.yaml create mode 100755 gh_actions.sh create mode 100644 tests/test_lesson7.py diff --git a/.github/workflows/std_tests.yaml b/.github/workflows/std_tests.yaml new file mode 100644 index 0000000..4f92dd1 --- /dev/null +++ b/.github/workflows/std_tests.yaml @@ -0,0 +1,70 @@ +--- +name: GNE Standard Tests +on: [push,pull_request] + +jobs: + std_tests: + name: std_tests + runs-on: ubuntu-22.04 + steps: + - name: Checkout 'gne_tests' repository + uses: actions/checkout@v4 + + - name: Checkout 'gne_exercises' repository (actions/checkout doesn't work) + run: ./gh_actions.sh + + - name: "List files and directories" + run: | + pwd + ls -al . + ls -al .. + + - name: Setup python + uses: actions/setup-python@v5 + id: cp311 + with: + python-version: '3.11' + architecture: x64 + + - name: Install Poetry + uses: snok/install-poetry@v1 + with: + version: 1.7.1 + virtualenvs-create: true + virtualenvs-in-project: true + + - name: Cache Poetry virtualenv + uses: actions/cache@v4 + id: cached-poetry-dependencies + with: + path: .venv + key: venv-${{ runner.os }}-cache-${{ steps.cp311.outputs.version }}-${{ hashFiles('**/poetry.lock') }} + + - name: Install Virtual Environment Dependencies + run: poetry install -C gne_tests/ + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' + + - name: "List files and directories" + run: | + pwd + ls -al . + ls -al .. + + - name: View installed packages + run: | + poetry run python -m pip list + + - name: Run pylama on 'gne_tests' and 'gne_exercises' + run: | + poetry run pylama . + poetry run pylama ../gne_exercises/ + + - name: Run black on 'gne_tests' and 'gne_exercises' + run: | + poetry run black --check . + poetry run black --check ../gne_exercises/ + + - name: Per-lesson tests + run: | + poetry run pytest -s -v -x tests/ + diff --git a/gh_actions.sh b/gh_actions.sh new file mode 100755 index 0000000..a6e2da1 --- /dev/null +++ b/gh_actions.sh @@ -0,0 +1,23 @@ +#!/bin/sh +if [ "$GITHUB_ACTIONS" != "true" ]; then + echo "Not running in GitHub Actions environment. Exiting..." + exit 1 +fi + +EXERCISE_DIR="gne_exercises" +CURRENT_DIR=$(pwd) +REPO="twin-bridges/gne_exercises" +cd .. + +if [ ! -d "$EXERCISE_DIR" ]; then + mkdir "$EXERCISE_DIR" +fi + +if [ -d "$EXERCISE_DIR" ]; then + cd "$EXERCISE_DIR" + git init --initial-branch=main + git remote add origin https://github.com/"$REPO" + git pull origin main +fi + +cd "$CURRENT_DIR" diff --git a/tests/TEST_CONSTANTS.py b/tests/TEST_CONSTANTS.py index d736209..06dbbed 100644 --- a/tests/TEST_CONSTANTS.py +++ b/tests/TEST_CONSTANTS.py @@ -1,6 +1,15 @@ from pathlib import Path +import os +HOME = Path.home() GIT = "/usr/bin/git" -REPOSITORY = Path.home() / "gne_exercises" DEFAULT_BRANCH = "main" -L4_REPOSITORY = Path.home() / "remotes_gne_exercises" +REPOSITORY = HOME / "gne_exercises" +L4_REPOSITORY = HOME / "remotes_gne_exercises" + +if os.getenv("GITHUB_ACTIONS"): + # GH-Actions uses '/home/runner/work/gne_tests/gne_tests' + # with the very final dir being the repo. + HOME = HOME / "work" / "gne_tests" + REPOSITORY = HOME / "gne_exercises" + L4_REPOSITORY = HOME / "remotes_gne_exercises" diff --git a/tests/conftest.py b/tests/conftest.py index be859ab..68dfd9c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,9 @@ import pytest -from pathlib import Path import shutil from datetime import datetime +from TEST_CONSTANTS import HOME + def mv_remotes_gne_exercises(src, dst): shutil.move(str(src), str(dst)) @@ -11,7 +12,7 @@ def mv_remotes_gne_exercises(src, dst): @pytest.fixture(scope="session", autouse=True) def backup_and_restore_gitconfig(): # BEFORE TESTS #### - home = Path.home() + home = HOME gitconfig = home / ".gitconfig" timestamp = datetime.now().strftime("%Y%m%d-%H%M%S") @@ -34,6 +35,3 @@ def backup_and_restore_gitconfig(): if backup_gitconfig.exists(): shutil.copy(backup_gitconfig, gitconfig) print(f"\n\nRestored from {backup_gitconfig} to {gitconfig}\n") - # backup_gitconfig.unlink() - else: - print(f"\n\nNo backup gitconfig found at {backup_gitconfig}\n") diff --git a/tests/test_lesson1.py b/tests/test_lesson1.py index 9579c4c..c59b720 100644 --- a/tests/test_lesson1.py +++ b/tests/test_lesson1.py @@ -1,9 +1,6 @@ import pytest -from pathlib import Path from utilities import subprocess_runner, file_dir_exists - -GIT = "/usr/bin/git" -REPOSITORY = Path.home() / "gne_exercises" +from TEST_CONSTANTS import GIT, REPOSITORY def test_git_version(): diff --git a/tests/test_lesson4.py b/tests/test_lesson4.py index 3532d54..545015c 100644 --- a/tests/test_lesson4.py +++ b/tests/test_lesson4.py @@ -6,7 +6,7 @@ subprocess_runner, file_dir_exists, ) -from TEST_CONSTANTS import GIT, L4_REPOSITORY +from TEST_CONSTANTS import GIT, L4_REPOSITORY, HOME def test_exercise1(): @@ -14,7 +14,7 @@ def test_exercise1(): test_dir = Path.cwd() - home = Path.home() + home = HOME os.chdir(home) directory = home / "remotes_gne_exercises" @@ -86,7 +86,7 @@ def test_exercise2(): std_out, _, _ = subprocess_runner(cmd_list, L4_REPOSITORY, check_errors=True) assert re.search(r"\*.l4\-testing", std_out) - home = Path.home() + home = HOME directory = home / "remotes_gne_exercises" / "lesson4" filename = "WELCOME.md" file_dir_exists(directory, filename, invert=False) diff --git a/tests/test_lesson6.py b/tests/test_lesson6.py index 661e2bf..e6498dd 100644 --- a/tests/test_lesson6.py +++ b/tests/test_lesson6.py @@ -21,6 +21,11 @@ def commit_change(file_name): def test_exercise1a(): + cmd_list = [GIT, "tag", "--list"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + print(std_out) + assert "v0.1" in std_out + commit = "983a8b7" git_checkout(commit) diff --git a/tests/test_lesson7.py b/tests/test_lesson7.py new file mode 100644 index 0000000..661e2bf --- /dev/null +++ b/tests/test_lesson7.py @@ -0,0 +1,137 @@ +import re + +from utilities import subprocess_runner, git_checkout +from TEST_CONSTANTS import GIT, REPOSITORY + + +def commit_change(file_name): + cmd_list = [GIT, "add", file_name] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + + cmd_list = [GIT, "commit", "-m", "TESTS: Lesson6, Ex3"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "TESTS: Lesson6, Ex3" in std_out + assert "1 file changed, 0 insertions(+), 0 deletions(-)" in std_out + assert "create mode 100644 test_lesson6_ex3.txt" in std_out + + # Clean 'git status' + cmd_list = [GIT, "status"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "nothing to commit, working tree clean" in std_out + + +def test_exercise1a(): + commit = "983a8b7" + git_checkout(commit) + + cmd_list = [GIT, "log", "--oneline"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + git_log_line1 = std_out.splitlines()[0] + assert re.search(r"983a8b7 Tag exercise", std_out) + + git_checkout(tag="v0.1") + + cmd_list = [GIT, "log", "--oneline"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + git_log_line1 = std_out.splitlines()[0] + assert re.search(r"983a8b7 Tag exercise", git_log_line1) + + git_checkout(branch="main") + + +def test_exercise1b(): + cmd_list = [GIT, "tag", "--list"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "v0.1" in std_out + + +def test_exercise2a(): + cmd_list = [GIT, "config", "--global", "init.defaultBranch", "test123"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + + cmd_list = [GIT, "config", "init.defaultBranch"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "test123" in std_out + + cmd_list = [GIT, "config", "--global", "init.defaultBranch", "main"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + + cmd_list = [GIT, "config", "init.defaultBranch"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "main" in std_out + + +def test_exercise2b(): + cmd_list = [GIT, "config", "--list"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "init.defaultbranch=main" in std_out + assert "core.bare=false" in std_out + + +def test_exercise2c(): + cmd_list = [GIT, "config", "pull.rebase", "false"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + + cmd_list = [GIT, "config", "--list"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "pull.rebase=false" in std_out + + +def test_exercise2d(): + cmd_list = [GIT, "config", "--list", "--global"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "init.defaultbranch=main" in std_out + + cmd_list = [GIT, "config", "--list", "--local"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "pull.rebase=false" in std_out + + +def test_exercise3(): + # Ex 3a + cmd_list = [GIT, "log", "--oneline"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + first_line = std_out.splitlines()[0] + original_commit = first_line.split()[0] + + file_name = "test_lesson6_ex3.txt" + cmd_list = ["touch", file_name] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + + commit_change(file_name) + + # Ex 3b + cmd_list = [GIT, "reset", "--soft", "HEAD~1"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + + # Soft reset should restore the change to staging + cmd_list = [GIT, "status"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + pattern = re.escape(r"new file: test_lesson6_ex3.txt") + assert re.search(pattern, std_out) + + # Ex 3c + commit_change(file_name) + + # Ex 3d + cmd_list = [GIT, "reset", "--mixed", "HEAD~1"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + + # Mixed reset should undo the commit and revert staging (unstage the file) + cmd_list = [GIT, "status"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "Untracked files:" in std_out + assert """use "git add ..." to include""" in std_out + assert "test_lesson6_ex3.txt" in std_out + + # Ex 3e + commit_change(file_name) + + # Ex 3f + cmd_list = [GIT, "reset", "--hard", "HEAD~1"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert f"HEAD is now at {original_commit}" in std_out + + cmd_list = [GIT, "status"] + std_out, _, _ = subprocess_runner(cmd_list, REPOSITORY, check_errors=True) + assert "nothing to commit, working tree clean" in std_out