예제 #1
0
    def student_file(self, filename):
        """
        Given a path to a student's file relative to the root of the student's
        submission, returns an absolute path to that file.

        """

        testables_directory = \
            self.sheep_data.get("testables_directory", "../submission/")

        path = os.path.join(testables_directory, filename)

        # Ensure that we return an absolute path.
        return _utils.resolve_path(path)
def check_compiles(files, flags=[], ignore_cache=False):
    """
    Attempts to compile some files.

    :param files: A list of paths to files to compile.
    :param flags: A list of command line arguments to supply to the compiler.
            Note that ``-o main`` will be added after your arguments.
    :param ignore_cache: If you ask Galah Interact to compile some files, it
            will cache the results. The next time you try to compile the same
            files, the executable that was cached will be used instead. Set
            this argument to ``True`` if you don't want the cache to be used.
    :returns: A ``TestResult`` object.

    .. code-block:: python

        >>> print interact.standardtests.check_compiles(["main.cpp", "foo.cpp"])
        Score: 0 out of 10

        This test ensures that your code compiles without errors. Your program was compiled with g++ -o main /tmp/main.cpp /tmp/foo.cpp.

        Your code did not compile. The compiler outputted the following errors:

        ```
        /tmp/main.cpp: In function 'int main()':
        /tmp/main.cpp:7:9: error: 'foo' was not declared in this scope
        /tmp/main.cpp:9:18: error: 'dothings' was not declared in this scope
        /tmp/main.cpp:11:19: error: 'dootherthings' was not declared in this scope

        ```

    """

    files = [_utils.resolve_path(i) for i in files]

    # Try to compile the program
    compiler_output, executable_path = execute.compile_program(files, flags=flags, ignore_cache=ignore_cache)

    command = pretty.craft_shell_command(execute.create_compile_command(files, flags))

    result = core.TestResult(
        brief="This test ensures that your code compiles without errors. "
        "Your program was compiled with %s." % (command,),
        default_message="**Great job!** Your code compiled cleanly without " "any problems.",
        max_score=10,
        bulleted_messages=False,
    )

    # If the compilation failed
    if executable_path is None:
        if compiler_output:
            compiler_output = pretty.truncate_string(compiler_output)

            result.add_message(
                "Your code did not compile. The compiler outputted the following "
                "errors:\n\n```\n{compiler_output}\n```\n",
                compiler_output=compiler_output,
                dscore=-10,
            )
        else:
            result.add_message("Your code did not compile but the compiler did not output " "any errors or warnings.")

        result.score = 0
    else:
        result.score = 10

    return result