Repository: kraina-ai/srai (language: python)

Issue:
Setup PyPI project and workflow for automatic deployment



Discussion / hints:
Github Actions contains ready to use workflow
https://pdm.fming.dev/latest/usage/advanced/#export-requirementstxt-or-setuppy
Commands from pdm to use as precommit hook

Fix patch:
diff --git a/.github/workflows/docs-ci.yml b/.github/workflows/docs-ci.yml
deleted file mode 100644
index 80ce06a4..00000000
--- a/.github/workflows/docs-ci.yml
+++ /dev/null
@@ -1,21 +0,0 @@
-name: docs-ci
-on:
-  push:
-    branches:
-      - main
-jobs:
-  deploy:
-    runs-on: ubuntu-latest
-    steps:
-      - uses: actions/checkout@v2
-      - uses: actions/setup-python@v4
-        with:
-          python-version: 3.8
-      - name: Install pdm
-        run: pip install pdm
-      - name: Generate requirements.txt
-        run: pdm export -G docs > requirements.txt
-      - name: Install dependencies
-        run: pip install -r requirements.txt
-      - name: Deploy documentation
-        run: mkdocs gh-deploy --force
diff --git a/.github/workflows/full-ci.yml b/.github/workflows/full-ci.yml
new file mode 100644
index 00000000..ff6328b2
--- /dev/null
+++ b/.github/workflows/full-ci.yml
@@ -0,0 +1,60 @@
+name: full-ci
+on:
+  push:
+    branches:
+      - main
+
+jobs:
+  run-tests:
+    uses: ./.github/workflows/_tests.yml
+
+  build-n-publish:
+    name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI
+    needs: [run-tests]
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+      - uses: pdm-project/setup-pdm@main
+        name: Setup PDM
+        with:
+          python-version: 3.8
+          architecture: x64
+          enable-pep582: true
+      - name: Publish distribution 📦 to PyPI
+        if: ${{ startsWith(github.ref, 'refs/tags') }}
+        run: pdm publish --repository pypi --username __token__ --password ${{ secrets.PYPI_API_TOKEN }} --verbose
+      - run: pip install toml
+      - uses: jannekem/run-python-script-action@v1
+        name: Rename test version
+        with:
+          script: |
+            import time
+            import toml
+            data = toml.load("pyproject.toml")
+            current_date = time.strftime("%Y%m%d%H%M%S")
+            data['project']['version']=f"{data['project']['version']}-{current_date}"
+            data['tool']['bumpver']['current_version']=f"{data['tool']['bumpver']['current_version']}-{current_date}"
+            f = open("pyproject.toml",'w')
+            toml.dump(data, f)
+            f.close()
+      - name: Publish distribution 📦 to Test PyPI
+        run: pdm publish --repository testpypi --username __token__ --password ${{ secrets.PYPI_TEST_API_TOKEN }} --comment ${{ github.sha }} --verbose
+
+  generate-docs:
+    name: Publish library 📦 documentation 📄 to Github Pages 🌐
+    needs: [run-tests]
+    if: ${{ startsWith(github.ref, 'refs/tags') }}
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+      - uses: actions/setup-python@v4
+        with:
+          python-version: 3.8
+      - name: Install pdm
+        run: pip install pdm
+      - name: Generate requirements.txt
+        run: pdm export -G docs > requirements.txt
+      - name: Install dependencies
+        run: pip install -r requirements.txt
+      - name: Deploy documentation
+        run: mkdocs gh-deploy --force
diff --git a/pdm.lock b/pdm.lock
index 7b27760e..1d4124f5 100644
--- a/pdm.lock
+++ b/pdm.lock
@@ -1156,6 +1156,32 @@ version = "6.2"
 requires_python = ">= 3.7"
 summary = "Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed."
 
+[[package]]
+name = "tox"
+version = "3.26.0"
+requires_python = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7"
+summary = "tox is a generic virtualenv management and test command line tool"
+dependencies = [
+    "colorama>=0.4.1; platform_system == \"Windows\"",
+    "filelock>=3.0.0",
+    "packaging>=14",
+    "pluggy>=0.12.0",
+    "py>=1.4.17",
+    "six>=1.14.0",
+    "tomli>=2.0.1; python_version >= \"3.7\" and python_version < \"3.11\"",
+    "virtualenv!=20.0.0,!=20.0.1,!=20.0.2,!=20.0.3,!=20.0.4,!=20.0.5,!=20.0.6,!=20.0.7,>=16.0.0",
+]
+
+[[package]]
+name = "tox-pdm"
+version = "0.6.0"
+requires_python = ">=3.7"
+summary = "A plugin for tox that utilizes PDM as the package manager and installer"
+dependencies = [
+    "toml>=0.10",
+    "tox>=3.18.0",
+]
+
 [[package]]
 name = "traitlets"
 version = "5.4.0"
@@ -1209,7 +1235,7 @@ summary = "Backport of pathlib-compatible object wrapper for zip files"
 
 [metadata]
 lock_version = "4.0"
