Skip to content
Myr

test_types.py

This file

Path

tests/test_types.py

130 lines · 4.1 KiB · Python

Last changed by

Merge branch '8.0.x'David Lordbefore adoption65eceb0

History of this file

Take it

1import os.path2import pathlib34import pytest5from conftest import symlinks_supported67import click8910@pytest.mark.parametrize(11    ("type", "value", "expect"),12    [13        (click.IntRange(0, 5), "3", 3),14        (click.IntRange(5), "5", 5),15        (click.IntRange(5), "100", 100),16        (click.IntRange(max=5), "5", 5),17        (click.IntRange(max=5), "-100", -100),18        (click.IntRange(0, clamp=True), "-1", 0),19        (click.IntRange(max=5, clamp=True), "6", 5),20        (click.IntRange(0, min_open=True, clamp=True), "0", 1),21        (click.IntRange(max=5, max_open=True, clamp=True), "5", 4),22        (click.FloatRange(0.5, 1.5), "1.2", 1.2),23        (click.FloatRange(0.5, min_open=True), "0.51", 0.51),24        (click.FloatRange(max=1.5, max_open=True), "1.49", 1.49),25        (click.FloatRange(0.5, clamp=True), "-0.0", 0.5),26        (click.FloatRange(max=1.5, clamp=True), "inf", 1.5),27    ],28)29def test_range(type, value, expect):30    assert type.convert(value, None, None) == expect313233@pytest.mark.parametrize(34    ("type", "value", "expect"),35    [36        (click.IntRange(0, 5), "6", "6 is not in the range 0<=x<=5."),37        (click.IntRange(5), "4", "4 is not in the range x>=5."),38        (click.IntRange(max=5), "6", "6 is not in the range x<=5."),39        (click.IntRange(0, 5, min_open=True), 0, "0<x<=5"),40        (click.IntRange(0, 5, max_open=True), 5, "0<=x<5"),41        (click.FloatRange(0.5, min_open=True), 0.5, "x>0.5"),42        (click.FloatRange(max=1.5, max_open=True), 1.5, "x<1.5"),43    ],44)45def test_range_fail(type, value, expect):46    with pytest.raises(click.BadParameter) as exc_info:47        type.convert(value, None, None)4849    assert expect in exc_info.value.message505152def test_float_range_no_clamp_open():53    with pytest.raises(TypeError):54        click.FloatRange(0, 1, max_open=True, clamp=True)5556    sneaky = click.FloatRange(0, 1, max_open=True)57    sneaky.clamp = True5859    with pytest.raises(RuntimeError):60        sneaky.convert("1.5", None, None)616263@pytest.mark.parametrize(64    ("nargs", "multiple", "default", "expect"),65    [66        (2, False, None, None),67        (2, False, (None, None), (None, None)),68        (None, True, None, ()),69        (None, True, (None, None), (None, None)),70        (2, True, None, ()),71        (2, True, [(None, None)], ((None, None),)),72        (-1, None, None, ()),73    ],74)75def test_cast_multi_default(runner, nargs, multiple, default, expect):76    if nargs == -1:77        param = click.Argument(["a"], nargs=nargs, default=default)78    else:79        param = click.Option(["-a"], nargs=nargs, multiple=multiple, default=default)8081    cli = click.Command("cli", params=[param], callback=lambda a: a)82    result = runner.invoke(cli, standalone_mode=False)83    assert result.exception is None84    assert result.return_value == expect858687@pytest.mark.parametrize(88    ("cls", "expect"),89    [90        (None, "a/b/c.txt"),91        (str, "a/b/c.txt"),92        (bytes, b"a/b/c.txt"),93        (pathlib.Path, pathlib.Path("a", "b", "c.txt")),94    ],95)96def test_path_type(runner, cls, expect):97    cli = click.Command(98        "cli",99        params=[click.Argument(["p"], type=click.Path(path_type=cls))],100        callback=lambda p: p,101    )102    result = runner.invoke(cli, ["a/b/c.txt"], standalone_mode=False)103    assert result.exception is None104    assert result.return_value == expect105106107@pytest.mark.skipif(108    not symlinks_supported, reason="The current OS or FS doesn't support symlinks."109)110def test_path_resolve_symlink(tmp_path, runner):111    test_file = tmp_path / "file"112    test_file_str = os.fsdecode(test_file)113    test_file.write_text("")114115    path_type = click.Path(resolve_path=True)116    param = click.Argument(["a"], type=path_type)117    ctx = click.Context(click.Command("cli", params=[param]))118119    test_dir = tmp_path / "dir"120    test_dir.mkdir()121122    abs_link = test_dir / "abs"123    abs_link.symlink_to(test_file)124    abs_rv = path_type.convert(os.fsdecode(abs_link), param, ctx)125    assert abs_rv == test_file_str126127    rel_link = test_dir / "rel"128    rel_link.symlink_to(pathlib.Path("..") / "file")129    rel_rv = path_type.convert(os.fsdecode(rel_link), param, ctx)130    assert rel_rv == test_file_str