Skip to content

Commit

Permalink
add decorator for forcing current direc workingory during tests (mood…
Browse files Browse the repository at this point in the history
…le2amc)
  • Loading branch information
luclaurent committed Oct 9, 2024
1 parent e9fa873 commit 97f4edf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"python.testing.unittestArgs": [
"-v",
"-s",
"./amc2moodle/tests",
"-p",
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
2 changes: 1 addition & 1 deletion amc2moodle/tests/test_amc2moodle.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
__OUTPUT_TEST_DIR__ = os.path.abspath(os.path.join(os.getcwd(), 'output_tests'))
# create output directory and switch working dir on it
os.makedirs(__OUTPUT_TEST_DIR__, exist_ok=True)
os.chdir(__OUTPUT_TEST_DIR__)
# os.chdir(__OUTPUT_TEST_DIR__)

# Silence other loggers
# for log_name, log_obj in logging.Logger.manager.loggerDict.items():
Expand Down
13 changes: 8 additions & 5 deletions amc2moodle/tests/test_moodle2amc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from amc2moodle.utils.customLogging import customLogger
from amc2moodle.utils.misc import check_hash
from amc2moodle.utils.misc import check_hash, for_all_methods, decorator_set_cwd
from amc2moodle.moodle2amc import Quiz
import amc2moodle as amdlpkg
import os
Expand All @@ -42,15 +42,14 @@
__OUTPUT_TEST_DIR__ = os.path.abspath(os.path.join(os.getcwd(), 'output_tests'))
# create output directory and switch working dir on it
os.makedirs(__OUTPUT_TEST_DIR__, exist_ok=True)
os.chdir(__OUTPUT_TEST_DIR__)
# os.chdir(__OUTPUT_TEST_DIR__)


# Silence other loggers
# for log_name, log_obj in logging.Logger.manager.loggerDict.items():
# if "amc2moodle" not in log_name and "tests_a2m" not in log_name:
# log_obj.disabled = True


class TestSuite(unittest.TestCase):
""" Define test cases for unittest.
Expand All @@ -59,7 +58,7 @@ class TestSuite(unittest.TestCase):
teacher / sandbox
"""

@decorator_set_cwd(__OUTPUT_TEST_DIR__)
def test_mdl_bank(self):
""" Tests if input XML file yields reference LaTeX file.
"""
Expand All @@ -74,7 +73,8 @@ def test_mdl_bank(self):
# move sty's file to output dir
src = os.path.abspath(os.path.join(__PAYLOAD_TEST_DIR__,
"automultiplechoice.sty"))
dst = os.path.abspath(os.path.join(__OUTPUT_TEST_DIR__,'automultiplechoice.sty'))
dst = os.path.abspath(os.path.join(__OUTPUT_TEST_DIR__,
"automultiplechoice.sty"))
shutil.copyfile(src, dst)

Logger.info('============ Run test conversion ============')
Expand All @@ -90,10 +90,13 @@ def test_mdl_bank(self):
Logger.info('> Converted XML is identical to the reference: OK')
# test latex compilation
status = quiz.compileLatex(fileOut)
Logger.info('cwd {}'.format(os.getcwd()))
if status.returncode != 0:
Logger.info('> pdflatex encounters Errors, see logs...')
else:
Logger.info('> pdflatex compile without Errors: OK')

Logger.info('cwd {}'.format(os.getcwd()))
self.assertEqual(status.returncode, 0)


Expand Down
51 changes: 50 additions & 1 deletion amc2moodle/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"""

import hashlib
import inspect
import os

def check_hash(file1, file2):
""" Return the md5 sum after removing all withspace.
Expand Down Expand Up @@ -53,4 +55,51 @@ def check_hash(file1, file2):
h1 = hashlib.md5(content1.encode()).hexdigest()
h2 = hashlib.md5(content2.encode()).hexdigest()
equiv = h1 == h2
return equiv
return equiv


def for_all_methods(decorator):
""" Decorate to class to apply a decorator on all methods.
Parameters
----------
decorator : function
Decorator function.
Returns
-------
cls : class
return the decorated class.
"""
def decorate(cls):
for name, fn in inspect.getmembers(cls, inspect.ismethod):
setattr(cls, name, decorator(fn))
return cls
return decorate

def decorator_set_cwd(directory):
""" Set and unset the current working directory.
Parameters
----------
directory : path
Directory to use as current working directory.
Returns
-------
func : function
return the decorated function.
"""
def decorator(function):
def wrapper(*args, **kwargs):
# initial cwd
init_cwd = os.getcwd()
# set cwd
os.chdir(directory)
# run function
result = function(*args, **kwargs)
# restore cwd
os.chdir(init_cwd)
return result
return wrapper
return decorator

0 comments on commit 97f4edf

Please sign in to comment.