Skip to content

Commit

Permalink
Adding tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenric committed Jun 11, 2024
1 parent add1110 commit f3fe555
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 5 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: check
on:
push:
pull_request:
schedule:
- cron: "0 8 * * *"

concurrency:
group: check-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
name: test with ${{ matrix.py }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
py:
- "3.12"
- "3.11"
- "3.10"
- "3.9"
- "3.8"
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup python for test ${{ matrix.py }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.py }}
- name: Install tox
run: python -m pip install tox-gh>=1.2
- name: Setup test suite
run: tox -vv --notest
- name: Run test suite
run: tox --skip-pkg-install
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ name = "pypi"
click = "*"

[dev-packages]
pytest = "*"

[requires]
python_version = "3.6"
61 changes: 56 additions & 5 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions tests/test_git_quotes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# test_git_quotes.py

import click
import os
import pytest
import json
import random

from click.testing import CliRunner

from quotes.utils import create_git_repository
from quotes.git_quotes import (
on,
off,
toggle,
refresh,
status
)

def test_create_git_repository():
assert os.path.isdir(".git")

def test_get_quote():

# Load quotes file
dir_path = os.path.dirname(os.path.realpath(__file__))
quotes_file = os.path.join(dir_path, '..', 'quotes', 'hooks', 'quotes.json')
with open(quotes_file, "r", encoding='utf-8') as fquotes:
quotes = json.load(fquotes)

# Get random quote
quote = random.choice(quotes)

# Quotes is not empty and is in file.
assert len(quote) > 0 and (quote in quotes)

def test_activate_git_quotes():
runner = CliRunner()
result = runner.invoke(on, ["--force"])

assert result.exit_code == 0

# Checks if hook is copied into repository
assert os.path.isfile(os.path.join(".", '.git', 'hooks', 'prepare-commit-msg'))


def test_deactivate_git_quotes():
runner = CliRunner()
result = runner.invoke(off)

assert result.exit_code == 0

# Checks if hook is missing
assert not os.path.isfile(os.path.join('.git', 'hooks', 'prepare-commit-msg'))

def test_toggle_git_quotes():
runner = CliRunner()
result_status = runner.invoke(status)

assert result_status.exit_code == 0

if result_status == "\nGit-quotes is unactive! :@\n":
result_toggle_act= runner.invoke(toggle)
assert result_toggle_act.exit_code == 0
assert result_toggle_act.output in ["\nGit-quotes has been activated successfully :)\n", "\nGit-quotes is active!\n"]
elif result_status == "\nGit-quotes is active!\n":
result_toggle_disable = runner.invoke(toggle)
assert result_toggle_disable.exit_code == 0
msg = "\nGit-quotes has been disabled! :(\n\nUse --default to disable git-quotes by default\n"
assert result_toggle_disable.output == msg

def test_refresh_git_quotes():
runner = CliRunner()

result_status = runner.invoke(status)
result_refresh = runner.invoke(refresh)

assert result_status.exit_code == 0
assert result_refresh.exit_code == 0

if result_status.output == "\nGit-quotes is active! :)\n":
assert result_refresh.output == "\nGit-quotes is active\n\nNo hook has changed!\n"
assert os.path.isfile(os.path.join('.git', 'hooks', 'prepare-commit-msg'))
elif result_status.output == "\nGit-quotes is unactive! :@\n":
assert result_refresh.output == "Git-quotes is unactive\n\nNo hook has changed!\n"
assert os.path.isfile(os.path.join('.git', 'hooks', 'prepare-commit-msg-quotes'))

def test_check_git_quotes_status():

runner = CliRunner()

# Checks if status off when deactivated
result = runner.invoke(on)
assert result.output in ["\nGit-quotes has been activated successfully :)\n", "\nGit-quotes is active!\n"]

result = runner.invoke(off)

msg = "\nGit-quotes has been disabled! :(\n\nUse --default to disable git-quotes by default\n"
assert result.output == msg
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gh]
python =
3.12 = py312
3.11 = py311, type
3.10 = py310
3.9 = py39
3.8 = py38

0 comments on commit f3fe555

Please sign in to comment.