From 114f4bb53b2ed8e2309352efa01068c4ca58f1a5 Mon Sep 17 00:00:00 2001 From: Juan Mauricio Matera Date: Tue, 13 Aug 2024 07:55:04 -0300 Subject: [PATCH] Using tmp as pwd in pytests (#1068) As General Peron said, "Better than saying is doing". This is what I was talking about in #1067. Also, I improved some docstrings a little bit. --------- Co-authored-by: rocky --- test/builtin/files_io/test_files.py | 2 ++ test/helper.py | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/test/builtin/files_io/test_files.py b/test/builtin/files_io/test_files.py index f883abc18..bad16b4c7 100644 --- a/test/builtin/files_io/test_files.py +++ b/test/builtin/files_io/test_files.py @@ -102,6 +102,8 @@ def test_close(): @pytest.mark.parametrize( ("str_expr", "msgs", "str_expected", "fail_msg"), [ + (None, None, None, None), # Reset the session and set the working + # directory as the temporary directory ('Close["abc"]', ("abc is not open.",), "Close[abc]", ""), ( "exp = Sin[1]; FilePrint[exp]", diff --git a/test/helper.py b/test/helper.py index cff356df5..e42a844f6 100644 --- a/test/helper.py +++ b/test/helper.py @@ -14,8 +14,12 @@ def reset_session(add_builtin=True, catch_interrupt=False): + """ + reset the session cleaning all the definitions. + """ global session session.reset() + session.evaluate("SetDirectory[$TemporaryDirectory];") def evaluate_value(str_expr: str): @@ -38,21 +42,30 @@ def check_evaluation( ): """ Helper function to test Mathics expression against - its results + its results. Compares the expressions represented by ``str_expr`` and ``str_expected`` by evaluating the first, and optionally, the second. If omitted, `str_expected` is assumed to be `"Null"`. - to_string_expr: If ``True`` (default value) the result of the evaluation is - converted into a Python string. Otherwise, the expression is kept - as an Expression object. - If this argument is set to ``None``, the session is reset. + str_expr: The expression to be tested. If its value is ``None``, the session is + reset. + At the beginning of each set of pytests, it is important to call + ``check_evaluation(None)`` to avoid that definitions introduced by + other tests affect the results. + + str_expected: The expected result. The value ``None`` is equivalent to ``"Null"``. failure_message: message shown in case of failure. Use "" for no failure message. + hold_expected: If ``False`` (default value) the ``str_expected`` is evaluated. Otherwise, the expression is considered literally. + to_string_expr: If ``True`` (default value) the result of the evaluation is + converted into a Python string. Otherwise, the expression is kept + as an Expression object. + If this argument is set to ``None``, the session is reset. + to_string_expected: If ``True`` (default value) the expected expression is evaluated and then converted to a Python string. result of the evaluation is converted into a Python string.