2018-12-23 12:07:31 +00:00
|
|
|
from click.termui import _ansi_colors as ansi_colors # type: ignore
|
|
|
|
|
2018-12-18 20:16:34 +00:00
|
|
|
from catfish.project import DuplicateProcessException, Project, parse_procfile_processes
|
2018-12-23 12:07:31 +00:00
|
|
|
from catfish.worker.server import PayloadType, send_to_server
|
|
|
|
from tests import BaseTestCase, BaseWorkerTestCase
|
2018-12-17 20:40:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProjectTestCase(BaseTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
self.project = Project(self.EXAMPLE_DIR)
|
|
|
|
|
|
|
|
def test_exists(self):
|
|
|
|
self.assertTrue(self.project.exists())
|
|
|
|
|
|
|
|
def test_cant_create_nonexistent_directory(self):
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
Project("/nonexistent")
|
2018-12-17 21:43:46 +00:00
|
|
|
|
|
|
|
def test_read_processes(self):
|
2018-12-19 17:26:09 +00:00
|
|
|
self.assertEqual(len(self.project.processes), 4)
|
2018-12-18 20:16:34 +00:00
|
|
|
web_process = self.project.processes[0]
|
|
|
|
self.assertEqual(web_process.name, "web")
|
|
|
|
self.assertEqual(web_process.command, "python -m http.server $PORT")
|
|
|
|
self.assertEqual(web_process.project, self.project)
|
|
|
|
|
|
|
|
bg_process = self.project.processes[1]
|
|
|
|
self.assertEqual(bg_process.name, "bg")
|
|
|
|
self.assertEqual(bg_process.command, "python src/dummy_program.py")
|
|
|
|
self.assertEqual(bg_process.project, self.project)
|
|
|
|
|
2018-12-19 17:10:58 +00:00
|
|
|
bg_process = self.project.processes[2]
|
2018-12-19 17:26:09 +00:00
|
|
|
self.assertEqual(bg_process.name, "die")
|
2018-12-19 17:10:58 +00:00
|
|
|
self.assertEqual(bg_process.command, "python src/die_soon.py")
|
|
|
|
self.assertEqual(bg_process.project, self.project)
|
|
|
|
|
2018-12-19 17:26:09 +00:00
|
|
|
bg_process = self.project.processes[3]
|
|
|
|
self.assertEqual(bg_process.name, "exit")
|
|
|
|
self.assertEqual(bg_process.command, "python src/exit_soon.py")
|
|
|
|
self.assertEqual(bg_process.project, self.project)
|
|
|
|
|
2018-12-18 20:16:34 +00:00
|
|
|
def test_get_process(self):
|
|
|
|
self.assertEqual(self.project.get_process("web").name, "web")
|
|
|
|
self.assertEqual(self.project.get_process("bg").name, "bg")
|
|
|
|
self.assertIsNone(self.project.get_process("nonexistent"))
|
2018-12-18 15:34:57 +00:00
|
|
|
|
|
|
|
def test_name(self):
|
2018-12-18 15:49:20 +00:00
|
|
|
self.assertEqual(self.project.name, "example")
|
2018-12-18 20:16:34 +00:00
|
|
|
|
2018-12-18 21:42:23 +00:00
|
|
|
def test_read_environment(self):
|
|
|
|
self.assertEqual(self.project.env["FOO"], "bar")
|
|
|
|
|
2018-12-18 20:16:34 +00:00
|
|
|
|
|
|
|
class ProcessTestCase(BaseTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
self.project = Project(self.EXAMPLE_DIR)
|
|
|
|
self.process = self.project.get_process("web")
|
|
|
|
|
|
|
|
def test_process_ident(self):
|
|
|
|
self.assertEqual(self.process.ident, "example:web")
|
|
|
|
|
|
|
|
def test_duplicate_procfile(self):
|
|
|
|
with self.assertRaises(DuplicateProcessException) as e:
|
|
|
|
list(parse_procfile_processes(self.project, ["web: 123.py", "web: 456.py"]))
|
|
|
|
|
|
|
|
self.assertEqual(str(e.exception), "web")
|
2018-12-21 14:35:41 +00:00
|
|
|
|
|
|
|
def test_running_process(self):
|
|
|
|
self.assertFalse(self.process.is_running)
|
2018-12-23 12:07:31 +00:00
|
|
|
|
|
|
|
def test_unique_colour(self):
|
|
|
|
unique_colours = {proc.colour for proc in self.project.processes}
|
|
|
|
self.assertEqual(len(unique_colours), len(self.project.processes))
|
|
|
|
for colour in unique_colours:
|
|
|
|
self.assertIn(colour, ansi_colors)
|
|
|
|
|
|
|
|
|
|
|
|
class RunningProcessTestCase(BaseWorkerTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
self.project = Project(self.EXAMPLE_DIR)
|
|
|
|
self.process = self.project.get_process("web")
|
|
|
|
self.response = send_to_server(
|
|
|
|
PayloadType.PROCESS,
|
|
|
|
{"path": str(self.project.root), "process": str(self.process.name)},
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_running_process(self):
|
|
|
|
self.assertTrue(self.process.is_running)
|
|
|
|
|
|
|
|
def test_pid(self):
|
|
|
|
self.assertEqual(self.process.pid, self.response["pid"])
|