-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_cli.py
83 lines (60 loc) · 2.04 KB
/
test_cli.py
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
"""Test the command line interface."""
import re
from textwrap import dedent
import pytest
from click.testing import CliRunner
from autoimport.entrypoints.cli import cli
from autoimport.version import __version__
@pytest.fixture(name="runner")
def fixture_runner() -> CliRunner:
"""Configure the Click cli test runner."""
return CliRunner(mix_stderr=False)
def test_version(runner: CliRunner) -> None:
"""Prints program version when called with --version."""
result = runner.invoke(cli, ["--version"])
assert result.exit_code == 0
assert re.match(
fr" *autoimport version: {__version__}\n"
r" *python version: .*\n *platform: .*",
result.stdout,
)
def test_corrects_one_file(runner: CliRunner, tmpdir) -> None:
"""Correct the source code of a file."""
test_file = tmpdir.join("source.py")
test_file.write("os.getcwd()")
fixed_source = dedent(
"""\
import os
os.getcwd()"""
)
result = runner.invoke(cli, [str(test_file)])
assert result.exit_code == 0
assert test_file.read() == fixed_source
@pytest.mark.secondary
def test_corrects_three_files(runner: CliRunner, tmpdir) -> None:
"""Correct the source code of multiple files."""
test_files = []
for file_number in range(3):
test_file = tmpdir.join(f"source_{file_number}.py")
test_file.write("os.getcwd()")
test_files.append(test_file)
fixed_source = dedent(
"""\
import os
os.getcwd()"""
)
result = runner.invoke(cli, [str(test_file) for test_file in test_files])
assert result.exit_code == 0
for test_file in test_files:
assert test_file.read() == fixed_source
def test_corrects_code_from_stdin(runner: CliRunner) -> None:
"""Correct the source code passed as stdin."""
source = "os.getcwd()"
fixed_source = dedent(
"""\
import os
os.getcwd()"""
)
result = runner.invoke(cli, ["-"], input=source)
assert result.exit_code == 0
assert result.stdout == fixed_source