gradema.grader.console._console

Module Contents

Classes

ConsoleTestReporter

A TestReporter is passed to all tests. This exposes a public API for tests to use and report what they are doing.

ConsoleGraderReporter

A GraderReporter provides functionality to report on things occurring during the grading process.

Functions

has_vim_installed(→ bool)

vim_diff(→ None)

Creates a diff using vim.

html_diff(→ None)

Attributes

gradema.grader.console._console.TEST_DIRECTORY
gradema.grader.console._console.has_vim_installed() bool
gradema.grader.console._console.vim_diff(goal_file: pathlib.Path, output_file: pathlib.Path, html_output: pathlib.Path) None

Creates a diff using vim. Note that calling this function takes a while to complete.

Parameters:
  • goal_file

  • output_file

  • html_output

gradema.grader.console._console.html_diff(goal_path: pathlib.Path, actual_output_path: pathlib.Path, html_output_path: pathlib.Path) None
class gradema.grader.console._console.ConsoleTestReporter

Bases: gradema.test.TestReporter

A TestReporter is passed to all tests. This exposes a public API for tests to use and report what they are doing. A TestReporter’s implementation may log what the test is doing to the console, or even perform an action depending on how the TestReporter is configured.

reporter: ConsoleGraderReporter
log(message: str) None

Log an informative message to the user.

Parameters:

message – The message to log. No formatting is support

log_unexpected_exception(exception: BaseException) None

Log an unexpected exception

Parameters:

exception – The exception to log

resolve_command(command: Sequence[gradema.test.argument.Argument], test_identifier: str) tuple[Sequence[str], Mapping[tuple[int, gradema.test.argument.Argument], gradema.test.argument.ResolvedArgumentInfo]]

Resolves a list of Arguments by returning a list of strings.

The returned value contains the resolved command and an argument info dictionary. The key of the argument info dictionary is a tuple of (index, Argument) where index is the index of the Argument in the command parameter.

If you are wondering why command’s type is Sequence[Argument] rather than a list[Argument], it is because the Sequence type is covariant. That means that you can assign a Sequence[str] to a Sequence[Argument], which is necessary to make sure mypy type checking is happy, and correctly documents how the command argument is used: it is not modified. More information here: https://mypy.readthedocs.io/en/stable/common_issues.html#variance

Parameters:
  • test_identifier – The identifier of the test being run

  • command – The command to resolve

Returns:

A tuple of (resolved command, argument info dictionary)

Raises:

ValueError – A ValueError may be risen if the parameters supplied to this function do not give enough information to resolve a particular Argument. This exception is currently not risen in any known implementation, but it’s possible that optional parameters will be added in the future that aid in resolving an Argument that is not created as of writing this docstring

report_command(command: Sequence[str]) gradema.test.CommandData

Reports that the given command WILL be run

Parameters:

command – The command that will be run

Returns:

A CommandData object containing parameters that determine where the stdout and stdin of the command should go.

report_stdio_command(command: Sequence[str], test_identifier: str, input_file: pathlib.Path | None, stdout_as_output: bool) gradema.test.StdioCommandData

Reports that a stdio test command WILL be run.

This is similar to report_command, but it is expected that the caller will run the command and direct its output to that specified by the returned object.

Parameters:
  • command – The base command that is being run (not including any I/O redirection)

  • test_identifier – An identifier for the test. This is not recommended to contain spaces or upper case characters

  • input_file – The input file that will be directed into the command’s stdin, or None if no specific input file is being used.

  • stdout_as_output – True to use the command’s stdout as an output, False to not

Returns:

A StdioCommandData that contains the file to direct stdout to and the TextIO to direct stderr to.

maybe_launch_debugger(command: Sequence[str]) None

Calling this function will launch the debugger by running the given command only if we are in debugging mode. The implementation will likely prompt the user to make sure they want to launch the debugger

Parameters:

command – The command to launch the debugger

report_diff_result(test_identifier: str, goal_file: pathlib.Path, output_file: pathlib.Path, similarity: float, fuzzy: bool) None

Reports the result of a diff and possibly opens a diff viewer depending on the configuration of this reporter.

Parameters:
  • test_identifier – The identifier for the test

  • goal_file – The goal file

  • output_file – The output file that is supposed to contain the same contents of the goal file

  • similarity – A number from 0 to 1 representing the similarity (1 is exactly the same). Non-fuzzy diffs should only pass 0 or 1.

  • fuzzy – True if fuzzy, false otherwise

report_file_exists_test(file: pathlib.Path, expected: str) None

Reports that a file existence test WILL BE performed.

Parameters:
  • file

  • expected

Returns:

report_file_exists_test_result(file: pathlib.Path, expected: str, is_correct: bool, actual: str | None) None

Reports that a file existence test has been performed and that the result is this.

Parameters:
  • file – The file being tested for its existence

  • expected – The expected type of the file (basically the output of file <file>

  • is_correct – True if the actual matches the expected

  • actual – The actual type of the file, or None if the file does not exist

Returns:

class gradema.grader.console._console.ConsoleGraderReporter

Bases: gradema.grader.GraderReporter

A GraderReporter provides functionality to report on things occurring during the grading process.

property test_reporter: gradema.test.TestReporter
section: gradema.section.Section
console: rich.console.Console
stdout: TextIO
stderr: TextIO
interactive: bool
debug_mode: bool
depth: int
report_start(max_points: int) None
report_test_result(result: gradema.test.TestResult, points: int, max_points: int) None
subsection(section: gradema.section.Section) gradema.grader.GraderReporter

Creates a similar GraderReporter containing the passed subsection and updated attributes to correctly report on a subsection.

Parameters:

section – A subsection of the current section being reported on.

Returns:

The modified GraderReporter