Anki/anki-qt/tests/test_addons.py
Damien Elmes b23b6fbe35 move the separate components back into this monorepo
Earlier today I pushed a change that split this code up into multiple
repos, but that has proved to complicate things too much. So we're
back to a single repo, except the individual submodules are better
separated than they were before.

The README files need updating again; I will push them out soon.

Aside from splitting out the different modules, the sound code has
moved from from anki to aqt.
2020-01-02 19:43:19 +10:00

67 lines
1.7 KiB
Python

import os.path
from tempfile import TemporaryDirectory
from zipfile import ZipFile
from mock import MagicMock
from nose2.tools.such import helper
from aqt.addons import AddonManager
def test_readMinimalManifest():
assertReadManifest(
'{"package": "yes", "name": "no"}', {"package": "yes", "name": "no"}
)
def test_readExtraKeys():
assertReadManifest(
'{"package": "a", "name": "b", "mod": 3, "conflicts": ["d", "e"]}',
{"package": "a", "name": "b", "mod": 3, "conflicts": ["d", "e"]},
)
def test_invalidManifest():
assertReadManifest('{"one": 1}', {})
def test_mustHaveName():
assertReadManifest('{"package": "something"}', {})
def test_mustHavePackage():
assertReadManifest('{"name": "something"}', {})
def test_invalidJson():
assertReadManifest("this is not a JSON dictionary", {})
def test_missingManifest():
assertReadManifest(
'{"package": "what", "name": "ever"}', {}, nameInZip="not-manifest.bin"
)
def test_ignoreExtraKeys():
assertReadManifest(
'{"package": "a", "name": "b", "game": "c"}', {"package": "a", "name": "b"}
)
def test_conflictsMustBeStrings():
assertReadManifest(
'{"package": "a", "name": "b", "conflicts": ["c", 4, {"d": "e"}]}', {}
)
def assertReadManifest(contents, expectedManifest, nameInZip="manifest.json"):
with TemporaryDirectory() as td:
zfn = os.path.join(td, "addon.zip")
with ZipFile(zfn, "w") as zfile:
zfile.writestr(nameInZip, contents)
adm = AddonManager(MagicMock())
with ZipFile(zfn, "r") as zfile:
helper.assertEquals(adm.readManifestFile(zfile), expectedManifest)