From f4190f94da5667003c1cb5401a8b15e6efbc223f Mon Sep 17 00:00:00 2001 From: Brandon Butler Date: Fri, 13 Oct 2023 10:48:40 -0400 Subject: [PATCH] test: Refactor hook test into single template --- tests/test_project.py | 75 +++++++++++-------------------------------- 1 file changed, 18 insertions(+), 57 deletions(-) diff --git a/tests/test_project.py b/tests/test_project.py index 49c78e86f..f8873eb77 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -2543,14 +2543,15 @@ def get_log_output(job, log_filename): class TestHooksLogCmd(TestHooksLogOperationSetUp): - error_message = "42" - runtime_message = "" - @pytest.fixture(params=["base_cmd"]) def operation_name(self, request): return request.param - def test_logging(self, project, job, operation_name): + @pytest.fixture + def error_message(operation_name): + return "42" + + def test_logging(self, project, job, operation_name, error_message): log_fn = self.get_log_filename() assert not job.isfile(log_fn) @@ -2559,50 +2560,26 @@ def test_logging(self, project, job, operation_name): self.call_subcmd(f"run -o {operation_name} -j {job.id}") else: self.call_subcmd(f"run -o {operation_name} -j {job.id}") - assert job.isfile(log_fn) log_output = self.get_log_output(job, log_fn) assert self.ON_START_MSG.format(operation_name) in log_output if job.sp.raise_exception: - assert self.error_message in log_output + assert error_message in log_output assert self.EXCEPTION_MSG.format(operation_name) in log_output else: - assert self.runtime_message in log_output + assert error_message not in log_output assert self.SUCCESS_MSG.format(operation_name) in log_output class TestHooksLogBase(TestHooksLogCmd): - error_message = define_hooks_logging_project.HOOKS_ERROR_MESSAGE + @pytest.fixture + def error_message(operation_name): + return define_hooks_logging_project.HOOKS_ERROR_MESSAGE @pytest.fixture(params=["base"]) def operation_name(self, request): return request.param - def test_start(self, project, job, operation_name): - log_fn = self.get_log_filename() - - assert not job.isfile(log_fn) - - if job.sp.raise_exception: - with pytest.raises(subprocess.CalledProcessError): - self.call_subcmd(f"run -o {operation_name} -j {job.id}") - else: - self.call_subcmd(f"run -o {operation_name} -j {job.id}") - - assert job.isfile(log_fn) - - log_output = self.get_log_output(job, log_fn) - - assert self.runtime_message in log_output - - assert self.ON_START_MSG.format(operation_name) in log_output - if job.sp.raise_exception: - assert self.error_message in log_output - assert self.EXCEPTION_MSG.format(operation_name) - else: - assert self.error_message not in log_output - assert self.SUCCESS_MSG.format(operation_name) in log_output - class TestHooksLogInstall(TestHooksLogOperationSetUp): entrypoint = dict( @@ -2611,31 +2588,15 @@ class TestHooksLogInstall(TestHooksLogOperationSetUp): ) ) - def test_install(self, project, job): - log_fn = job.fn("operations.log") - - assert not job.isfile(log_fn) - - if job.sp.raise_exception: - with pytest.raises(subprocess.CalledProcessError): - self.call_subcmd(f"run -j {job.id} -o base") - with pytest.raises(subprocess.CalledProcessError): - self.call_subcmd(f"run -j {job.id} -o base_cmd") - else: - self.call_subcmd(f"run -j {job.id} -o base") - self.call_subcmd(f"run -j {job.id} -o base_cmd") - - assert job.isfile(log_fn) - log_output = self.get_log_output(job, log_fn) + @pytest.fixture(params=["base", "base_cmd"]) + def operation_name(self, request): + return request.param - if job.sp.raise_exception: - assert "42" in log_output - assert define_hooks_logging_project.HOOKS_ERROR_MESSAGE in log_output - assert self.EXCEPTION_MSG.format("base") in log_output - assert self.EXCEPTION_MSG.format("base_cmd") in log_output - else: - assert self.SUCCESS_MSG.format("base") in log_output - assert self.SUCCESS_MSG.format("base_cmd") in log_output + @pytest.fixture + def error_message(operation_name): + if operation_name == "base": + return "42" + return define_hooks_logging_project.HOOKS_ERROR_MESSAGE class TestIgnoreConditions: