diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 805d063e..cbf66c8c 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -27,9 +27,9 @@ jobs: - name: autopep8 run: | - autopep8 --in-place --aggressive --recursive ompackagemanager + autopep8 --in-place --aggressive --recursive ompackagemanager/ tests/ if ! git diff --exit-code; then - echo "❌ Code not formatted. Please run 'autopep8 --in-place --aggressive --recursive ompackagemanager' locally." + echo "❌ Code not formatted. Please run 'autopep8 --in-place --aggressive --recursive ompackagemanager/ tests/' locally." exit 1 else echo "✅ Code is properly formatted." diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f4501f0d..e36dc33d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,6 +8,32 @@ on: - master jobs: + unit-tests: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ['3.13'] + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + cache: 'pip' + + - name: Install Dependencies + run: | + pip install -r requirements.txt + + - name: Run unit tests + env: + GITHUB_AUTH: ${{ secrets.GITHUB_TOKEN }} + run: | + python -m unittest discover -s tests + test-generate: runs-on: ubuntu-latest permissions: diff --git a/README.md b/README.md index 3e667d0f..7ce12a14 100644 --- a/README.md +++ b/README.md @@ -174,5 +174,11 @@ All Python code is formatted with [autopep8](https://pypi.org/project/autopep8/): ```bash -autopep8 --in-place --aggressive --recursive ompackagemanager +autopep8 --in-place --aggressive --recursive ompackagemanager/ tests/ +``` + +Unit tests can be run with: + +```bash +python -m unittest discover -s test ``` diff --git a/ompackagemanager/check_missing.py b/ompackagemanager/check_missing.py index d779d70c..0795b2e1 100755 --- a/ompackagemanager/check_missing.py +++ b/ompackagemanager/check_missing.py @@ -1,7 +1,6 @@ -from github import Github +from github import Github, Auth import json import os -import pygit2 def main(): @@ -12,15 +11,16 @@ def main(): Prints the missing repositories. """ - gh_auth = os.environ["GITHUB_AUTH"] - g = Github(gh_auth) - data = json.load(open("repos.json")) + token = Auth.Token(os.environ["GITHUB_AUTH"]) + github_api = Github(auth=token) + with open("repos.json", "r") as f: + data = json.load(f) namesInRepos = set() for entry in data.values(): if "github" in entry: namesInRepos.add(entry["github"]) - for repo in list(g.get_user("modelica-3rdparty").get_repos()): + for repo in list(github_api.get_user("modelica-3rdparty").get_repos()): if repo.full_name in namesInRepos: continue if repo.fork and repo.parent.full_name in namesInRepos: diff --git a/ompackagemanager/genindex.py b/ompackagemanager/genindex.py index 184c6c3e..92d5470b 100755 --- a/ompackagemanager/genindex.py +++ b/ompackagemanager/genindex.py @@ -116,8 +116,10 @@ def checkProvides(libName, lib, indexdata): def main(): """Generate `index.json` database from `rawdata.json`. """ - repos = json.load(open("repos.json")) - rawdata = json.load(open("rawdata.json")) + with open("repos.json", "r") as f: + repos = json.load(f) + with open("rawdata.json", "r") as f: + rawdata = json.load(f) indexdata = {"libs": {}, "mirrors": [ "https://libraries.openmodelica.org/cache/"]} diff --git a/ompackagemanager/updateinfo.py b/ompackagemanager/updateinfo.py index 98db4924..9e3163a7 100755 --- a/ompackagemanager/updateinfo.py +++ b/ompackagemanager/updateinfo.py @@ -7,7 +7,7 @@ import requests import shutil import zipfile -from github import Github +from github import Github, Auth from atlassian import Bitbucket from ompackagemanager import common @@ -61,12 +61,14 @@ def main(): Queries the repositories where the libraries are stored. """ - gh_auth = os.environ["GITHUB_AUTH"] - g = Github(gh_auth) + + token = Auth.Token(os.environ["GITHUB_AUTH"]) + github_api = Github(auth=token) omc = OMPython.OMCSessionZMQ() - data = json.load(open("repos.json")) + with open("repos.json", "r") as f: + data = json.load(f) if os.path.exists("rawdata.json"): serverdata = json.load(open("rawdata.json")) else: @@ -96,7 +98,7 @@ def main(): if "github" in entry or "git" in entry or "zipfiles" in entry: if "github" in entry: try: - r = g.get_repo(entry["github"]) + r = github_api.get_repo(entry["github"]) branches = list((b.name, b.commit.sha) for b in r.get_branches()) tags = list((b.name, b.commit.sha) for b in r.get_tags()) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 00000000..6a132de0 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,36 @@ +import unittest +from io import StringIO +import sys + +from ompackagemanager import __main__ + + +class TestCLI(unittest.TestCase): + def test_run_check_missing(self): + saved_stdout = sys.stdout + try: + out = StringIO() + sys.stdout = out + + __main__.main(["check-missing"]) + + self.assertIn('''modelica-3rdparty/BuildSysPro +modelica-3rdparty/electrolytemedia +modelica-3rdparty/ExternalMedia +modelica-3rdparty/FluidSystemComponents +modelica-3rdparty/Greenhouses-Library +modelica-3rdparty/HeatTransferComponents +modelica-3rdparty/LCC_HVDC +modelica-3rdparty/MoSDH +modelica-3rdparty/NeuralNetwork +modelica-3rdparty/OpenHydraulics +modelica-3rdparty/ShipSIM +modelica-3rdparty/SMEHV +modelica-3rdparty/urdfmodelica +''', out.getvalue()) + finally: + sys.stdout = saved_stdout + + +if __name__ == "__main__": + unittest.main()