From 033bda5daf0eb16c52f6e18fb8854ee1f45a2580 Mon Sep 17 00:00:00 2001 From: Agustinus Kristiadi Date: Thu, 26 Nov 2026 15:32:04 -0500 Subject: [PATCH 1/2] feat(grader): hide hidden-test results from students Each question's row is now hidden on Gradescope and lists every test with its message. A visible 0-point "Public tests" row lists the public tests, and the top-level output gives the total and each question's score, since Gradescope hides the total once a row is hidden. Closes #33 --- docs/src/content/docs/instructors.md | 4 +- src/mogs/grader/results.py | 82 ++++++++++++++---- tests/e2e/test_gradescope_docker.py | 9 +- tests/test_main.py | 2 +- tests/test_results.py | 124 ++++++++++++++++++++++----- 5 files changed, 176 insertions(+), 45 deletions(-) diff --git a/docs/src/content/docs/instructors.md b/docs/src/content/docs/instructors.md index 5147317..095ccc5 100644 --- a/docs/src/content/docs/instructors.md +++ b/docs/src/content/docs/instructors.md @@ -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 diff --git a/src/mogs/grader/results.py b/src/mogs/grader/results.py index 1ba5de1..c7d9b86 100644 --- a/src/mogs/grader/results.py +++ b/src/mogs/grader/results.py @@ -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 @@ -18,6 +18,8 @@ SKIPPED_DIRS = {"__MACOSX", "__pycache__"} NOTICE_ROW = "Submission" +PUBLIC_ROW = "Public tests" +ALL_PASSED = "All tests passed" class Row(TypedDict): @@ -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) @@ -122,8 +127,9 @@ 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 + lists the public tests, and 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. @@ -141,7 +147,8 @@ def build_results( ValueError: If an auto-graded question has no tests. """ - rows = [notice_row(notices)] if notices else [] + public_parts = [] + question_rows = [] for question in bundle.config.questions.values(): if question.manual: @@ -155,16 +162,49 @@ 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, [])}") + 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))) + + 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: @@ -205,8 +245,12 @@ 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, @@ -214,7 +258,7 @@ def row( max_score=max_score, output=output, output_format="md", - visibility="visible", + visibility="hidden" if hidden else "visible", ) if number is not None: @@ -227,21 +271,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) diff --git a/tests/e2e/test_gradescope_docker.py b/tests/e2e/test_gradescope_docker.py index fff8117..4bb6319 100644 --- a/tests/e2e/test_gradescope_docker.py +++ b/tests/e2e/test_gradescope_docker.py @@ -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) @@ -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 diff --git a/tests/test_main.py b/tests/test_main.py index 94f333d..efd3d15 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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" diff --git a/tests/test_results.py b/tests/test_results.py index b3e53df..4ee372c 100644 --- a/tests/test_results.py +++ b/tests/test_results.py @@ -30,19 +30,32 @@ def outcomes(failing: str = "test_q1_secret_y", message: str = "SECRET hidden de return result +def question_row(results, qid: str): + [row] = [row for row in results["tests"] if row.get("number") == qid] + return row + + +def public_row(results): + [row] = [row for row in results["tests"] if row["name"] == "Public tests"] + return row + + +def shown_to_students(results) -> list: + rows = [row for row in results["tests"] if row["visibility"] != "hidden"] + return [results.get("output", ""), rows] + + # build_results def test_three_of_four_tests_give_three_quarters_of_the_points(): results = build_results(bundle(), outcomes(), []) - [row] = results["tests"] + row = question_row(results, "q1") assert row["name"] == "Addition" - assert row["number"] == "q1" assert row["score"] == 18.75 assert row["max_score"] == 25 assert row["output_format"] == "md" - assert row["visibility"] == "visible" assert results["stdout_visibility"] == "hidden" @@ -53,7 +66,7 @@ def test_score_is_rounded(): ) result = {name: Outcome(name, name == "test_q1_a") for name in cells[0].tests} - [row] = build_results(grading, result, [])["tests"] + row = question_row(build_results(grading, result, []), "q1") assert row["score"] == 0.3333 @@ -61,32 +74,101 @@ def test_score_is_rounded(): def test_manual_questions_get_no_row(): results = build_results(bundle(), outcomes(), []) - assert [row["number"] for row in results["tests"]] == ["q1"] + assert [row.get("number") for row in results["tests"]] == [None, "q1"] + + +def test_question_rows_are_hidden_from_students(): + results = build_results(bundle(), outcomes(), []) + + assert question_row(results, "q1")["visibility"] == "hidden" -def test_hidden_names_and_messages_never_reach_the_output(): +def test_hidden_names_and_messages_reach_only_hidden_rows(): results = build_results(bundle(), outcomes(), []) - text = json.dumps(results) + text = json.dumps(shown_to_students(results)) assert "SECRET" not in text assert "secret" not in text - assert "Hidden tests: 1/2 passed" in results["tests"][0]["output"] + assert "Hidden" not in text + + output = question_row(results, "q1")["output"] + assert "✗ `test_q1_secret_y`" in output + assert "SECRET hidden detail" in output + + +def test_public_row_is_visible_and_worth_nothing(): + results = build_results(bundle(), outcomes("test_q1_b", "AssertionError: nope"), []) + + row = public_row(results) + assert row["visibility"] == "visible" + assert row["score"] == 0 + assert row["max_score"] == 0 + assert "number" not in row + assert "status" not in row + assert "Addition" in row["output"] + assert "✓ `test_q1_a`" in row["output"] + assert "✗ `test_q1_b`" in row["output"] + assert "AssertionError: nope" in row["output"] + + +def test_question_row_lists_public_and_hidden_tests(): + output = question_row(build_results(bundle(), outcomes(), []), "q1")["output"] + + for name in PUBLIC + HIDDEN: + assert f"`{name}`" in output + +def test_all_passing_tests_are_summed_up(): + passing = {name: Outcome(name, True) for name in PUBLIC + HIDDEN} + results = build_results(bundle(), passing, []) + + assert "All tests passed" in public_row(results)["output"] + assert "All tests passed" in question_row(results, "q1")["output"] + assert "test_q1_a" not in public_row(results)["output"] + + +def test_summary_shows_the_total_and_each_question_score(): + results = build_results(bundle(), outcomes(), []) + + assert results["output_format"] == "md" + assert "Total: 18.75 / 25" in results["output"] + assert "Addition: 18.75 / 25" in results["output"] + assert "Essay" not in results["output"] + + +def test_question_with_only_hidden_tests_is_left_out_of_the_public_row(): + questions = { + "q1": Question("q1", "Addition", 25), + "q2": Question("q2", "Secret", 5), + } + cells = ( + TestCell("q1", False, "", PUBLIC, ()), + TestCell("q2", True, "", ("test_q2_x",), ()), + ) + grading = GradingBundle(AssignmentConfig("A", "hw.py", questions), "", cells) + result = {name: Outcome(name, True) for name in (*PUBLIC, "test_q2_x")} + + results = build_results(grading, result, []) + + assert "Secret" not in public_row(results)["output"] + assert question_row(results, "q2")["score"] == 5 + + +def test_no_public_tests_means_no_public_row(): + cells = (TestCell("q1", True, "", HIDDEN, ()),) + grading = GradingBundle( + AssignmentConfig("A", "hw.py", {"q1": Question("q1", "Q", 1)}), "", cells + ) + result = {name: Outcome(name, True) for name in HIDDEN} -def test_public_tests_are_listed_with_their_message(): - output = build_results(bundle(), outcomes("test_q1_b", "AssertionError: nope"), [])["tests"][0][ - "output" - ] + results = build_results(grading, result, []) - assert "✓ `test_q1_a`" in output - assert "✗ `test_q1_b`" in output - assert "AssertionError: nope" in output - assert "Hidden tests: 2/2 passed" in output + assert [row.get("number") for row in results["tests"]] == ["q1"] def test_message_cannot_close_its_code_fence(): message = "x\n````\n# Injected heading\n````" - output = build_results(bundle(), outcomes("test_q1_b", message), [])["tests"][0]["output"] + output = public_row(build_results(bundle(), outcomes("test_q1_b", message), []))["output"] fence = "`````" assert f"{fence}\n{message}\n{fence}" in output @@ -94,7 +176,7 @@ def test_message_cannot_close_its_code_fence(): def test_huge_messages_are_capped(): message = "y" * 1_000_000 - output = build_results(bundle(), outcomes("test_q1_b", message), [])["tests"][0]["output"] + output = public_row(build_results(bundle(), outcomes("test_q1_b", message), []))["output"] assert len(output.encode()) < 2 * MAX_BYTES @@ -108,11 +190,11 @@ def test_notices_come_first_in_a_zero_point_row(): assert first["max_score"] == 0 assert "No PDF found." in first["output"] assert len(first["output"].encode()) < 3 * MAX_BYTES - assert results["tests"][1]["number"] == "q1" + assert results["tests"][1]["name"] == "Public tests" def test_notice_row_shows_as_failed_and_question_rows_carry_no_status(): - [notice, question] = build_results(bundle(), outcomes(), ["No PDF found."])["tests"] + [notice, _, question] = build_results(bundle(), outcomes(), ["No PDF found."])["tests"] assert notice["status"] == "failed" assert "status" not in question @@ -126,6 +208,7 @@ def test_all_manual_assignment_gets_a_top_level_score(): assert results["tests"] == [] assert results["score"] == 0.0 + assert "output" not in results def test_graded_assignment_has_no_top_level_score(): @@ -138,6 +221,7 @@ def test_failure_rows_show_as_failed(grading): assert rows assert all(row["status"] == "failed" for row in rows) + assert all(row["visibility"] == "visible" for row in rows) # find_submission From 324626f445559deddc8f89ed8de72519a6324840 Mon Sep 17 00:00:00 2001 From: Agustinus Kristiadi Date: Thu, 26 Nov 2026 16:05:12 -0500 Subject: [PATCH 2/2] fix(grader): mark the Public tests row failed when a public test fails --- src/mogs/grader/results.py | 9 ++++++--- tests/test_results.py | 13 ++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/mogs/grader/results.py b/src/mogs/grader/results.py index c7d9b86..6a90921 100644 --- a/src/mogs/grader/results.py +++ b/src/mogs/grader/results.py @@ -128,8 +128,9 @@ def build_results( """Gradescope's `results.json` content. 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 - lists the public tests, and the top-level output gives the total and each question's score. + 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. @@ -148,6 +149,7 @@ def build_results( """ public_parts = [] + public_failed = False question_rows = [] for question in bundle.config.questions.values(): @@ -164,6 +166,7 @@ def build_results( 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) @@ -175,7 +178,7 @@ def build_results( rows = [notice_row(notices)] if notices else [] if public_parts: - rows.append(row(PUBLIC_ROW, 0, 0, "\n\n".join(public_parts))) + rows.append(row(PUBLIC_ROW, 0, 0, "\n\n".join(public_parts), failed=public_failed)) rows += question_rows diff --git a/tests/test_results.py b/tests/test_results.py index 4ee372c..b4b26dc 100644 --- a/tests/test_results.py +++ b/tests/test_results.py @@ -104,13 +104,24 @@ def test_public_row_is_visible_and_worth_nothing(): assert row["score"] == 0 assert row["max_score"] == 0 assert "number" not in row - assert "status" not in row assert "Addition" in row["output"] assert "✓ `test_q1_a`" in row["output"] assert "✗ `test_q1_b`" in row["output"] assert "AssertionError: nope" in row["output"] +def test_public_row_shows_as_failed_when_a_public_test_fails(): + results = build_results(bundle(), outcomes("test_q1_b"), []) + + assert public_row(results)["status"] == "failed" + + +def test_public_row_carries_no_status_when_only_hidden_tests_fail(): + results = build_results(bundle(), outcomes("test_q1_secret_y"), []) + + assert "status" not in public_row(results) + + def test_question_row_lists_public_and_hidden_tests(): output = question_row(build_results(bundle(), outcomes(), []), "q1")["output"]