Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/content/docs/instructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ To set up the assignment on Gradescope:
Then, upload `dist/autograder/hw.py` as a test submission to check the autograder.
It should get full marks.

In the results, each question gets one row.
The row shows each public test with its message, but only the number of hidden tests that passed.
Students see their total, each question's score, and a "Public tests" row listing each public test with its failure message.
Each question also gets a row, visible only to you, that lists all its tests and their messages, hidden ones included.
A question's points are split evenly across its tests.

## Security
Expand Down
85 changes: 66 additions & 19 deletions src/mogs/grader/results.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Find the uploaded files and turn test outcomes into Gradescope's `results.json`.

Students see everything this module renders. Hidden tests show up only as a count, and text a
student controls (failure messages, file names) is capped and quoted so it cannot change the
layout around it.
Students see the score summary, the public test results and the notices. Each question's row,
which also lists its hidden tests, is hidden from them. Text a student controls (failure
messages, file names) is capped and quoted so it cannot change the layout around it.
"""

import os
Expand All @@ -18,6 +18,8 @@

SKIPPED_DIRS = {"__MACOSX", "__pycache__"}
NOTICE_ROW = "Submission"
PUBLIC_ROW = "Public tests"
ALL_PASSED = "All tests passed"


class Row(TypedDict):
Expand All @@ -37,11 +39,14 @@ class Results(TypedDict):
"""The content of `results.json`.

`score` is set only when `tests` is empty, since Gradescope then has no rows to add up.
`output` is the score summary for students: Gradescope hides the total when a row is hidden.
"""

tests: list[Row]
stdout_visibility: str
score: NotRequired[float]
output: NotRequired[str]
output_format: NotRequired[str]


@dataclass(frozen=True, slots=True)
Expand Down Expand Up @@ -122,8 +127,10 @@ def build_results(
) -> Results:
"""Gradescope's `results.json` content.

Each auto-graded question gets a row worth `points × passed / total`. Its output lists the
public tests with their failure messages and gives only a count for the hidden tests.
Each auto-graded question gets a row worth `points × passed / total`, hidden from students,
that lists all its tests with their failure messages. A visible 0-point "Public tests" row,
marked failed when one of them fails, lists the public tests. The top-level output gives the
total and each question's score.
Notices go first, in a 0-point "Submission" row marked failed, so Gradescope does not show
it as passed. Manual questions get no row; with no rows at all, the results carry a
top-level score of 0.
Expand All @@ -141,7 +148,9 @@ def build_results(
ValueError: If an auto-graded question has no tests.
"""

rows = [notice_row(notices)] if notices else []
public_parts = []
public_failed = False
question_rows = []

for question in bundle.config.questions.values():
if question.manual:
Expand All @@ -155,16 +164,50 @@ def build_results(
if total == 0:
raise ValueError(f"question {question.id} has no tests")

if public:
public_parts.append(f"**{question.title}**\n\n{_output(public, [])}")
public_failed |= not all(outcome.passed for outcome in public)

passed = sum(outcome.passed for outcome in public + hidden)
score = round(question.points * passed / total, 4)
rows.append(
row(question.title, score, question.points, _output(public, hidden), question.id)
output = _output(public, hidden)
question_rows.append(
row(question.title, score, question.points, output, question.id, hidden=True)
)

if not rows:
rows = [notice_row(notices)] if notices else []

if public_parts:
rows.append(row(PUBLIC_ROW, 0, 0, "\n\n".join(public_parts), failed=public_failed))

rows += question_rows

if not question_rows:
return {"tests": rows, "stdout_visibility": "hidden", "score": 0.0}

return {"tests": rows, "stdout_visibility": "hidden"}
return {
"tests": rows,
"stdout_visibility": "hidden",
"output": _summary(question_rows),
"output_format": "md",
}


def _summary(rows: Sequence[Row]) -> str:
"""The total and each question's score, in Markdown."""

score = sum(row["score"] for row in rows)
max_score = sum(row["max_score"] for row in rows)
lines = [f"Total: {_fraction(score, max_score)}", ""]
lines += [f"- {row['name']}: {_fraction(row['score'], row['max_score'])}" for row in rows]

return "\n".join(lines)


def _fraction(score: float, max_score: float) -> str:
"""`18.75 / 25`."""

return f"{round(score, 4):g} / {max_score:g}"


def failure_results(bundle: GradingBundle | None, error: BaseException) -> Results:
Expand Down Expand Up @@ -205,16 +248,20 @@ def row(
number: str | None = None,
*,
failed: bool = False,
hidden: bool = False,
) -> Row:
"""One entry of `tests` in `results.json`; `failed` sets its status to `failed`."""
"""One entry of `tests` in `results.json`.

`failed` sets its status to `failed`, and `hidden` hides the row from students.
"""

entry = Row(
name=name,
score=score,
max_score=max_score,
output=output,
output_format="md",
visibility="visible",
visibility="hidden" if hidden else "visible",
)

if number is not None:
Expand All @@ -227,21 +274,21 @@ def row(


def _output(public: list[Outcome], hidden: list[Outcome]) -> str:
"""Markdown for one question: each public test, then the hidden count."""
"""Markdown for tests: `All tests passed`, or each test with its failure message."""

if all(outcome.passed for outcome in public + hidden):
return ALL_PASSED

parts = []
labeled = [(outcome, "") for outcome in public] + [(o, " (hidden)") for o in hidden]

for outcome in public:
for outcome, label in labeled:
mark = "✓" if outcome.passed else "✗"
parts.append(f"{mark} {inline_code(outcome.name)}")
parts.append(f"{mark} {inline_code(outcome.name)}{label}")

if not outcome.passed and outcome.message:
parts.append(code_block(cap(outcome.message)))

if hidden:
passed = sum(outcome.passed for outcome in hidden)
parts.append(f"Hidden tests: {passed}/{len(hidden)} passed")

return "\n\n".join(parts)


Expand Down
9 changes: 6 additions & 3 deletions tests/e2e/test_gradescope_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ def grade_on_host(assignment: Assignment, root: Path, name: str) -> dict:


def check_results(results: dict, expect: Expect, hidden: list[str]) -> None:
"""Assert that `results` matches `expect` and shows nothing of the hidden tests."""
"""Assert that `results` matches `expect` and shows students nothing of the hidden tests."""

text = json.dumps(results)
assert len(text) < MAX_RESULTS_BYTES, len(text)
Expand All @@ -600,11 +600,14 @@ def check_results(results: dict, expect: Expect, hidden: list[str]) -> None:
assert notice is not None, text[-TAIL:]
assert expect.notice in notice["output"]

shown = "\n".join(f"{row['name']}\n{row['output']}" for row in results["tests"])
visible = [row for row in results["tests"] if row["visibility"] != "hidden"]
assert all(row["visibility"] == "hidden" for row in (rows["q1"], rows["q2"]))

shown = "\n".join(f"{row['name']}\n{row['output']}" for row in visible)
if expect.shown is not None:
assert expect.shown in shown, shown[-TAIL:]

every = "\n".join(strings(results))
every = "\n".join(strings([results.get("output", ""), visible]))
for secret in hidden:
assert secret not in every, secret

Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_nested_upload_with_macosx_junk_gets_full_marks(paths):
results = paths.run()

by_number = rows(results)
assert list(by_number) == ["q1", "q2"]
assert list(by_number) == ["Public tests", "q1", "q2"]
assert by_number["q1"]["score"] == 4
assert by_number["q2"]["score"] == 6
assert by_number["q2"]["name"] == "Vowels"
Expand Down
Loading
Loading