如何通过 python click.version_option 装饰器的 Travis CI 测试?

How to pass Travis CI test for python click.version_option decorator?

我试图在 Travis CI 中为我的 click 应用程序通过以下测试:

@pytest.fixture
def runner():
    return CliRunner()  # from click.testing import CliRunner
    enter code here


def test_cli_with_version(runner):
    result = runner.invoke(cli.main, ['--version'])
    assert result.exit_code == 0
    assert not result.exception
    assert result.output.strip()

目标函数是一个标准的 click 组函数。

@click.group(invoke_without_command=True)
@click.help_option(help='Show help index and exit')
@click.version_option(message='%(prog)s v%(version)s', help='Show the version and exit')
@click.option('-p', '--profile', default='dev', help='Set configuration profile')
@click.pass_context
def main(ctx, profile='dev'):
    if ctx.invoked_subcommand is None:
        click.echo(click.style(title, fg='green'))

    ctx.obj = ContextConfig().load_for(profile)

测试的目标是模拟一个看起来像 main --version.

的命令

当我 运行 在本地环境中进行此测试时,它通过了(与其他测试一起)。

$ pytest
============================ test session starts ============================
platform linux -- Python 3.8.3, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /path/to/root/dir
collected 15 items                                                          

tests/test_cli.py ..........                                          [ 66%]
tests/test_contexts.py .                                              [ 73%]
tests/test_models.py .                                                [ 80%]
tests/test_utils.py ...                                               [100%]

============================ 15 passed in 0.30s =============================

但是,当 Travis 尝试构建它时,这个特定的测试失败了。这是来自 Travis

的调试日志
$ pytest
============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: /path/to/root/dir
collected 15 items                                                             
tests/test_cli.py ..F.......                                             [ 66%]
tests/test_contexts.py .                                                 [ 73%]
tests/test_models.py .                                                   [ 80%]
tests/test_utils.py ...                                                  [100%]
=================================== FAILURES ===================================
____________________________ test_cli_with_version _____________________________
runner = <click.testing.CliRunner object at 0x7fe288f596a0>
    def test_cli_with_version(runner):
        result = runner.invoke(cli.main, ['--version'])
>       assert result.exit_code == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <Result RuntimeError('Could not determine version',)>.exit_code
tests/test_cli.py:35: AssertionError
=========================== short test summary info ============================
FAILED tests/test_cli.py::test_cli_with_version - AssertionError: assert 1 == 0
========================= 1 failed, 14 passed in 0.34s =========================
The command "pytest" exited with 1.
Done. Your build exited with 1.

看起来 click.version_option 装饰器无法读取 Travis CI 环境中的版本字符串。回购的版本控制由 setuptools-scm 库维护。但是,我还没有给 master b运行ch 添加任何标签。

.travis.yml 文件如下所示:

language: python
# target python versions
python:
  - "3.6"
  - "3.7"
  - "3.8"
# operating systems
os:
  - linux
  - osx
  - windows
# configure jobs
jobs:
  allow_failures:
    - os: windows
    - os: osx
# install dependencies
install:
  - pip install --upgrade pip
  - pip install -r requirements.txt
# run tests
script:
  - pytest
# target branches for builds
branches:
  only:
    - master

我该如何解决这个问题?

将评论变成答案:

您需要生成包元数据以便从中读取版本。这可以通过不同的方式完成,最小的

$ python setup.py egg_info

但是,以可编辑模式安装包要好得多:

$ pip install --editable .

对于 Travis,只需在 install 部分添加另一个命令:

install:
  - pip install --upgrade pip
  - pip install -r requirements.txt
  - pip install --editable .

您甚至可以将所有三个命令合二为一:

- pip install --upgrade pip -r requirements.txt --editable .