Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix multiline code in generate/update diff #253

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pytest_doctestplus/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,9 @@ def write_modified_file(fname, new_fname, changes):
if change["test_lineno"] is None:
bad_tests.append(change["name"])
continue
lineno = change["test_lineno"] + change["example_lineno"] + 1
# Find the first line of the output:
lineno = change["test_lineno"] + change["example_lineno"]
lineno += change["source"].count("\n")

indentation = " " * change["nindent"]
want = indent(change["want"], indentation, lambda x: True)
Expand Down
42 changes: 42 additions & 0 deletions tests/test_doctestplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -1431,3 +1431,45 @@ def f():
result = f.read()

assert result == original.replace("4", "2").replace("5", "3")


def test_generate_diff_multiline(testdir, capsys):
p = testdir.makepyfile("""
def f():
'''
>>> print(2)
2
>>> for i in range(4):
... print(i)
1
2
'''
pass
""")
with open(p) as f:
original = f.read()

testdir.inline_run(p, "--doctest-plus-generate-diff")
diff = dedent("""
>>> for i in range(4):
... print(i)
+ 0
1
2
+ 3
""")
captured = capsys.readouterr()
print(captured.out)
print("====")
print(diff)
assert diff in captured.out

testdir.inline_run(p, "--doctest-plus-generate-diff=overwrite")
captured = capsys.readouterr()
assert "Applied fix to the following files" in captured.out

with open(p) as f:
result = f.read()

original_fixed = original.replace("1\n 2", "\n ".join(["0", "1", "2", "3"]))
assert result == original_fixed