File: test_cli.py

package info (click to toggle)
python-cligj 0.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 168 kB
  • sloc: python: 528; makefile: 4
file content (223 lines) | stat: -rwxr-xr-x 5,949 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import os
import os.path
import sys

import click
import pytest

import cligj


def test_files_in(runner):
    @click.command()
    @cligj.files_in_arg
    def cmd(files):
        for f in files:
            click.echo(f)

    result = runner.invoke(cmd, ['1.tif', '2.tif'])
    assert not result.exception
    assert result.output.splitlines() == [
        os.path.join(os.getcwd(), '1.tif'),
        os.path.join(os.getcwd(), '2.tif'),
    ]


def test_files_inout(runner):
    @click.command()
    @cligj.files_inout_arg
    def cmd(files):
        for f in files:
            click.echo(f)

    result = runner.invoke(cmd, ['1.tif', '2.tif'])
    assert not result.exception
    assert result.output.splitlines() == [
        os.path.join(os.getcwd(), '1.tif'),
        os.path.join(os.getcwd(), '2.tif'),
    ]


def test_verbose(runner):
    @click.command()
    @cligj.verbose_opt
    def cmd(verbose):
        click.echo("%s" % verbose)

    result = runner.invoke(cmd, ['-vv'])
    assert not result.exception
    assert result.output.splitlines() == ['2']


def test_quiet(runner):
    @click.command()
    @cligj.quiet_opt
    def cmd(quiet):
        click.echo("%s" % quiet)

    result = runner.invoke(cmd, ['-qq'])
    assert not result.exception
    assert result.output.splitlines() == ['2']


def test_format(runner):
    @click.command()
    @cligj.format_opt
    def cmd(driver):
        click.echo("%s" % driver)

    result = runner.invoke(cmd, ['--driver', 'lol'])
    assert not result.exception
    assert result.output.splitlines() == ['lol']

    result = runner.invoke(cmd, ['--format', 'lol'])
    assert not result.exception
    assert result.output.splitlines() == ['lol']

    result = runner.invoke(cmd, ['-f', 'lol'])
    assert not result.exception
    assert result.output.splitlines() == ['lol']

    result = runner.invoke(cmd)
    assert not result.exception
    assert result.output.splitlines() == ['GTiff']


def test_indent(runner):
    @click.command()
    @cligj.indent_opt
    def cmd(indent):
        click.echo("%s" % indent)

    result = runner.invoke(cmd, ['--indent', '2'])
    assert not result.exception
    assert result.output.splitlines() == ['2']

    result = runner.invoke(cmd)
    assert not result.exception
    assert result.output.splitlines() == ['None']


def test_compact(runner):
    @click.command()
    @cligj.compact_opt
    def cmd(compact):
        click.echo("%s" % compact)

    result = runner.invoke(cmd, ['--compact'])
    assert not result.exception
    assert result.output.splitlines() == ['True']

    result = runner.invoke(cmd, ['--not-compact'])
    assert not result.exception
    assert result.output.splitlines() == ['False']

    result = runner.invoke(cmd)
    assert not result.exception
    assert result.output.splitlines() == ['False']


def test_precision(runner):
    @click.command()
    @cligj.precision_opt
    def cmd(precision):
        click.echo("%s" % precision)

    result = runner.invoke(cmd, ['--precision', '2'])
    assert not result.exception
    assert result.output.splitlines() == ['2']

    result = runner.invoke(cmd)
    assert not result.exception
    assert result.output.splitlines() == ['-1']


def test_projection(runner):
    @click.command()
    @cligj.projection_geographic_opt
    @cligj.projection_projected_opt
    @cligj.projection_mercator_opt
    def cmd(projection):
        click.echo("%s" % projection)

    result = runner.invoke(cmd, ['--geographic'])
    assert not result.exception
    assert result.output.splitlines() == ['geographic']

    result = runner.invoke(cmd, ['--projected'])
    assert not result.exception
    assert result.output.splitlines() == ['projected']

    result = runner.invoke(cmd, ['--mercator'])
    assert not result.exception
    assert result.output.splitlines() == ['mercator']

    result = runner.invoke(cmd)
    assert not result.exception
    assert result.output.splitlines() == ['geographic']


@pytest.mark.filterwarnings("ignore")
@pytest.mark.parametrize(
    ("opt", "val"),
    [
        ("--sequence", True),
        ("--no-sequence", False),
        (None, cligj.__version__.startswith("1.0")),
    ],
)
def test_sequence(runner, opt, val):
    """True becomes the default in 1.0"""
    @click.command()
    @cligj.sequence_opt
    def cmd(sequence):
        click.echo(str(sequence))

    result = runner.invoke(cmd, [opt] if opt is not None else [])
    assert not result.exception
    assert result.output.splitlines() == [str(val)]


@pytest.mark.skipif(sys.version_info < (3,), reason="Requires Python 3")
@pytest.mark.xfail(cligj.__version__.startswith("1.0"), reason="No warning in 1.0")
def test_sequence_warns(runner):
    """Warn about --sequence until 1.0"""
    @click.command()
    @cligj.sequence_opt
    def cmd(sequence):
        click.echo(str(sequence))

    with pytest.warns(FutureWarning):
        result = runner.invoke(cmd, ["--sequence"])


@pytest.mark.filterwarnings("ignore")
@pytest.mark.parametrize(("opt", "val"), [("--rs", True), (None, False)])
def test_sequence_rs(runner, opt, val):
    @click.command()
    @cligj.sequence_opt
    @cligj.use_rs_opt
    def cmd(sequence, use_rs):
        click.echo(str(sequence))
        click.echo(str(use_rs))

    result = runner.invoke(cmd, ["--sequence"] + ([opt] if opt is not None else []))
    assert not result.exception
    assert result.output.splitlines() == ["True", str(val)]


@pytest.mark.parametrize(
    ("opt", "val"),
    [("--collection", "collection"), ("--feature", "feature"), ("--bbox", "bbox")],
)
def test_geojson_type(runner, opt, val):
    @click.command()
    @cligj.geojson_type_collection_opt(True)
    @cligj.geojson_type_feature_opt(False)
    @cligj.geojson_type_bbox_opt(False)
    def cmd(geojson_type):
        click.echo(str(geojson_type))

    result = runner.invoke(cmd, [opt])
    assert not result.exception
    assert result.output.splitlines() == [val]