|
| 1 | +import unittest |
| 2 | +import json |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +import shutil |
| 6 | +import pygit2 |
| 7 | +import OMPython |
| 8 | + |
| 9 | +from ompackagemanager.updateinfo import new_libentry, getgitrepo |
| 10 | + |
| 11 | + |
| 12 | +class TestNewLibentry(unittest.TestCase): |
| 13 | + @classmethod |
| 14 | + def setUpClass(cls): |
| 15 | + """Clone git repositories, start OMC session.""" |
| 16 | + |
| 17 | + def checkout_repo(github_repo: str, refname: str) -> str: |
| 18 | + """Clone and checkout a git repository from GitHub""" |
| 19 | + giturl = "https://github.com/%s.git" % github_repo |
| 20 | + repopath = os.path.join(cls.cache_dir, github_repo.split("/")[-1]) |
| 21 | + |
| 22 | + if not os.path.exists(repopath): |
| 23 | + pygit2.clone_repository(giturl, repopath) |
| 24 | + gitrepo = pygit2.Repository(repopath) |
| 25 | + |
| 26 | + if refname.startswith("refs/"): |
| 27 | + gitrepo.checkout( |
| 28 | + gitrepo.lookup_reference(refname), |
| 29 | + strategy=pygit2.GIT_CHECKOUT_FORCE | pygit2.GIT_CHECKOUT_RECREATE_MISSING) |
| 30 | + else: |
| 31 | + gitrepo.checkout_tree( |
| 32 | + gitrepo.get(refname), |
| 33 | + strategy=pygit2.GIT_CHECKOUT_FORCE | pygit2.GIT_CHECKOUT_RECREATE_MISSING) |
| 34 | + |
| 35 | + return os.path.normpath(os.path.join(gitrepo.path, os.pardir)) |
| 36 | + |
| 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) |
| 39 | + |
| 40 | + cls.omc = OMPython.OMCSessionZMQ() |
| 41 | + cls.omc.sendExpression('setCommandLineOptions("--std=latest")') |
| 42 | + |
| 43 | + cls.aixlib_path = checkout_repo("RWTH-EBC/AixLib", "refs/tags/v2.1.1") |
| 44 | + cls.msl_path = checkout_repo("modelica/ModelicaStandardLibrary", "478538e43d3c9f682b2a6fb667783e2b3760f9f3") |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def tearDownClass(cls): |
| 48 | + """Remove git repositories, stop OMC session.""" |
| 49 | + cls.omc.sendExpression("exit") |
| 50 | + shutil.rmtree(cls.cache_dir) |
| 51 | + |
| 52 | + def tearDown(self): |
| 53 | + """Clear omc session""" |
| 54 | + self.omc.sendExpression('clear()') |
| 55 | + |
| 56 | + def test_aixlib_tag_version(self): |
| 57 | + """Test libentry for AixLib v2.1.1""" |
| 58 | + |
| 59 | + libname = "AixLib" |
| 60 | + tagName = "v2.1.1" |
| 61 | + entry = json.loads( |
| 62 | + """ |
| 63 | + { |
| 64 | + "names": ["AixLib"], |
| 65 | + "github": "RWTH-EBC/AixLib", |
| 66 | + "branches": {"development": "development", "main": "main"}, |
| 67 | + "ignore-tags": ["v0.7.2", "v0.7.1", "v0.7.0", "v0.1.0"], |
| 68 | + "support": [ |
| 69 | + ["prerelease", "noSupport"], |
| 70 | + [">=1.3.1", "support"], |
| 71 | + [">=0.9.1", "experimental"], |
| 72 | + ["*", "obsolete"] |
| 73 | + ] |
| 74 | + } |
| 75 | + """) |
| 76 | + |
| 77 | + # Load package.mo |
| 78 | + repopath = self.aixlib_path |
| 79 | + hits = [os.path.join(repopath, "AixLib", "package.mo")] |
| 80 | + self.omc.sendExpression('loadFile("%s", uses=false)' % hits[0]) |
| 81 | + |
| 82 | + libentry = new_libentry( |
| 83 | + libname=libname, |
| 84 | + tagName=tagName, |
| 85 | + entry=entry, |
| 86 | + hits=hits, |
| 87 | + repopath=repopath, |
| 88 | + omc=self.omc |
| 89 | + ) |
| 90 | + |
| 91 | + expected_libentry = { |
| 92 | + "version": "2.1.1", |
| 93 | + "path": "AixLib", |
| 94 | + "uses": { |
| 95 | + "Modelica": "4.0.0", |
| 96 | + "Modelica_DeviceDrivers": "2.1.1", |
| 97 | + "SDF": "0.4.2" |
| 98 | + }, |
| 99 | + "convertFromVersion": [ |
| 100 | + "2.1.0" |
| 101 | + ], |
| 102 | + } |
| 103 | + self.assertDictEqual(libentry, expected_libentry) |
| 104 | + |
| 105 | + def test_modelica_master_version(self): |
| 106 | + """Test libentry for Modelica master""" |
| 107 | + |
| 108 | + libname = "Modelica" |
| 109 | + branch = "master" |
| 110 | + entry = json.loads( |
| 111 | + """ |
| 112 | + { |
| 113 | + "names": ["Modelica","ModelicaReference","ModelicaServices","ModelicaTest","ModelicaTestOverdetermined","Complex","ObsoleteModelica3","ObsoleteModelica4"], |
| 114 | + "github": "modelica/ModelicaStandardLibrary", |
| 115 | + "branches": { |
| 116 | + "master": "master" |
| 117 | + }, |
| 118 | + "standard": [ |
| 119 | + [">=3.4.0", "latest"], |
| 120 | + ["*", "latest"] |
| 121 | + ], |
| 122 | + "support": [ |
| 123 | + [">=3.2.3", "fullSupport"], |
| 124 | + ["*", "obsolete"] |
| 125 | + ] |
| 126 | + } |
| 127 | + """) |
| 128 | + |
| 129 | + # Load package.mo |
| 130 | + repopath = self.msl_path |
| 131 | + hits = [os.path.join(repopath, "Modelica", "package.mo")] |
| 132 | + self.omc.sendExpression('clear()') |
| 133 | + self.omc.sendExpression('loadFile("%s", uses=false)' % hits[0]) |
| 134 | + |
| 135 | + libentry = new_libentry( |
| 136 | + libname=libname, |
| 137 | + tagName=branch, |
| 138 | + entry=entry, |
| 139 | + hits=hits, |
| 140 | + repopath=repopath, |
| 141 | + omc=self.omc |
| 142 | + ) |
| 143 | + |
| 144 | + expected_libentry = { |
| 145 | + "version": "4.2.0-dev", |
| 146 | + "path": "Modelica", |
| 147 | + "uses": { |
| 148 | + "Complex": "4.2.0-dev", |
| 149 | + "ModelicaServices": "4.2.0-dev" |
| 150 | + }, |
| 151 | + "provides": [ |
| 152 | + "4.0.0", |
| 153 | + "4.1.0" |
| 154 | + ], |
| 155 | + "convertFromVersion": [ |
| 156 | + "3.0.0", |
| 157 | + "3.0.1", |
| 158 | + "3.1.0", |
| 159 | + "3.2.0", |
| 160 | + "3.2.1", |
| 161 | + "3.2.2", |
| 162 | + "3.2.3" |
| 163 | + ] |
| 164 | + } |
| 165 | + self.assertDictEqual(libentry, expected_libentry) |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + unittest.main() |
0 commit comments