Skip to content
Myr

test_types.py

This file

Path

tests/test_types.py

140 lines · 4.5 KiB · Python

Last changed by

Merge pull request #2046 from MLH-Fellowship/1921-fix-symlinksDavid Lordbefore adoption09b9d61

History of this file

Take it

1import os.path2import pathlib34import pytest5from conftest import check_symlink_impl67import 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(not check_symlink_impl(), reason="symlink not allowed on device")108@pytest.mark.parametrize(109    ("sym_file", "abs_fun"),110    [111        (("relative_symlink",), os.path.basename),112        (("test", "absolute_symlink"), lambda x: x),113    ],114)115def test_symlink_resolution(tmpdir, sym_file, abs_fun):116    """This test ensures symlinks are properly resolved by click"""117    tempdir = str(tmpdir)118    real_path = os.path.join(tempdir, "test_file")119    sym_path = os.path.join(tempdir, *sym_file)120121    # create dirs and files122    os.makedirs(os.path.join(tempdir, "test"), exist_ok=True)123    open(real_path, "w").close()124    os.symlink(abs_fun(real_path), sym_path)125126    # test127    ctx = click.Context(click.Command("do_stuff"))128    rv = click.Path(resolve_path=True).convert(sym_path, None, ctx)129130    # os.readlink prepends path prefixes to absolute131    # links in windows.132    # https://docs.microsoft.com/en-us/windows/win32/133    # ... fileio/naming-a-file#win32-file-namespaces134    #135    # Here we strip win32 path prefix from the resolved path136    rv_drive, rv_path = os.path.splitdrive(rv)137    stripped_rv_drive = rv_drive.split(os.path.sep)[-1]138    rv = os.path.join(stripped_rv_drive, rv_path)139140    assert rv == real_path