Skip to content

Commit

Permalink
Merge pull request #260 from cs50/exit_code
Browse files Browse the repository at this point in the history
exit with error code when check fails
  • Loading branch information
Kareem Zidane committed May 25, 2021
2 parents 46dc500 + 9c9b5ea commit 53f58e3
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 1 deletion.
5 changes: 5 additions & 0 deletions check50/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ def main():

termcolor.cprint(_("To see the results in your browser go to {}").format(url), "white", attrs=["bold"])

sys.exit(should_fail(results))

def should_fail(results):
return "error" in results or any(not result["passed"] for result in results["results"])


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"console_scripts": ["check50=check50.__main__:main"]
},
url="https://github.com/cs50/check50",
version="3.2.2",
version="3.3.0",
include_package_data=True
)
31 changes: 31 additions & 0 deletions tests/check50_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pexpect
import pathlib
import shutil
import subprocess
import os
import tempfile

Expand Down Expand Up @@ -461,5 +462,35 @@ def test_directories_exist(self):
process = pexpect.spawn(f"check50 --dev {CHECKS_DIRECTORY}/internal_directories")
process.expect_exact(":)")


class TestExitCode(Base):
def test_error_result_exit_code(self):
process = subprocess.run(
["check50", "--dev", f"{CHECKS_DIRECTORY}/exit_code/error"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=2
)
self.assertEqual(process.returncode, 1)

def test_failed_check_exit_code(self):
process = subprocess.run(
["check50", "--dev", f"{CHECKS_DIRECTORY}/exit_code/failure"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=2
)
self.assertEqual(process.returncode, 1)

def test_successful_exit(self):
process = subprocess.run(
["check50", "--dev", f"{CHECKS_DIRECTORY}/exit_code/success"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=2
)
self.assertEqual(process.returncode, 0)


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions tests/checks/exit_code/error/.cs50.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
check50:
checks: check.py
22 changes: 22 additions & 0 deletions tests/checks/exit_code/error/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import check50
from check50._exceptions import RemoteCheckError

json = {
"slug": "jelleas/foo/master",
"error": {
"type": "InvalidSlugError",
"value": "foo",
"traceback": [
"Traceback (most recent call last):\n",
"bar\n"
],
"actions": {
"show_traceback": False,
"message": "foo"
},
"data": {}
},
"version": "3.1.1"
}

raise RemoteCheckError(json)
1 change: 1 addition & 0 deletions tests/checks/exit_code/failure/.cs50.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
check50: true
5 changes: 5 additions & 0 deletions tests/checks/exit_code/failure/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import check50

@check50.check()
def should_fail():
raise check50.Failure("BANG!")
1 change: 1 addition & 0 deletions tests/checks/exit_code/success/.cs50.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
check50: true
5 changes: 5 additions & 0 deletions tests/checks/exit_code/success/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import check50

@check50.check()
def should_pass():
pass

0 comments on commit 53f58e3

Please sign in to comment.