How to use the check50.internal.register function in check50

To help you get started, we’ve selected a few check50 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cs50 / check50 / tests / c_tests.py View on Github external
def test_no_leak(self):
        with open("foo.c", "w") as f:
            src = 'int main() {}'
            f.write(src)

        check50.c.compile("foo.c")
        with check50.internal.register:
            check50.c.valgrind("./foo").exit()
github cs50 / check50 / tests / internal_tests.py View on Github external
def test_after_every(self):
        l = []
        check50.internal.register.after_every(lambda : l.append("foo"))

        with check50.internal.register:
            self.assertEqual(l, [])

        self.assertEqual(l, ["foo"])

        with check50.internal.register:
            self.assertEqual(l, ["foo"])

        self.assertEqual(l, ["foo", "foo"])
github cs50 / check50 / tests / internal_tests.py View on Github external
def test_before_every(self):
        l = []
        check50.internal.register.before_every(lambda : l.append("foo"))

        with check50.internal.register:
            self.assertEqual(l, ["foo"])

        with check50.internal.register:
            self.assertEqual(l, ["foo", "foo"])

        self.assertEqual(l, ["foo", "foo"])
github cs50 / check50 / tests / internal_tests.py View on Github external
def test_after_every(self):
        l = []
        check50.internal.register.after_every(lambda : l.append("foo"))

        with check50.internal.register:
            self.assertEqual(l, [])

        self.assertEqual(l, ["foo"])

        with check50.internal.register:
            self.assertEqual(l, ["foo"])

        self.assertEqual(l, ["foo", "foo"])
github cs50 / check50 / tests / internal_tests.py View on Github external
def test_after_check(self):
        check50.internal.check_running = True
        l = []
        check50.internal.register.after_check(lambda : l.append("foo"))

        with check50.internal.register:
            self.assertEqual(l, [])

        self.assertEqual(l, ["foo"])

        with check50.internal.register:
            self.assertEqual(l, ["foo"])

        self.assertEqual(l, ["foo"])
        check50.internal.check_running = False
github cs50 / check50 / check50 / c.py View on Github external
This function works exactly like :func:`check50.run`, with the additional effect that ``command`` is run through
    ``valgrind`` and ``valgrind``'s output is automatically reviewed at the end of the check for memory leaks and other
    bugs. If ``valgrind`` reports any issues, the check is failed and student-friendly messages are printed to the log.

    Example usage::

        check50.c.valgrind("./leaky").stdin("foo").stdout("bar").exit(0)

    .. note::
        It is recommended that the student's code is compiled with the `-ggdb`
        flag so that additional information, such as the file and line number at which
        the issue was detected can be included in the log as well.
    """
    xml_file = tempfile.NamedTemporaryFile()
    internal.register.after_check(lambda: _check_valgrind(xml_file))

    # Ideally we'd like for this whole command not to be logged.
    return run(f"valgrind --show-leak-kinds=all --xml=yes --xml-file={xml_file.name} -- {command}", env=env)
github cs50 / check50 / check50 / runner.py View on Github external
def wrapper(checks_root, dependency_state):
            # Result template
            result = CheckResult.from_check(check)
            # Any shared (returned) state
            state = None

            try:
                # Setup check environment, copying disk state from dependency
                internal.run_dir = checks_root / check.__name__
                src_dir = checks_root / (dependency.__name__ if dependency else "-")
                shutil.copytree(src_dir, internal.run_dir)
                os.chdir(internal.run_dir)

                # Run registered functions before/after running check and set timeout
                with internal.register, _timeout(seconds=timeout):
                    args = (dependency_state,) if inspect.getfullargspec(check).args else ()
                    state = check(*args)
            except Failure as e:
                result.passed = False
                result.cause = e.payload
            except BaseException as e:
                result.passed = None
                result.cause = {"rationale": _("check50 ran into an error while running checks!"),
                                "error": {
                                    "type": type(e).__name__,
                                    "value": str(e),
                                    "traceback": traceback.format_tb(e.__traceback__),
                                    "data" : e.payload if hasattr(e, "payload") else {}
                                }}
            else:
                result.passed = True
github cs50 / check50 / check50 / _api.py View on Github external
def log(line):
    """
    Add to check log

    :param line: line to be added to the check log
    :type line: str

    The check log is student-visible via the ``--log`` flag to ``check50``.
    """
    _log.append(line.replace("\n", "\\n"))


_data = {}
internal.register.before_every(_data.clear)


def data(**kwargs):
    """
    Add data to the check payload

    :params kwargs: key/value mappings to be added to the check payload

    Example usage::

        check50.data(time=7.3, mem=23)

    """

    _data.update(kwargs)
github cs50 / check50 / check50 / _api.py View on Github external
import hashlib
import functools
import os
import shlex
import shutil
import signal
import sys
import time

import pexpect
from pexpect.exceptions import EOF, TIMEOUT

from . import internal

_log = []
internal.register.before_every(_log.clear)


def log(line):
    """
    Add to check log

    :param line: line to be added to the check log
    :type line: str

    The check log is student-visible via the ``--log`` flag to ``check50``.
    """
    _log.append(line.replace("\n", "\\n"))


_data = {}
internal.register.before_every(_data.clear)