#!/usr/bin/env python

import pytest
import os
import sys
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

from pathlib import Path
from dselib.thread import initTLS
initTLS()

from constants import CONST
from dselib.path import normalizePath
from dselib.dir import GetDSEExtDirectory
from dselib.module import LoadModule, LoadModulePath
from dselib.module import LoadModulePath2, LoadModuleFromPackage


def test_1():
    # add tests for LoadModule
    mh = LoadModule('argparse')
    assert mh is not None
    assert hasattr(mh, 'ArgumentParser')

    mh = LoadModule('madeup_module_name')
    assert mh is None

    testmodule = 'dselib.env'
    if testmodule in sys.modules:
        del sys.modules[testmodule]
    assert testmodule not in sys.modules
    mh = LoadModule(testmodule)
    assert mh is not None
    assert testmodule in sys.modules
    assert hasattr(mh, 'GetDSEDebug')

def test_2():
    # add tests for LoadModulePath2

    tempModule = GetDSEExtDirectory() / 'dseutil/dsehelp'
    mh = LoadModulePath2(tempModule)
    assert mh is not None
    assert hasattr(mh, 'GenerateDocs')

    mh = LoadModulePath2(tempModule, removeIfLoaded=True)
    assert mh is not None
    assert hasattr(mh, 'GenerateDocs')

def test_3():
    # add tests for LoadModuleFromPackage
    testpackage = 'root.dselib'
    testmodulestem = 'env'
    testmodule = f'{testpackage}.{testmodulestem}'
    if testmodule in sys.modules:
        del sys.modules[testmodule]
    assert testmodule not in sys.modules
    mh = LoadModuleFromPackage(testpackage, testmodulestem)
    assert mh is not None
    assert testmodule in sys.modules
    assert hasattr(mh, 'GetDSEDebug')

# def _create_python_module(path, retVal=True):
#     logger.debug('writing python module to %s', path)
#     with open(path, 'w', encoding='utf-8') as fp:
#         fp.write(f'''def test{retVal}():
#         \treturn {retVal}
#         ''')

def test_4():
    # add tests for LoadModulePath
    tmpdir = normalizePath(Path('./tmod').resolve())
    tempModule = tmpdir / 'p42.py'
    logger.debug('tempModule=%s', tempModule)
    assert tempModule.is_file()
    mh = LoadModulePath(str(tempModule))
    assert mh is not None
    assert hasattr(mh, 'testTrue')
    assert mh.testTrue() is True
    assert str(tmpdir) in sys.path
    sys.path.remove(str(tmpdir))
    sys.modules.pop(str(tempModule.stem))   # not necessary, but so I'll remember this method

    tempModule2 = Path(tmpdir) / 'p42false.py'
    assert tempModule2.is_file()
    mh = LoadModulePath(tempModule2, remove=True)
    assert mh is not None
    assert str(tmpdir) not in sys.path
    assert hasattr(mh, 'testFalse')
    assert mh.testFalse() is False

if __name__ == '__main__':
    pytest.main(['-k', 'test_module.py'])

#!/usr/bin/env python

import pytest
import os
import sys
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

from pathlib import Path
from dselib.thread import initTLS
initTLS()

from constants import CONST
from dselib.path import normalizePath
from dselib.dir import GetDSEExtDirectory
from dselib.module import LoadModule, LoadModulePath
from dselib.module import LoadModulePath2, LoadModuleFromPackage


def test_1():
    # add tests for LoadModule
    mh = LoadModule('argparse')
    assert mh is not None
    assert hasattr(mh, 'ArgumentParser')

    mh = LoadModule('madeup_module_name')
    assert mh is None

    testmodule = 'dselib.env'
    if testmodule in sys.modules:
        del sys.modules[testmodule]
    assert testmodule not in sys.modules
    mh = LoadModule(testmodule)
    assert mh is not None
    assert testmodule in sys.modules
    assert hasattr(mh, 'GetDSEDebug')

def test_2():
    # add tests for LoadModulePath2

    tempModule = GetDSEExtDirectory() / 'dseutil/dsehelp'
    mh = LoadModulePath2(tempModule)
    assert mh is not None
    assert hasattr(mh, 'GenerateDocs')

    mh = LoadModulePath2(tempModule, removeIfLoaded=True)
    assert mh is not None
    assert hasattr(mh, 'GenerateDocs')

def test_3():
    # add tests for LoadModuleFromPackage
    testpackage = 'root.dselib'
    testmodulestem = 'env'
    testmodule = f'{testpackage}.{testmodulestem}'
    if testmodule in sys.modules:
        del sys.modules[testmodule]
    assert testmodule not in sys.modules
    mh = LoadModuleFromPackage(testpackage, testmodulestem)
    assert mh is not None
    assert testmodule in sys.modules
    assert hasattr(mh, 'GetDSEDebug')

# def _create_python_module(path, retVal=True):
#     logger.debug('writing python module to %s', path)
#     with open(path, 'w', encoding='utf-8') as fp:
#         fp.write(f'''def test{retVal}():
#         \treturn {retVal}
#         ''')

def test_4():
    # add tests for LoadModulePath
    tmpdir = normalizePath(Path('./tmod').resolve())
    tempModule = tmpdir / 'p42.py'
    logger.debug('tempModule=%s', tempModule)
    assert tempModule.is_file()
    mh = LoadModulePath(str(tempModule))
    assert mh is not None
    assert hasattr(mh, 'testTrue')
    assert mh.testTrue() is True
    assert str(tmpdir) in sys.path
    sys.path.remove(str(tmpdir))
    sys.modules.pop(str(tempModule.stem))   # not necessary, but so I'll remember this method

    tempModule2 = Path(tmpdir) / 'p42false.py'
    assert tempModule2.is_file()
    mh = LoadModulePath(tempModule2, remove=True)
    assert mh is not None
    assert str(tmpdir) not in sys.path
    assert hasattr(mh, 'testFalse')
    assert mh.testFalse() is False

if __name__ == '__main__':
    pytest.main(['-k', 'test_module.py'])

#!/usr/bin/env python # add tests for LoadModule # add tests for LoadModulePath2 # add tests for LoadModuleFromPackage # def _create_python_module(path, retVal=True): #     logger.debug('writing python module to %s', path) #     with open(path, 'w', encoding='utf-8') as fp: #         fp.write(f'''def test{retVal}(): #         \treturn {retVal} #         ''') # add tests for LoadModulePath # not necessary, but so I'll remember this method