Skip to content

Commit

Permalink
Adding GH Actions to gne_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers committed May 22, 2024
1 parent a816d67 commit c38555c
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 8 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/std_tests.yaml
Original file line number Diff line number Diff line change
@@ -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/
23 changes: 23 additions & 0 deletions gh_actions.sh
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 11 additions & 2 deletions tests/TEST_CONSTANTS.py
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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")
Expand Down
5 changes: 1 addition & 4 deletions tests/test_lesson1.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
137 changes: 137 additions & 0 deletions tests/test_lesson7.py
Original file line number Diff line number Diff line change
@@ -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 <file>..." 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

0 comments on commit c38555c

Please sign in to comment.