Skip to content

Commit d4e6dad

Browse files
authored
Adding unit test (#47)
* Fixing DeprecationWarning for login_or_token - Fixes "DeprecationWarning: Argument login_or_token is deprecated, please use auth=github.Auth.Token(...)" * Adding CLI test and workflow job for unit tests - Also fixed ResourceWarning: unclosed file <_io.TextIOWrapper name='repos.json' mode='r' encoding='UTF-8'> data = json.load(open("repos.json"))
1 parent 243b9c8 commit d4e6dad

8 files changed

Lines changed: 88 additions & 16 deletions

File tree

.github/workflows/format.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ jobs:
2727

2828
- name: autopep8
2929
run: |
30-
autopep8 --in-place --aggressive --recursive ompackagemanager
30+
autopep8 --in-place --aggressive --recursive ompackagemanager/ tests/
3131
if ! git diff --exit-code; then
32-
echo "❌ Code not formatted. Please run 'autopep8 --in-place --aggressive --recursive ompackagemanager' locally."
32+
echo "❌ Code not formatted. Please run 'autopep8 --in-place --aggressive --recursive ompackagemanager/ tests/' locally."
3333
exit 1
3434
else
3535
echo "✅ Code is properly formatted."

.github/workflows/test.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ on:
88
- master
99

1010
jobs:
11+
unit-tests:
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
python-version: ['3.13']
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v5
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
cache: 'pip'
26+
27+
- name: Install Dependencies
28+
run: |
29+
pip install -r requirements.txt
30+
31+
- name: Run unit tests
32+
env:
33+
GITHUB_AUTH: ${{ secrets.GITHUB_TOKEN }}
34+
run: |
35+
python -m unittest discover -s tests
36+
1137
test-generate:
1238
runs-on: ubuntu-latest
1339
permissions:

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,11 @@ All Python code is formatted with
174174
[autopep8](https://pypi.org/project/autopep8/):
175175

176176
```bash
177-
autopep8 --in-place --aggressive --recursive ompackagemanager
177+
autopep8 --in-place --aggressive --recursive ompackagemanager/ tests/
178+
```
179+
180+
Unit tests can be run with:
181+
182+
```bash
183+
python -m unittest discover -s test
178184
```

ompackagemanager/check_missing.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from github import Github
1+
from github import Github, Auth
22
import json
33
import os
4-
import pygit2
54

65

76
def main():
@@ -12,15 +11,16 @@ def main():
1211
Prints the missing repositories.
1312
"""
1413

15-
gh_auth = os.environ["GITHUB_AUTH"]
16-
g = Github(gh_auth)
17-
data = json.load(open("repos.json"))
14+
token = Auth.Token(os.environ["GITHUB_AUTH"])
15+
github_api = Github(auth=token)
16+
with open("repos.json", "r") as f:
17+
data = json.load(f)
1818
namesInRepos = set()
1919
for entry in data.values():
2020
if "github" in entry:
2121
namesInRepos.add(entry["github"])
2222

23-
for repo in list(g.get_user("modelica-3rdparty").get_repos()):
23+
for repo in list(github_api.get_user("modelica-3rdparty").get_repos()):
2424
if repo.full_name in namesInRepos:
2525
continue
2626
if repo.fork and repo.parent.full_name in namesInRepos:

ompackagemanager/genindex.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ def checkProvides(libName, lib, indexdata):
116116
def main():
117117
"""Generate `index.json` database from `rawdata.json`.
118118
"""
119-
repos = json.load(open("repos.json"))
120-
rawdata = json.load(open("rawdata.json"))
119+
with open("repos.json", "r") as f:
120+
repos = json.load(f)
121+
with open("rawdata.json", "r") as f:
122+
rawdata = json.load(f)
121123

122124
indexdata = {"libs": {}, "mirrors": [
123125
"https://libraries.openmodelica.org/cache/"]}

ompackagemanager/updateinfo.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import requests
88
import shutil
99
import zipfile
10-
from github import Github
10+
from github import Github, Auth
1111
from atlassian import Bitbucket
1212

1313
from ompackagemanager import common
@@ -61,12 +61,14 @@ def main():
6161
6262
Queries the repositories where the libraries are stored.
6363
"""
64-
gh_auth = os.environ["GITHUB_AUTH"]
65-
g = Github(gh_auth)
64+
65+
token = Auth.Token(os.environ["GITHUB_AUTH"])
66+
github_api = Github(auth=token)
6667

6768
omc = OMPython.OMCSessionZMQ()
6869

69-
data = json.load(open("repos.json"))
70+
with open("repos.json", "r") as f:
71+
data = json.load(f)
7072
if os.path.exists("rawdata.json"):
7173
serverdata = json.load(open("rawdata.json"))
7274
else:
@@ -96,7 +98,7 @@ def main():
9698
if "github" in entry or "git" in entry or "zipfiles" in entry:
9799
if "github" in entry:
98100
try:
99-
r = g.get_repo(entry["github"])
101+
r = github_api.get_repo(entry["github"])
100102
branches = list((b.name, b.commit.sha)
101103
for b in r.get_branches())
102104
tags = list((b.name, b.commit.sha) for b in r.get_tags())

tests/__init__.py

Whitespace-only changes.

tests/test_main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import unittest
2+
from io import StringIO
3+
import sys
4+
5+
from ompackagemanager import __main__
6+
7+
8+
class TestCLI(unittest.TestCase):
9+
def test_run_check_missing(self):
10+
saved_stdout = sys.stdout
11+
try:
12+
out = StringIO()
13+
sys.stdout = out
14+
15+
__main__.main(["check-missing"])
16+
17+
self.assertIn('''modelica-3rdparty/BuildSysPro
18+
modelica-3rdparty/electrolytemedia
19+
modelica-3rdparty/ExternalMedia
20+
modelica-3rdparty/FluidSystemComponents
21+
modelica-3rdparty/Greenhouses-Library
22+
modelica-3rdparty/HeatTransferComponents
23+
modelica-3rdparty/LCC_HVDC
24+
modelica-3rdparty/MoSDH
25+
modelica-3rdparty/NeuralNetwork
26+
modelica-3rdparty/OpenHydraulics
27+
modelica-3rdparty/ShipSIM
28+
modelica-3rdparty/SMEHV
29+
modelica-3rdparty/urdfmodelica
30+
''', out.getvalue())
31+
finally:
32+
sys.stdout = saved_stdout
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)