Skip to content

Commit

Permalink
Added the ability for tests to avoid a CLI executable exiting on errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
eriq-augustine committed Sep 5, 2024
1 parent faa4214 commit c29453f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion autograder/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def handle_api_request(arguments, params, endpoint, exit_on_error = False, files
except autograder.api.error.AutograderError as ex:
if (exit_on_error):
print("ERROR: " + ex.args[0], file = sys.stderr)
sys.exit(1)
autograder.api.error.exit_from_error(1)

raise ex

Expand Down
2 changes: 1 addition & 1 deletion autograder/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def parse_api_config(config, params,
except autograder.api.error.APIError as ex:
if (exit_on_error):
print("ERROR: " + ex.args[0], file = sys.stderr)
sys.exit(1)
autograder.api.error.exit_from_error(1)

raise ex

Expand Down
17 changes: 17 additions & 0 deletions autograder/api/error.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import sys

# Control if exit_from_error() should actually exit.
# Testing infrastructure can set this to control exit behavior.
_exit_on_error_for_testing = True

def exit_from_error(exit_status = 1):
"""
Exit because an error occurred.
Tetsing infrastructure can set _exit_on_error_for_testing to false to avoid exiting.
"""

if (not _exit_on_error_for_testing):
return

sys.exit(exit_status)

class AutograderError(Exception):
pass

Expand Down
5 changes: 2 additions & 3 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import re
import sys

import requests

import tests.server.base
import tests.server.server
import autograder.api.error
import autograder.util.dirent

THIS_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Expand Down Expand Up @@ -175,7 +174,7 @@ def __method(self):
if (is_error):
self.fail("No error was not raised when one was expected ('%s')." % (
str(expected_output)))
except requests.exceptions.ConnectionError:
except autograder.api.error.ConnectionError:
# Catch errors where the server does not responsed and suppress large connection errors.
try:
self.fail("Server had an error. See earlier output from the server.")
Expand Down
7 changes: 7 additions & 0 deletions tests/server/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import sys

import autograder.api.error
import tests.server.server

SERVER_URL_FORMAT = "http://127.0.0.1:%s"
Expand All @@ -25,11 +26,17 @@ def setUpClass(cls):
cls._server_process, cls._port = tests.server.server.start()
cls._base_arguments['server'] = SERVER_URL_FORMAT % cls._port

# Do not actually exit on errors, raise instead.
autograder.api.error._exit_on_error_for_testing = False

@classmethod
def tearDownClass(cls):
tests.server.server.stop(cls._server_process)
cls._server_process = None

# Reset.
autograder.api.error._exit_on_error_for_testing = True

def get_base_arguments(self):
return ServerBaseTest._base_arguments.copy()

Expand Down

0 comments on commit c29453f

Please sign in to comment.