cs50 / problems

Checks for check50
134 stars 227 forks source link

CS50R readlines not handled if on multiple lines #256

Closed curiouskiwi closed 2 months ago

curiouskiwi commented 2 months ago

For the checks in CS50R that are dealing with readline prompts (teahouse for example), if the code has split the readline into multiple lines, check50 will produce an error

:| teahouse.R uses readline
    check50 ran into an error while running checks!
    UnboundLocalError: cannot access local variable 'end' where it is not associated with a value
      File "/usr/local/lib/python3.12/site-packages/check50/runner.py", line 148, in wrapper
    state = check(*args)
            ^^^^^^^^^^^^
      File "/home/ubuntu/.local/share/check50/cs50/problems/teahouse/__init__.py", line 18, in readline
    new_contents = replace_readline(contents)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/ubuntu/.local/share/check50/cs50/problems/teahouse/__init__.py", line 93, in replace_readline
    line = line[:start] + f"commandArgs(trailingOnly = TRUE)[{readline_count}]" + line[end + 1:]
                                                                                       ^^

Can be replicated with code like

x <- readline(
    "Prompt: ")

instead of x <-readline("Prompt: ")

CarterZenke commented 2 months ago

Addressed with https://github.com/cs50/problems/commit/1be9de1eec9ccf9a7aab3c6e7bcb3c0905284142. Unfortunately, I don't think it's feasible to reliably find and replace readline if it spans multiple lines—so I've given students a message that they should ensure their usage spans a single line.