diff --git a/tmt/cli.py b/tmt/cli.py index e3f3701209..c9a0d1fbca 100644 --- a/tmt/cli.py +++ b/tmt/cli.py @@ -1880,12 +1880,14 @@ def status( @option( '-s', '--skip', choices=CLEAN_RESOURCES, help='The resources which should be kept on the disk.', multiple=True) +@workdir_root_options @verbosity_options @dry_options def clean(context: Context, last: bool, id_: tuple[str, ...], skip: list[str], + workdir_root: str, **kwargs: Any) -> None: """ Clean workdirs, guests or images. @@ -1903,6 +1905,9 @@ def clean(context: Context, raise tmt.utils.GeneralError( "Options --last and --id cannot be used together.") + if workdir_root and not Path(workdir_root).exists(): + raise tmt.utils.GeneralError(f"Path '{workdir_root}' doesn't exist.") + context.obj.clean_logger = context.obj.logger \ .descend(logger_name='clean', extra_shift=0) \ .apply_verbosity_options(**kwargs) @@ -1917,8 +1922,6 @@ def clean(context: Context, if context.invoked_subcommand is None: assert context.obj.clean_logger is not None # narrow type - # Set path to default - context.params['workdir_root'] = tmt.utils.WORKDIR_ROOT # Create another level to the hierarchy so that logging indent is # consistent between the command and subcommands clean_obj = tmt.Clean( @@ -1927,14 +1930,16 @@ def clean(context: Context, .apply_verbosity_options(**kwargs), parent=clean_obj, cli_invocation=CliInvocation.from_context(context)) - if tmt.utils.WORKDIR_ROOT.exists(): + root_path = effective_workdir_root(workdir_root) + clean_obj.workdir_root = root_path + if root_path.exists(): if 'guests' not in skip and not clean_obj.guests(): exit_code = 1 if 'runs' not in skip and not clean_obj.runs(id_): exit_code = 1 else: clean_obj.warn( - f"Directory '{tmt.utils.WORKDIR_ROOT}' does not exist, " + f"Directory '{root_path}' does not exist, " f"skipping guest and run cleanup.") if 'images' not in skip and not clean_obj.images(): exit_code = 1