Skip to content

Commit

Permalink
Merge pull request #253 from seberg/fix-multiline-generate-diff
Browse files Browse the repository at this point in the history
BUG: Fix multiline code in generate/update diff
  • Loading branch information
bsipocz authored Jun 27, 2024
2 parents b54e7ac + 680c6f4 commit 99350f8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1.3.0 (unreleased)
==================

- Fixing output update for multiline code. [#253]

1.2.1 (2024-03-09)
==================
Expand Down
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

0 comments on commit 99350f8

Please sign in to comment.