Skip to content

Commit

Permalink
Switch to tmt.utils.Command
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
  • Loading branch information
LecrisUT committed May 9, 2023
1 parent c17acc9 commit d10f735
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions tmt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,17 @@ def completion_fish(context: Context, install: bool, **kwargs: Any) -> None:
@click.option(
'-v', '--verbose', count=True, default=0,
help='Variable passed to tmt main. See `tmt --help`')
@click.option(
'-d', '--debug', count=True, default=0,
help='Variable passed to tmt main. See `tmt --help`')
@click.option(
'--no-color', is_flag=True, default=False,
help='Variable passed to tmt main. See `tmt --help`'
)
@click.option(
'--force-color', is_flag=True, default=False,
help='Variable passed to tmt main. See `tmt --help`'
)
@click.option(
'--version', is_flag=True,
help="Variable passed to tmt main. See `tmt --help`")
Expand All @@ -1647,7 +1658,10 @@ def pre_commit(
context: Context,
root: str,
verbose: int,
debug: int,
version: bool,
no_color: bool,
force_color: bool,
lint_type: Optional[str]) -> None:
"""
Cli wrapper of tmt lint for
Expand All @@ -1656,26 +1670,37 @@ def pre_commit(
if version:
main(['--version'])

# Create logger to reuse tmt.utils.Command utility
# apply_colors_output is not used because this is recreated and handled by main app instead
apply_colors_output, apply_colors_logging = tmt.log.decide_colorization(no_color, force_color)
logger = tmt.log.Logger.create()
logger.add_console_handler(apply_colors=apply_colors_logging)

# Check that .fmf/version file is present for the specific root requested
git_root = subprocess.check_output(
args=[
'git',
'rev-parse',
'--show-toplevel'],
text=True).strip()
subprocess.check_call(
args=[
'git',
'ls-files',
'--error-unmatch',
f"{git_root}/{root}/.fmf/version"])
git_root = tmt.utils.Command('git', 'rev-parse', '--show-toplevel').\
run(cwd=None, logger=logger).stdout
if not git_root:
raise tmt.utils.GeneralError(
"git rev-parse did not produce a path")
git_root = git_root.strip()
tmt.utils.Command('git', 'ls-files', '--error-unmatch', f"{git_root}/{root}/.fmf/version").\
run(cwd=None, logger=logger)

# Construct the arguments for main cli
args = ['--root', f"{git_root}/{root}"]
if verbose:
args += ['-' + 'v' * verbose]
if debug:
args += ['-' + 'd' * debug]
if no_color:
args += ['--no-color']
if force_color:
args += ['--force-color']

# Pass test/plan/stories before lint
if lint_type:
args += [lint_type]
# Pass lint and default flags of the lint command
args += ['lint', '--source']
# Get everything else
if '--source' in context.args:
Expand Down

0 comments on commit d10f735

Please sign in to comment.