Browse Source

Py3: Test and code-format apireg

pull/1253/head
Safihre 6 years ago
parent
commit
a92f94d560
  1. 3
      .travis.yml
  2. 2
      appveyor.yml
  3. 22
      tests/test_win_utils.py
  4. 54
      util/apireg.py

3
.travis.yml

@ -42,11 +42,12 @@ install:
- python3 -m pip install --upgrade -r tests/requirements.txt - python3 -m pip install --upgrade -r tests/requirements.txt
script: script:
- python3 -m pytest - python3 -m pytest -v
# Force code-style for folders that we already did, only on 1 job # Force code-style for folders that we already did, only on 1 job
- if [[ $TRAVIS_PYTHON_VERSION == "3.7" ]]; then python3 -m black tests -l120 --py36 -N --check; fi - if [[ $TRAVIS_PYTHON_VERSION == "3.7" ]]; then python3 -m black tests -l120 --py36 -N --check; fi
- if [[ $TRAVIS_PYTHON_VERSION == "3.7" ]]; then python3 -m black tools -l120 --py36 -N --check; fi - if [[ $TRAVIS_PYTHON_VERSION == "3.7" ]]; then python3 -m black tools -l120 --py36 -N --check; fi
- if [[ $TRAVIS_PYTHON_VERSION == "3.7" ]]; then python3 -m black util -l120 --py36 -N --check; fi
- if [[ $TRAVIS_PYTHON_VERSION == "3.7" ]]; then python3 -m black sabnzbd/utils -l120 --py36 -N --check; fi - if [[ $TRAVIS_PYTHON_VERSION == "3.7" ]]; then python3 -m black sabnzbd/utils -l120 --py36 -N --check; fi
notifications: notifications:

2
appveyor.yml

@ -20,4 +20,4 @@ install:
- python -m pip install --upgrade -r tests/requirements.txt - python -m pip install --upgrade -r tests/requirements.txt
build_script: build_script:
- python -m pytest - python -m pytest -v

22
tests/test_mailslot.py → tests/test_win_utils.py

@ -27,6 +27,8 @@ import pytest
if not sys.platform.startswith("win"): if not sys.platform.startswith("win"):
pytest.skip("Skipping Windows-only tests", allow_module_level=True) pytest.skip("Skipping Windows-only tests", allow_module_level=True)
import util.apireg as ar
class TestMailslot: class TestMailslot:
def test_mailslot_basic(self): def test_mailslot_basic(self):
@ -49,3 +51,23 @@ class TestMailslot:
# Client outputs nothing # Client outputs nothing
assert not client_p.stdout.readlines() assert not client_p.stdout.readlines()
class TestAPIReg:
def test_set_get_connection_info_user(self):
""" Test the saving of the URL in USER-registery
We can't test the SYSTEM one.
"""
test_url = "sab_test:8080"
ar.set_connection_info(test_url, True)
assert ar.get_connection_info(True) == test_url
assert not ar.get_connection_info(False)
# Remove and check if gone
ar.del_connection_info(True)
assert not ar.get_connection_info(True)
def test_get_install_lng(self):
""" Not much to test yet.. """
assert ar.get_install_lng() == "en"

54
util/apireg.py

@ -44,10 +44,10 @@ def get_connection_info(user=True):
try: try:
hive = winreg.ConnectRegistry(None, section) hive = winreg.ConnectRegistry(None, section)
key = winreg.OpenKey(hive, keypath + r'\api') key = winreg.OpenKey(hive, keypath + r"\api")
for i in range(0, winreg.QueryInfoKey(key)[1]): for i in range(0, winreg.QueryInfoKey(key)[1]):
name, value, val_type = winreg.EnumValue(key, i) name, value, val_type = winreg.EnumValue(key, i)
if name == 'url': if name == "url":
url = value url = value
winreg.CloseKey(key) winreg.CloseKey(key)
@ -70,11 +70,11 @@ def set_connection_info(url, user=True):
hive = winreg.ConnectRegistry(None, section) hive = winreg.ConnectRegistry(None, section)
try: try:
winreg.CreateKey(hive, keypath) winreg.CreateKey(hive, keypath)
except: except OSError:
pass pass
key = winreg.OpenKey(hive, keypath) key = winreg.OpenKey(hive, keypath)
mykey = winreg.CreateKey(key, 'api') mykey = winreg.CreateKey(key, "api")
winreg.SetValueEx(mykey, 'url', None, winreg.REG_SZ, url) winreg.SetValueEx(mykey, "url", None, winreg.REG_SZ, url)
winreg.CloseKey(mykey) winreg.CloseKey(mykey)
winreg.CloseKey(key) winreg.CloseKey(key)
except WindowsError: except WindowsError:
@ -90,7 +90,7 @@ def del_connection_info(user=True):
try: try:
hive = winreg.ConnectRegistry(None, section) hive = winreg.ConnectRegistry(None, section)
key = winreg.OpenKey(hive, keypath) key = winreg.OpenKey(hive, keypath)
winreg.DeleteKey(key, 'api') winreg.DeleteKey(key, "api")
winreg.CloseKey(key) winreg.CloseKey(key)
except WindowsError: except WindowsError:
if user: if user:
@ -107,7 +107,7 @@ def get_install_lng():
key = winreg.OpenKey(hive, r"Software\SABnzbd") key = winreg.OpenKey(hive, r"Software\SABnzbd")
for i in range(0, winreg.QueryInfoKey(key)[1]): for i in range(0, winreg.QueryInfoKey(key)[1]):
name, value, val_type = winreg.EnumValue(key, i) name, value, val_type = winreg.EnumValue(key, i)
if name == 'Installer Language': if name == "Installer Language":
lng = value lng = value
winreg.CloseKey(key) winreg.CloseKey(key)
except WindowsError: except WindowsError:
@ -117,32 +117,32 @@ def get_install_lng():
if lng in LanguageMap: if lng in LanguageMap:
return LanguageMap[lng] return LanguageMap[lng]
return 'en' return "en"
# Map from NSIS-codepage to our language-strings # Map from NSIS-codepage to our language-strings
LanguageMap = { LanguageMap = {
'1033': 'en', "1033": "en",
'1036': 'fr', "1036": "fr",
'1031': 'de', "1031": "de",
'1043': 'nl', "1043": "nl",
'1035': 'fi', "1035": "fi",
'1045': 'pl', "1045": "pl",
'1053': 'sv', "1053": "sv",
'1030': 'da', "1030": "da",
'2068': 'nb', "2068": "nb",
'1048': 'ro', "1048": "ro",
'1034': 'es', "1034": "es",
'1046': 'pr_BR', "1046": "pr_BR",
'3098': 'sr', "3098": "sr",
'1037': 'he', "1037": "he",
'1049': 'ru', "1049": "ru",
'2052': 'zh_CN' "2052": "zh_CN",
} }
if __name__ == '__main__': if __name__ == "__main__":
print(('URL = %s' % get_connection_info())) print("URL = %s" % get_connection_info())
print(('Language = %s' % get_install_lng())) print("Language = %s" % get_install_lng())
# del_connection_info() # del_connection_info()
# set_connection_info('localhost', '8080', 'blabla', user=False) # set_connection_info('localhost', '8080', 'blabla', user=False)

Loading…
Cancel
Save