Skip to content

Commit

Permalink
Prefer capsys instead of capfd
Browse files Browse the repository at this point in the history
capfd was used to workaround a compatibility issue between Python 2,
Windows and Click. With Python 2 gone, can use capsys.
  • Loading branch information
jdufresne committed Dec 31, 2020
1 parent 53dec66 commit acd27fe
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 4 additions & 4 deletions tests/test_repository_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@


def test_get_hashes_local_repository_cache_miss(
capfd, pip_conf, from_line, pypi_repository
capsys, pip_conf, from_line, pypi_repository
):
existing_pins = {}
local_repository = LocalRequirementsRepository(existing_pins, pypi_repository)
with local_repository.allow_all_wheels():
hashes = local_repository.get_hashes(from_line("small-fake-a==0.1"))
assert hashes == EXPECTED
captured = capfd.readouterr()
captured = capsys.readouterr()
assert captured.out == ""
assert (
captured.err.strip()
Expand Down Expand Up @@ -45,7 +45,7 @@ def test_get_hashes_local_repository_cache_hit(from_line, repository):
("reuse_hashes", "expected"), ((True, NONSENSE), (False, EXPECTED))
)
def test_toggle_reuse_hashes_local_repository(
capfd, pip_conf, from_line, pypi_repository, reuse_hashes, expected
capsys, pip_conf, from_line, pypi_repository, reuse_hashes, expected
):
# Create an install requirement with the hashes included in its options
options = {"hashes": {"sha256": [entry.split(":")[1] for entry in NONSENSE]}}
Expand All @@ -57,7 +57,7 @@ def test_toggle_reuse_hashes_local_repository(
)
with local_repository.allow_all_wheels():
assert local_repository.get_hashes(from_line("small-fake-a==0.1")) == expected
captured = capfd.readouterr()
captured = capsys.readouterr()
assert captured.out == ""
if reuse_hashes:
assert captured.err == ""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_repository_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from piptools.repositories.pypi import open_local_or_remote_file


def test_generate_hashes_all_platforms(capfd, pip_conf, from_line, pypi_repository):
def test_generate_hashes_all_platforms(capsys, pip_conf, from_line, pypi_repository):
expected = {
"sha256:8d4d131cd05338e09f461ad784297efea3652e542c5fabe04a62358429a6175e",
"sha256:ad05e1371eb99f257ca00f791b755deb22e752393eb8e75bc01d651715b02ea9",
Expand All @@ -20,7 +20,7 @@ def test_generate_hashes_all_platforms(capfd, pip_conf, from_line, pypi_reposito
ireq = from_line("small-fake-multi-arch==0.1")
with pypi_repository.allow_all_wheels():
assert pypi_repository.get_hashes(ireq) == expected
captured = capfd.readouterr()
captured = capsys.readouterr()
assert captured.out == ""
assert (
captured.err.strip()
Expand Down
12 changes: 6 additions & 6 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,14 @@ def test_sync_ask_declined(
to_install = {from_line(pkg) for pkg in to_install}
sync(to_install, to_uninstall, ask=True)

out, err = capsys.readouterr()
assert out.splitlines() == [
captured = capsys.readouterr()
assert captured.out.splitlines() == [
expected_message,
" click==4.0",
" django==1.8",
"Would you like to proceed with these changes? [y/N]: ",
]
assert err == ""
assert captured.err == ""
run.assert_not_called()


Expand All @@ -457,15 +457,15 @@ def test_sync_ask_accepted(run, monkeypatch, capsys, from_line, dry_run):
)

assert run.call_count == 2
out, err = capsys.readouterr()
assert out.splitlines() == [
captured = capsys.readouterr()
assert captured.out.splitlines() == [
"Would uninstall:",
" click==4.0",
"Would install:",
" django==1.8",
"Would you like to proceed with these changes? [y/N]: ",
]
assert err == ""
assert captured.err == ""


@mock.patch("piptools.sync.run")
Expand Down
8 changes: 4 additions & 4 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_iter_lines__unsafe_dependencies(writer, from_line, allow_unsafe):
assert tuple(lines) == expected_lines


def test_iter_lines__unsafe_with_hashes(capfd, writer, from_line):
def test_iter_lines__unsafe_with_hashes(capsys, writer, from_line):
writer.allow_unsafe = False
writer.emit_header = False
ireqs = [from_line("test==1.2")]
Expand All @@ -134,12 +134,12 @@ def test_iter_lines__unsafe_with_hashes(capfd, writer, from_line):
comment("# setuptools"),
)
assert tuple(lines) == expected_lines
captured = capfd.readouterr()
captured = capsys.readouterr()
assert captured.out == ""
assert captured.err.strip() == MESSAGE_UNINSTALLABLE


def test_iter_lines__hash_missing(capfd, writer, from_line):
def test_iter_lines__hash_missing(capsys, writer, from_line):
writer.allow_unsafe = False
writer.emit_header = False
ireqs = [from_line("test==1.2"), from_line("file:///example/#egg=example")]
Expand All @@ -153,7 +153,7 @@ def test_iter_lines__hash_missing(capfd, writer, from_line):
"test==1.2 \\\n --hash=FAKEHASH",
)
assert tuple(lines) == expected_lines
captured = capfd.readouterr()
captured = capsys.readouterr()
assert captured.out == ""
assert captured.err.strip() == MESSAGE_UNINSTALLABLE

Expand Down

0 comments on commit acd27fe

Please sign in to comment.