Skip to content

Commit

Permalink
Merge pull request #33 from stixes/tests-running-in-github
Browse files Browse the repository at this point in the history
Tests running in GitHub
  • Loading branch information
stixes authored May 18, 2024
2 parents c83676c + b3788c4 commit 896d578
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 4 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/run_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Run Unit Test via Pytest

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with Ruff
run: |
pip install ruff
ruff -- --format=github --target-version=py310 .
continue-on-error: true
- name: Test with pytest
run: |
coverage run -m pytest -v -s
- name: Generate Coverage Report
run: |
coverage report -m
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Detect OS for venv differences
OS=$(shell uname -s)

ifeq ($(OS),Linux)
VENV=.venv/bin
else ifeq ($(OS),Darwin)
VENV=.venv/bin
else
VENV=.venv/Scripts
endif

# Do the normal QA, similar to the GH action
all: requirements lint test

# Lint using ruff. Will not break build
lint:
-${VENV}/ruff -- --format=github --target-version=py310 .

# Run pytests with proper output
test:
${VENV}/pytest -v -s

# Run hellsnake :D
run: requirements
${VENV}/python hell_snake.py

# Use make init to initialize venv
init: requirements
.venv:
python -m venv .venv

# Install/Update requirements
.PHONY: requirements
requirements: .venv
${VENV}/pip install -r requirements.txt

# Clean the venv
clean:
ifeq ($(OS),Linux)
rm -rf .venv
else ifeq ($(OS),Darwin)
rm -rf .venv
else
rm -r .venv
endif

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ To get started with Hell Snake, follow these steps:
pip install -r requirements.txt
```
## Makefile usage (replaces other installation)
1. Initialize using
```
make init
```
2. Run all QA using make
```
make lint test
```
3. Run HellSnake
```
make run
```
Look into the Makefile for the exact targets and what the do.
## Configuration
To configure Hell Snake you need to create or modify a json file named: **settings.json** file located in the root of the project folder
Expand Down
1 change: 1 addition & 0 deletions hell_snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def main():
print("Please configure a supported view framework in the settings.", file=sys.stderr)
sys.exit(1)

view.add_executor_settings()
controller.set_view(view)

if __name__ == "__main__":
Expand Down
7 changes: 6 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ Pillow==10.3.0
pyserial==3.5
PyAutoGUI==0.9.54
PyQt5==5.15.10
pytest==8.2.0

## Tests and linting
pytest==8.2.0
pytest-cov==5.0.0
ruff==0.4.4
coverage-badge==1.1.1
1 change: 0 additions & 1 deletion src/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def __init__(self, model):

def set_view(self, view):
self.view = view
self.view.add_executor_settings()
#TODO Replace with last used loadout
self.set_active_loadout(self.model.get_next_loadout())
self.view.show_interface()
Expand Down
1 change: 0 additions & 1 deletion src/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def __init__(self):
self.strategems.update({index: strate})

self.settings = Settings.getInstance()
self.settings.loadFromFile()

def update_macro_binding(self, key, strategemId):
strategem = self.strategems[strategemId]
Expand Down
8 changes: 7 additions & 1 deletion tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ def test_initial_configuration_should_have_default_values_when_created(self):
@patch("builtins.open", new_callable=mock_open, read_data='{"triggerKey": "shift", "triggerDelay": 50}')
def test_configuration_should_change_when_loaded_from_file(self, mock_file):
settings = Settings.getInstance()
# settings.loadFromFile()
assert settings.triggerKey == "shift"
assert settings.triggerDelay == 50

@patch("builtins.open", new_callable=mock_open, read_data='{"triggerKey": "shift", "triggerDelay": 50}')
def test_unknown_property_should_be_stored_when_created(self, mock_file):
settings = Settings.getInstance()
settings.testProp = "ctrl"
assert settings.testProp == "ctrl"

@patch("builtins.open", mock_open())
def test_should_save_correct_settings_to_file_when_modified(self):
settings = Settings.getInstance()
settings.triggerKey = 'alt'
settings.triggerDelay = 200
settings.testProp = "test"

settings.saveToFile()

Expand All @@ -49,6 +54,7 @@ def test_should_save_correct_settings_to_file_when_modified(self):
# Verify specific values are accurately written to the file
assert written_data["triggerKey"] == "alt"
assert written_data["triggerDelay"] == 200
assert written_data["testProp"] == "test"

def teardown_method(self, method):
# Reset the singleton instance for isolated tests
Expand Down

0 comments on commit 896d578

Please sign in to comment.