Skip to content

Commit

Permalink
test: Refactor hook test into single template
Browse files Browse the repository at this point in the history
  • Loading branch information
b-butler committed Oct 13, 2023
1 parent a6579e2 commit f4190f9
Showing 1 changed file with 18 additions and 57 deletions.
75 changes: 18 additions & 57 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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(
Expand All @@ -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:
Expand Down

0 comments on commit f4190f9

Please sign in to comment.