Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Two-Play committed Oct 27, 2024
1 parent 1ca901e commit 030e2f5
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 93 deletions.
Empty file added .coveragerc
Empty file.
83 changes: 15 additions & 68 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -1,126 +1,73 @@
name: Docker

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

on:
schedule:
- cron: '25 14 * * *'
push:
branches: [ "master" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "master" ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}
IMAGE_NAME_DOCKERHUB: ${{ vars.DOCKERHUB_USERNAME }}/crafty-discord-bot



jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 #v3.5.0
uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20
with:
cosign-release: 'v2.2.4'

# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}


# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata for GH Registry
id: meta-ghcr
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_GITHUB }}
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

# Extract metadata (tags, labels) for Docker Hub
- name: Extract Docker metadata for Docker Hub
id: meta-dockerhub
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
with:
images: docker.io/${env.IMAGE_NAME_DOCKERHUB}
- name: Get Git version tag
id: git-version
run: echo "GIT_TAG=$(git describe --tags --always)" >> $GITHUB_ENV


# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Build and push Docker image for Docker Hub
id: build-and-push-dockerhub
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-dockerhub.outputs.tags }}
labels: ${{ steps.meta-dockerhub.outputs.labels }}
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.GIT_TAG }}
labels: ${{ steps.meta-ghcr.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
env:
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
TAGS: ${{ steps.meta.outputs.tags }}
TAGS: ${{ steps.meta-ghcr.outputs.tags }}
DIGEST: ${{ steps.build-and-push.outputs.digest }}
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}
43 changes: 24 additions & 19 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
name: Pylint

on: [push]

name: Python Lintingon
on:
push:
branches: [master]
pull_request:
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.6.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
pip install pylint
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
flake8 src --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 src --count --max-complexity=10 --max-line-length=79 --statistics
- name: Lint with Pylint
run: |
pylint core
24 changes: 18 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
name: Test
on: [push]
# This workflow will install Python dependencies and run tests
name: Python Test CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Prepare repo
uses: actions/checkout@master
- name: Test
uses: onichandame/python-test-action@master
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v5.3.0
with:
python-version: "3.10"
architecture: "x64"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
- name: Run tests
run: |
python -m unittest
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ env.sh

__pycache__/
/htmlcov/

.coverage
Empty file added core/__init__.py
Empty file.
Empty file added tests/__init__.py
Empty file.
67 changes: 67 additions & 0 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
import unittest
from unittest.mock import patch

from core.helper import check_env_vars

class TestCheckEnvVars(unittest.TestCase):
@patch.dict(os.environ, {'SERVER_URL': 'http://example.com', 'DISCORD_TOKEN': 'token', 'CRAFTY_TOKEN': 'token'})
def test_all_env_vars_set(self):
check_env_vars()
self.assertTrue(True) # If no exception, the test passes

@patch.dict(os.environ, {}, clear=True)
def test_missing_all_env_vars(self):
with self.assertRaises(SystemExit):
check_env_vars()


class TestCheckMissingEnvVars(unittest.TestCase):
@patch.dict(os.environ, {'DISCORD_TOKEN': 'token', 'CRAFTY_TOKEN': 'token'})
def test_missing_server_url(self):
with self.assertRaises(SystemExit):
check_env_vars()

@patch.dict(os.environ, {'SERVER_URL': 'http://example.com', 'DISCORD_TOKEN': 'token'})
def test_missing_crafty_token_and_missing_credentials(self):
with self.assertRaises(SystemExit):
check_env_vars()

@patch.dict(os.environ, {'SERVER_URL': 'http://example.com', 'DISCORD_TOKEN': 'token',
'USERNAME': 'user', 'PASSWORD': 'pass'})
def test_missing_crafty_token_but_has_credentials(self):
with self.assertRaises(SystemExit):
check_env_vars() # If no exception, the test passes


class TestCheckEmptyEnvVars(unittest.TestCase):
@patch.dict(os.environ, {'SERVER_URL': '', 'DISCORD_TOKEN': 'token', 'CRAFTY_TOKEN': 'token'})
def test_empty_server_url(self):
with self.assertRaises(SystemExit):
check_env_vars()

@patch.dict(os.environ, {'SERVER_URL': 'http://example.com', 'DISCORD_TOKEN': '', 'CRAFTY_TOKEN': 'token'})
def test_empty_discord_token(self):
with self.assertRaises(SystemExit):
check_env_vars()

@patch.dict(os.environ, {'SERVER_URL': 'http://example.com', 'DISCORD_TOKEN': 'token', 'CRAFTY_TOKEN': ''})
def test_empty_crafty_token(self):
with self.assertRaises(SystemExit):
check_env_vars()

@patch.dict(os.environ, {'SERVER_URL': 'http://example.com', 'DISCORD_TOKEN': 'token', 'CRAFTY_TOKEN': '', 'USERNAME': '', 'PASSWORD': ''})
def test_empty_crafty_token_and_empty_credentials(self):
with self.assertRaises(SystemExit):
check_env_vars()


@patch.dict(os.environ, {'SERVER_URL': 'http://example.com', 'DISCORD_TOKEN': 'token', 'CRAFTY_TOKEN': '', 'USERNAME': 'Bla', 'PASSWORD': ''})
def test_empty_crafty_token_and_empty_password(self):
with self.assertRaises(SystemExit):
check_env_vars()


if __name__ == '__main__':
unittest.main()

Empty file added tests/test_main.py
Empty file.
8 changes: 8 additions & 0 deletions tests/test_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import unittest

class TestNetwork(unittest.TestCase):
def setUp(self):
pass

def tearDown(self):
pass
8 changes: 8 additions & 0 deletions tests/test_priniting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import unittest

class TestPrinting(unittest.TestCase):
def setUp(self):
pass

def tearDown(self):
pass

0 comments on commit 030e2f5

Please sign in to comment.