radeklat / delfino

A toolbox of command line helper script, wrapping tools used during Python development.
MIT License
12 stars 3 forks source link

Pass through options/arguments to pytest #35

Closed shimpeko closed 1 year ago

shimpeko commented 2 years ago

closes https://github.com/radeklat/delfino/issues/34

Implement https://github.com/radeklat/delfino/issues/34 for test-related commands, test-unit and test-integration.

These command now can pass options/arguments to its underlying command, pytest. Options/arguments to be passed, pass-through options/arguments, can be configured in two ways; config file and command line argument.

Please see this docstring for detail usage.

codecov[bot] commented 2 years ago

Codecov Report

Base: 47.86% // Head: 48.15% // Increases project coverage by +0.28% :tada:

Coverage data is based on head (cfca4da) compared to base (e34c621). Patch coverage: 59.61% of modified lines in pull request are covered.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #35 +/- ## ========================================== + Coverage 47.86% 48.15% +0.28% ========================================== Files 21 22 +1 Lines 750 785 +35 Branches 109 117 +8 ========================================== + Hits 359 378 +19 - Misses 388 403 +15 - Partials 3 4 +1 ``` | Flag | Coverage Δ | | |---|---|---| | integration_tests | `5.85% <0.00%> (-0.28%)` | :arrow_down: | | total | `48.02% <59.61%> (+0.29%)` | :arrow_up: | | unit_tests | `48.02% <59.61%> (+0.29%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Radek+L%C3%A1t#carryforward-flags-in-the-pull-request-comment) to find out more. | [Impacted Files](https://codecov.io/gh/radeklat/delfino/pull/35?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Radek+L%C3%A1t) | Coverage Δ | | |---|---|---| | [src/delfino/execution.py](https://codecov.io/gh/radeklat/delfino/pull/35/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Radek+L%C3%A1t#diff-c3JjL2RlbGZpbm8vZXhlY3V0aW9uLnB5) | `33.33% <0.00%> (-1.29%)` | :arrow_down: | | [src/delfino/models/pyproject\_toml.py](https://codecov.io/gh/radeklat/delfino/pull/35/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Radek+L%C3%A1t#diff-c3JjL2RlbGZpbm8vbW9kZWxzL3B5cHJvamVjdF90b21sLnB5) | `100.00% <ø> (ø)` | | | [src/delfino/commands/test.py](https://codecov.io/gh/radeklat/delfino/pull/35/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Radek+L%C3%A1t#diff-c3JjL2RlbGZpbm8vY29tbWFuZHMvdGVzdC5weQ==) | `40.96% <60.86%> (-0.60%)` | :arrow_down: | | [src/delfino/click\_utils/pass\_through\_command.py](https://codecov.io/gh/radeklat/delfino/pull/35/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Radek+L%C3%A1t#diff-c3JjL2RlbGZpbm8vY2xpY2tfdXRpbHMvcGFzc190aHJvdWdoX2NvbW1hbmQucHk=) | `62.96% <62.96%> (ø)` | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Radek+L%C3%A1t). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Radek+L%C3%A1t)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

shimpeko commented 2 years ago

I have a few questions about the implementation but I think the root cause of why I have them is that there is not documentation on how to use the patterns you are introducing in this code. If I wanted to add pass through functionality to a different command, I wouldn't know how.

Can you please write a documentation into the README on how to make commands pass through? Trying to explain it might result in changes of the implementation as well.

I've explained it on a docstring hope it explains how to use it. I've avoided README as is more about how we design commands rather than Delfino's functionality.

shimpeko commented 2 years ago

Discussed offline as https://github.com/radeklat/delfino/issues/34#issuecomment-1256142952


@radeklat I have three options for implementation and I think Option 1 is the best because of the following reason. Can I have your thought?

The downside of option 1 is a complex UI.

Option1: Use --CMD-option

@click.command()
@click.option("--mypy-option", type=str, callback=shlex.split) # callback is to split option value and make it list of str
def typecheck(mypy_option: List[str]):
    run(["mypy", *mypy_option])

$ delfino typecheck --mypy-option '--huga path_to_file'
-> run `mypy --huga path_to_file`

The implementation of a helper decorator for this option can be found here.

Option2: Use argument

@click.command()
@click.argument("mypy_option", type=str, nargs=-1) # nargs=-1 will accept unlimited number of args
def typecheck(mypy_option: List[str], mypy_option: List[str]):
    run(["mypy", *mypy_option])

$ delfino typecheck path_to_file
-> run `mypy path_to_file`

$ delfino typecheck --huga path_to_file
-> raise error as `--huga` option is not defined

$ delfino typecheck -- --huga path_to_file' # All values after `--` will be considered as arg
-> run `mypy --huga path_to_file`

Option3: Use allow_unknown_options and allow_extra_args

@click.command(allow_unknow_options=True, allow_extra_args=True) # will pass all undefined options + argument in to click_context.args
@click.pass_context
def typecheck(mypy_option: List[str], click_context: click.Context):
    run(["mypy", *click_context.args])

$ delfino typecheck --huga path_to_file
-> run `mypy --huga path_to_file`

The implementation of helper decorator for this option can be found here.

radeklat commented 1 year ago

Finished as part of #49. Thank you for the research of the options and base functionality.