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

The walk stopped after 200 commits: lines older than ca1d017 point at it.

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoptionc3687bf

line 1why, one step back
1import os.path

Merge pull request #1804 from pallets/pathlibDavid Lordbefore adoption4c7399e

lines 2–3why, one step back
2import pathlib
3

Merge pull request #1747 from pallets/dependabot/pip/pip-tools-5.5.0dependabot-preview[bot]before adoptionca1d017

line 4why, one step back
4import pytest

Merge branch '8.0.x'David Lordbefore adoption65eceb0

line 5why, one step back
5from conftest import symlinks_supported

Merge pull request #1747 from pallets/dependabot/pip/pip-tools-5.5.0dependabot-preview[bot]before adoptionca1d017

lines 6–82why, one step back
6
7import 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)

Merge pull request #1804 from pallets/pathlibDavid Lordbefore adoption4c7399e

lines 83–102why, one step back
83 assert result.exception is None
84 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)

Merge pull request #1747 from pallets/dependabot/pip/pip-tools-5.5.0dependabot-preview[bot]before adoptionca1d017

lines 103–104why, one step back
103 assert result.exception is None
104 assert result.return_value == expect

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoptionc3687bf

lines 105–106why, one step back
105
106

Merge branch '8.0.x'David Lordbefore adoption65eceb0

lines 107–108why, one step back
107@pytest.mark.skipif(
108 not symlinks_supported, reason="The current OS or FS doesn't support symlinks."

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoptionc3687bf

line 109why, one step back
109)

Merge branch '8.0.x'David Lordbefore adoption65eceb0

lines 110–113why, one step back
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("")

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoptionc3687bf

line 114why, one step back
114

Merge branch '8.0.x'David Lordbefore adoption65eceb0

lines 115–117why, one step back
115 path_type = click.Path(resolve_path=True)
116 param = click.Argument(["a"], type=path_type)117 ctx = click.Context(click.Command("cli", params=[param]))

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoptionc3687bf

line 118why, one step back
118

Merge branch '8.0.x'David Lordbefore adoption65eceb0

lines 119–120why, one step back
119 test_dir = tmp_path / "dir"
120 test_dir.mkdir()

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoptioncd5d188

line 121why, one step back
121

Merge branch '8.0.x'David Lordbefore adoption65eceb0

lines 122–125why, one step back
122 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_str

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoptioncd5d188

line 126why, one step back
126

Merge branch '8.0.x'David Lordbefore adoption65eceb0

lines 127–130why, one step back
127 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