diff --git a/src/gridtk/cli.py b/src/gridtk/cli.py index 9fd98ed..428df5c 100644 --- a/src/gridtk/cli.py +++ b/src/gridtk/cli.py @@ -94,6 +94,21 @@ def states_callback(ctx, param, value): return parse_states(value) +def no_jobs_message( + action: str, + *, + default_states: bool = False, +) -> str: + """Build a helpful message when no jobs match the given filters.""" + msg = f"No jobs were {action}." + if default_states: + msg += ( + " Note: the default state filter is active." + " Use --state all to include all jobs." + ) + return msg + + def job_filters(f_py=None, default_states=None): """Filter jobs based on the provided function and default states.""" assert callable(f_py) or f_py is None @@ -389,10 +404,10 @@ def resubmit( ) if not jobs: click.echo( - "No jobs were resubmitted. Note that the default state " - "filtering may have excluded some jobs. If you want to " - "resubmit all jobs, please use the option: " - "gridtk resubmit --state all" + no_jobs_message( + "resubmitted", + default_states=True, + ) ) for job in jobs: click.echo(f"Resubmitted job {job.id}") @@ -507,6 +522,8 @@ def truncate_str(content: str, max_width: int) -> str: maxheadercolwidths=maxcolwidths, ) ) + else: + click.echo(no_jobs_message("found")) session.commit() @@ -528,6 +545,8 @@ def stop( jobs = job_manager.stop_jobs( job_ids=job_ids, states=states, names=names, dependents=dependents ) + if not jobs: + click.echo(no_jobs_message("stopped")) for job in jobs: click.echo(f"Stopped job {job.id} with slurm id {job.grid_id}") session.commit() @@ -551,6 +570,8 @@ def delete( jobs = job_manager.delete_jobs( job_ids=job_ids, states=states, names=names, dependents=dependents ) + if not jobs: + click.echo(no_jobs_message("deleted")) for job in jobs: click.echo(f"Deleted job {job.id} with slurm id {job.grid_id}") session.commit() @@ -581,6 +602,8 @@ def report( jobs = job_manager.list_jobs( job_ids=job_ids, states=states, names=names, dependents=dependents ) + if not jobs: + click.echo(no_jobs_message("found")) for job in jobs: report_text = "" report_text += f"Job ID: {job.id}\n" diff --git a/tests/test_gridtk.py b/tests/test_gridtk.py index 7c9eed3..4bb90c5 100644 --- a/tests/test_gridtk.py +++ b/tests/test_gridtk.py @@ -286,7 +286,7 @@ def test_list_jobs(mock_check_output, runner): # test when there are no jobs result = runner.invoke(cli, ["list"]) assert_click_runner_result(result) - assert result.output == "" + assert "No jobs were found." in result.output # test when there are jobs submit_job_id = 9876543 @@ -496,8 +496,9 @@ def test_resubmit_no_jobs(mock_check_output, runner): ) result = runner.invoke(cli, ["resubmit"]) assert_click_runner_result(result) - assert "No jobs were resubmitted" in result.output - assert "gridtk resubmit --state all" in result.output + assert "No jobs were resubmitted." in result.output + assert "default state filter" in result.output + assert "--state all" in result.output @patch("subprocess.check_output")