aboutsummaryrefslogtreecommitdiff
path: root/tests/test_functions.py
blob: d71f616457d94558b43d85a62c30af1369d6cf3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
from tempfile import TemporaryDirectory
import pytest

from unixreg.constants import *
from unixreg.functions import *


_temp_dir = TemporaryDirectory()
os.environ["XDG_CONFIG_HOME"] = os.path.join(_temp_dir.name, "unixreg")

def test_ConnectRegistry():
    key = ConnectRegistry(None, HKEY_CURRENT_USER)
    key.Close()

    with pytest.raises(OSError):
        key = ConnectRegistry("FakeComputer", HKEY_CURRENT_USER)
        key.Close()

def test_CreateKey():
    def _create(key, subkey):
        with CreateKey(key, subkey) as key:
            CloseKey(key)

    _create(HKEY_CURRENT_USER, None)
    _create(HKEY_CURRENT_USER, "str")
    _create(HKEY_CURRENT_USER, RegKey("RegKey"))

    with pytest.raises(TypeError):
        _create(HKEY_CURRENT_USER, 1)
        _create(HKEY_CURRENT_USER, [])
        _create(HKEY_CURRENT_USER, ())
        _create(HKEY_CURRENT_USER, {})

def test_QueryValue():
    value = "value"
    value_name = "value_name"

    print(__import__("unixreg").HKEY_CURRENT_USER,)
    key = CreateKey(HKEY_CURRENT_USER, None)
    SetValue(key, value_name, REG_SZ, value)
    assert QueryValue(key, value_name) == value


def test_ExpandEnvironmentStrings():
    _test_str = "testvar"
    os.environ["TEST"] = _test_str
    os.environ["HOME"] = _test_str

    assert ExpandEnvironmentStrings(r"%TEST%") == os.environ["TEST"]
    assert ExpandEnvironmentStrings(r"%USERPROFILE%") == os.environ["HOME"]