diff --git a/crab/cli.py b/crab/cli.py index a62d567..01ab03a 100644 --- a/crab/cli.py +++ b/crab/cli.py @@ -2,6 +2,7 @@ from crab import router, __version__ import shlex import socket +import subprocess import sys from dotenv import dotenv_values import shutil @@ -78,12 +79,20 @@ def main(command=None): port = env.setdefault("PORT", get_free_port()) command = [item.replace("$PORT", port) for item in command] - if shutil.which(command[0], path=env["PATH"]) is None: - print('Could not find "{}" in your procfile or $PATH.'.format(command[0])) - exit(1) + # Is it a shell function? + if ( + subprocess.run( + ["declare", "-f", command[0]], shell=True, stdout=subprocess.DEVNULL + ).returncode + == 0 + ): + return os.execvpe(env["SHELL"], [env["SHELL"], "-ci"] + command, env) + + if shutil.which(command[0], path=env["PATH"]) is not None: + return os.execvpe(command[0], command, env) - # off we go - os.execvpe(command[0], command, env) + print('Could not find "{}" in your procfile or $PATH.'.format(command[0])) + exit(1) if __name__ == "__main__": diff --git a/tests/test_cli.py b/tests/test_cli.py index 48f3ff9..986c30b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -21,7 +21,7 @@ def test_missing_command(self): cmd = subprocess.run( ["crab", "this-command-shouldnt-exist"], stdout=subprocess.PIPE ) - self.assertEqual(cmd.returncode, 1) + self.assertNotEqual(cmd.returncode, 0) self.assertIn( 'Could not find "this-command-shouldnt-exist" in your procfile or $PATH.', cmd.stdout.decode(), diff --git a/tests/test_router.py b/tests/test_router.py index 2e4f149..4c2064a 100644 --- a/tests/test_router.py +++ b/tests/test_router.py @@ -11,5 +11,5 @@ def test_get_routes(self): env={**os.environ, "VIRTUAL_HOST": "test.localhost", "PORT": "1234"}, ) as subproc: routes = get_routes() - self.assertEqual(routes, {"test.localhost": "1234"}) + self.assertEqual(routes["test.localhost"], "1234") subproc.terminate()