Skip to content

Commit

Permalink
Add tests for --newline option
Browse files Browse the repository at this point in the history
  • Loading branch information
AndydeCleyre committed Oct 3, 2022
1 parent 2af5c3e commit 16ce7ca
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,88 @@ def test_generate_hashes_with_annotations(runner):
)


@pytest.mark.network
@pytest.mark.parametrize("gen_hashes", (True, False))
@pytest.mark.parametrize(
"annotate_options",
(
("--no-annotate",),
("--annotation-style", "line"),
("--annotation-style", "split"),
),
)
@pytest.mark.parametrize(
("nl_options", "must_include", "must_exclude"),
(
pytest.param(("--newline", "lf"), "\n", "\r\n", id="LF"),
pytest.param(("--newline", "crlf"), "\r\n", "\n", id="CRLF"),
pytest.param(
("--newline", "native"),
os.linesep,
{"\n": "\r\n", "\r\n": "\n"}[os.linesep],
id="native",
),
),
)
def test_override_newline(
runner, gen_hashes, annotate_options, nl_options, must_include, must_exclude
):
opts = annotate_options + nl_options
if gen_hashes:
opts += ("--generate-hashes",)

with open("requirements.in", "w") as req_in:
req_in.write("six==1.15.0\n")
req_in.write("setuptools\n")
req_in.write("pip-tools @ git+https://github.com/jazzband/pip-tools\n")

runner.invoke(cli, [*opts, "requirements.in"])
with open("requirements.txt", "rb") as req_txt:
txt = req_txt.read().decode()

assert must_include in txt

if must_exclude in must_include:
txt = txt.replace(must_include, "")
assert must_exclude not in txt

# Do it again, with --newline=preserve:

opts = annotate_options + ("--newline", "preserve")
if gen_hashes:
opts += ("--generate-hashes",)

runner.invoke(cli, [*opts, "requirements.in"])
with open("requirements.txt", "rb") as req_txt:
txt = req_txt.read().decode()

assert must_include in txt

if must_exclude in must_include:
txt = txt.replace(must_include, "")
assert must_exclude not in txt


@pytest.mark.network
@pytest.mark.parametrize(
("linesep", "must_exclude"),
(pytest.param("\n", "\r\n", id="LF"), pytest.param("\r\n", "\n", id="CRLF")),
)
def test_preserve_newline_from_input(runner, linesep, must_exclude):
with open("requirements.in", "wb") as req_in:
req_in.write(f"six{linesep}".encode())

runner.invoke(cli, ["--newline=preserve", "requirements.in"])
with open("requirements.txt", "rb") as req_txt:
txt = req_txt.read().decode()

assert linesep in txt

if must_exclude in linesep:
txt = txt.replace(linesep, "")
assert must_exclude not in txt


@pytest.mark.network
def test_generate_hashes_with_split_style_annotations(runner):
with open("requirements.in", "w") as fp:
Expand Down

0 comments on commit 16ce7ca

Please sign in to comment.