Skip to content

Commit 411472d

Browse files
authored
Fix unit test, use pathlib (#56)
1 parent 52f83cc commit 411472d

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

tests/test_libentry.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
import unittest
22
import json
3-
import os
43
from pathlib import Path
54
import shutil
65
import pygit2
76
import OMPython
87

9-
from ompackagemanager.updateinfo import new_libentry, getgitrepo
8+
from ompackagemanager.updateinfo import new_libentry
109

1110

1211
class TestNewLibentry(unittest.TestCase):
1312
@classmethod
1413
def setUpClass(cls):
1514
"""Clone git repositories, start OMC session."""
1615

17-
def checkout_repo(github_repo: str, refname: str) -> str:
16+
def checkout_repo(github_repo: str, refname: str) -> Path:
1817
"""Clone and checkout a git repository from GitHub"""
1918
giturl = "https://github.com/%s.git" % github_repo
20-
repopath = os.path.join(cls.cache_dir, github_repo.split("/")[-1])
19+
repopath = cls.cache_dir.joinpath(github_repo.split("/")[-1])
2120

22-
if not os.path.exists(repopath):
21+
if not repopath.is_dir():
2322
pygit2.clone_repository(giturl, repopath)
2423
gitrepo = pygit2.Repository(repopath)
2524

@@ -32,10 +31,10 @@ def checkout_repo(github_repo: str, refname: str) -> str:
3231
gitrepo.get(refname),
3332
strategy=pygit2.GIT_CHECKOUT_FORCE | pygit2.GIT_CHECKOUT_RECREATE_MISSING)
3433

35-
return os.path.normpath(os.path.join(gitrepo.path, os.pardir))
34+
return Path(gitrepo.path).joinpath().parent.resolve()
3635

37-
cls.cache_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp-cache")
38-
os.makedirs(cls.cache_dir, exist_ok=True)
36+
cls.cache_dir = Path(__file__).parent.joinpath("tmp-cache")
37+
Path.mkdir(cls.cache_dir, parents=True, exist_ok=True)
3938

4039
cls.omc = OMPython.OMCSessionZMQ()
4140
cls.omc.sendExpression('setCommandLineOptions("--std=latest")')
@@ -76,15 +75,15 @@ def test_aixlib_tag_version(self):
7675

7776
# Load package.mo
7877
repopath = self.aixlib_path
79-
hits = [os.path.join(repopath, "AixLib", "package.mo")]
78+
hits = [str(repopath.joinpath("AixLib", "package.mo"))]
8079
self.omc.sendExpression('loadFile("%s", uses=false)' % hits[0])
8180

8281
libentry = new_libentry(
8382
libname=libname,
8483
tagName=tagName,
8584
entry=entry,
8685
hits=hits,
87-
repopath=repopath,
86+
repopath=str(repopath),
8887
omc=self.omc
8988
)
9089

@@ -128,7 +127,7 @@ def test_modelica_master_version(self):
128127

129128
# Load package.mo
130129
repopath = self.msl_path
131-
hits = [os.path.join(repopath, "Modelica", "package.mo")]
130+
hits = [str(repopath.joinpath("Modelica", "package.mo"))]
132131
self.omc.sendExpression('clear()')
133132
self.omc.sendExpression('loadFile("%s", uses=false)' % hits[0])
134133

@@ -137,7 +136,7 @@ def test_modelica_master_version(self):
137136
tagName=branch,
138137
entry=entry,
139138
hits=hits,
140-
repopath=repopath,
139+
repopath=str(repopath),
141140
omc=self.omc
142141
)
143142

tests/test_main.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
import unittest
22
from io import StringIO
3+
from pathlib import Path
34
import sys
45

56
from ompackagemanager import __main__
67

78

8-
class TestCLI(unittest.TestCase):
9-
def test_run_check_missing(self):
9+
class TestCaseBase(unittest.TestCase):
10+
def assertIsFile(self, path: Path):
11+
if not Path(path).resolve().is_file():
12+
raise AssertionError("File does not exist: %s" % str(path))
13+
14+
15+
class TestCLI(TestCaseBase):
16+
def setUp(self) -> None:
17+
"""Remove index.json"""
18+
self.index_file = Path(__file__).parents[1].joinpath("index.json")
19+
if self.index_file.is_file():
20+
self.index_file.unlink()
21+
22+
def tearDown(self):
23+
"""Remove index.json"""
24+
if self.index_file.is_file():
25+
self.index_file.unlink()
26+
27+
def test_run_check_missing(self) -> None:
1028
saved_stdout = sys.stdout
1129
try:
1230
out = StringIO()
@@ -27,11 +45,15 @@ def test_run_check_missing(self):
2745
modelica-3rdparty/ShipSIM
2846
modelica-3rdparty/SMEHV
2947
modelica-3rdparty/ThermoSysPro
30-
modelica-3rdparty/urdfmodelica
3148
''', out.getvalue())
3249
finally:
3350
sys.stdout = saved_stdout
3451

52+
def test_run_genindex(self) -> None:
53+
__main__.main(["genindex"])
54+
55+
self.assertIsFile(self.index_file)
56+
3557

3658
if __name__ == "__main__":
3759
unittest.main()

0 commit comments

Comments
 (0)