ucbds-infra / otter-grader

A Python and R autograding solution
https://otter-grader.readthedocs.io
BSD 3-Clause "New" or "Revised" License
123 stars 64 forks source link

Otter assign doctest generation does not handle with statements correctly #840

Open chrispyles opened 1 week ago

chrispyles commented 1 week ago

The test

import os
# print (os.getcwd())
# os.chdir('test-repo')
from otter.utils  import chdir

with chdir("test-repo"):
    import subprocess
    # Function to run shell commands and capture their output
    def run_shell_command(command):
        # Run command and capture the output
        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        output, error = process.communicate()

        # Check if there are errors
        if process.returncode != 0:
            print(f"Error: {error.decode().strip()}")
            return None

        return output.decode().strip()

    initial_hash = run_shell_command('git rev-parse HEAD')
    print(initial_hash[:6] == "d892cc")

gets converted to

>>> import os
>>> from otter.utils import chdir
>>> with chdir('test-repo'):
...     import subprocess
>>> 
...     def run_shell_command(command):
...         process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
...         (output, error) = process.communicate()
...         if process.returncode != 0:
...             print(f'Error: {error.decode().strip()}')
...             return None
...         return output.decode().strip()
...     initial_hash = run_shell_command('git rev-parse HEAD')
...     print(initial_hash[:6] == 'd892cc')
True