-content_hash = "sha256:7cda8e3bd51db0b8e316d3e8a545d8d4f4a950c87abe696a8039e78cbeaf8649"
+content_hash = "sha256:7b2fd15901fc904b3d419215f6eeb8ed372bf0f5a093d312a557b362fdb1a4ba"
 
 [metadata.files]
 "about-time 3.1.1" = [
@@ -2420,6 +2446,14 @@ content_hash = "sha256:7cda8e3bd51db0b8e316d3e8a545d8d4f4a950c87abe696a8039e78cb
     {url = "https://files.pythonhosted.org/packages/f9/51/6f63a166d9a3077be100cbb13dcbce434e5654daaaa7003b0a045b9f6edf/tornado-6.2-cp37-abi3-musllinux_1_1_x86_64.whl", hash = "sha256:1d54d13ab8414ed44de07efecb97d4ef7c39f7438cf5e976ccd356bebb1b5fca"},
     {url = "https://files.pythonhosted.org/packages/fb/bd/074254a55bfc82d7a1886abbd803600ef01cbd76ee66bc30307b2724130b/tornado-6.2-cp37-abi3-macosx_10_9_universal2.whl", hash = "sha256:20f638fd8cc85f3cbae3c732326e96addff0a15e22d80f049e00121651e82e72"},
 ]
+"tox 3.26.0" = [
+    {url = "https://files.pythonhosted.org/packages/59/14/1931cd6cb9804f2326cccdd750af32a5095132b7d73cc33577644ab59888/tox-3.26.0.tar.gz", hash = "sha256:44f3c347c68c2c68799d7d44f1808f9d396fc8a1a500cbc624253375c7ae107e"},
+    {url = "https://files.pythonhosted.org/packages/a0/62/4a490d6e064727ced44b2a7897bf3f8cc09fa9443f2ef258ff56973d96e1/tox-3.26.0-py2.py3-none-any.whl", hash = "sha256:bf037662d7c740d15c9924ba23bb3e587df20598697bb985ac2b49bdc2d847f6"},
+]
+"tox-pdm 0.6.0" = [
+    {url = "https://files.pythonhosted.org/packages/3a/8e/92d832ead1c7d7899484ac01ec91c88b953cccc94c9ed5e04ed097d6a57d/tox-pdm-0.6.0.tar.gz", hash = "sha256:b2918c5cf0ca8cfbf30d4c0b41446ecf9c2c7254e102deb5349462a7eb8194bb"},
+    {url = "https://files.pythonhosted.org/packages/fa/45/e261c0499707fb0171a0742a591fb02f950c77fa04d40c18f5d4338dad55/tox_pdm-0.6.0-py3-none-any.whl", hash = "sha256:3e9067ee1620ee43948e063e32239e84020a8b37ff1893cf7797fbd6acb43e46"},
+]
 "traitlets 5.4.0" = [
     {url = "https://files.pythonhosted.org/packages/7d/28/8f4757d68ee7c46e0733dda81595f1bd107fda7bc0c6a577912387e87d86/traitlets-5.4.0-py3-none-any.whl", hash = "sha256:93663cc8236093d48150e2af5e2ed30fc7904a11a6195e21bab0408af4e6d6c8"},
     {url = "https://files.pythonhosted.org/packages/a2/4d/1f59a77fba6e8e9feea7a168652030564089d66c06492ba5670e73b0e078/traitlets-5.4.0.tar.gz", hash = "sha256:3f2c4e435e271592fe4390f1746ea56836e3a080f84e7833f0f801d9613fec39"},
diff --git a/pyproject.toml b/pyproject.toml
index fcc12a5c..52f0114d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -50,6 +50,7 @@ dev = [
     "ipykernel>=6.16.0",
     "alive-progress>=2.4.1",
     "bumpver>=2022.1118",
+    "tox-pdm>=0.6.0",
 ]
 
 [tool.black]
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 00000000..f8737030
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,26 @@
+[tox]
+envlist =
+    python3.8
+    python3.9
+    python3.10
+    report
+isolated_build = True
+
+[testenv]
+setenv =
+    python3.{8,9,10}: COVERAGE_FILE = .coverage.{envname}
+groups =
+    dev
+commands =
+    pytest --cov --cov-append --cov-report=term-missing tests/
+
+depends =
+    report: python3.8,python3.9,python3.10
+
+[testenv:report]
+deps = coverage
+skip_install = true
+commands =
+    coverage combine
+    coverage html
+    coverage report --fail-under=100


Test patch:
diff --git a/.github/workflows/_tests.yml b/.github/workflows/_tests.yml
new file mode 100644
index 00000000..eaa350e8
--- /dev/null
+++ b/.github/workflows/_tests.yml
@@ -0,0 +1,23 @@
+name: tests
+on: workflow_call
+
+jobs:
+  run-tests:
+    name: Run tests 🛠️ on multiple systems 🖥️ and Python 🐍 versions
+    runs-on: ${{ matrix.os }}
+    strategy:
+      matrix:
+        os: [ubuntu-latest, macos-latest]
+        python-version: ["3.8", "3.9", "3.10"]
+    steps:
+      - uses: actions/checkout@v2
+      - uses: pdm-project/setup-pdm@main
+        name: Setup PDM (Python ${{ matrix.python-version }})
+        with:
+          python-version: ${{ matrix.python-version }}
+          architecture: x64
+          enable-pep582: true
+      - name: Install dependencies
+        run: pdm install -d
+      - name: Run tests with tox
+        run: pdm run tox -e python${{ matrix.python-version }}
diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml
new file mode 100644
index 00000000..324099d4
--- /dev/null
+++ b/.github/workflows/run-tests.yml
@@ -0,0 +1,9 @@
+name: run-tests
+on:
+  pull_request:
+    branches:
+      - main
+
+jobs:
+  run-tests:
+    uses: ./.github/workflows/_tests.yml
diff --git a/tests/base_test.py b/tests/base_test.py
new file mode 100644
index 00000000..995c32ba
--- /dev/null
+++ b/tests/base_test.py
@@ -0,0 +1,6 @@
+"""Docstring."""
+
+
+def test_base_assert() -> None:
+    """Base test for CI purposes."""
+    assert 2 == 2