Skip to content

in-place editing: treat each file as a separate input - #569

Merged
sylvestre merged 1 commit into
uutils:mainfrom
SichenLiang:in-place-separate
Sep 25, 2026
Merged

sylvestre merged 1 commit into
uutils:mainfrom
SichenLiang:in-place-separate

Conversation

@SichenLiang

Copy link
Copy Markdown
Contributor

Fixes #567.

GNU documents -i as implying -s, so --in-place now enables the
same per-file handling as --separate.

The second hunk fixes the same problem under -n. Without it,

sed -n -i -e 'N;s/\n/+/;p' f1 f2

can still carry the last line of f1 into f2, because a pending N
is cleared at the end of a file only when automatic output is enabled.

The bug is not specific to -i. With f1 containing a1, a2, a3
and f2 containing b1, b2, b3,

sed -n -s -e 'N;s/\n/+/;p' f1 f2

currently prints a1+a2, a3+b1, b2+b3. GNU prints a1+a2,
b1+b2.

While testing this, I found five other existing differences from GNU
sed that this change does not address:

Expected output in the new tests matches GNU sed 4.9 and 4.10.

Comment thread tests/by-util/test_sed.rs
Ok(())
}

// -i implies -s: each file is edited as a separate stream.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost the same setup code 11 times
could you please add a small helper that creates the two files? :)
or do a loop ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you can still do a loop

like
"input", "arg", "expected"

Comment thread tests/by-util/test_sed.rs Outdated
std::fs::write(&path1, "a1\na2\na3\n")?;
std::fs::write(&path2, "b1\nb2\nb3\n")?;

// Lines collected in the hold space stay in their own file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure about this one?
as far as i know, GNU does not reset the hold space between files, even with -s/-i
so i would expect a3,b1,b2,b3 in the second file

Comment thread tests/by-util/test_sed.rs Outdated
std::fs::write(&path1, "one\n")?;
std::fs::write(&path2, "two\n")?;

// Neither file has a line 2, so Q never runs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't test much if Q never runs
could you please test that q in the first file leaves the second file untouched?

@SichenLiang

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I addressed all three points and folded the changes
into the original commit.

Test setup. I added a two_inputs() helper that returns the temporary
directory along with the two file paths. The directory has to remain alive
until the command runs, or the files are deleted. This removes the repeated
five-line setup from each test, although rustfmt still expands the
.args(&[...]) calls.

Hold space. GNU clears the hold space between files under both -s and
-i. I checked with GNU sed 4.9 and 4.10:

$ printf 'a1\na2\n' > f1; printf 'b1\nb2\n' > f2
$ sed -s -n -e '$h' -e '1{x;s/^/hold=[/;s/$/]/;p}' f1 f2
hold=[]
hold=[]

No additional hold-space change is needed. process_all_files already clears
it when separate is set; this PR makes -i use that existing path. I kept
the test.

Quit. You were right that the old test proved very little. I replaced it
with $Q, which exercises the change. GNU leaves a1\na2 in the first file
and never opens the second file at all. Before this change, the first file is
untouched and the second is truncated to b1\nb2.

I also added the requested 2q case. It confirms that quitting in the first
file leaves the second untouched. That test already passes without the fix,
so it records the existing behavior rather than exercising this change.

There are now 12 tests in this area. Reverting the two source changes makes
11 fail; only the 2q test still passes.

@codecov

codecov Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.84%. Comparing base (c46dd6d) to head (25c9a2d).

Files with missing lines Patch % Lines
src/sed/processor.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #569   +/-   ##
=======================================
  Coverage   82.84%   82.84%           
=======================================
  Files          13       13           
  Lines        6865     6865           
  Branches      401      402    +1     
=======================================
  Hits         5687     5687           
  Misses       1175     1175           
  Partials        3        3           
Flag Coverage Δ
macos_latest 83.97% <50.00%> (ø)
ubuntu_latest 84.19% <50.00%> (ø)
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

sed -i with more than one file treated them as one continuous stream:
line numbers continued across files, $ matched only the last line of
the last file, and N could join lines from different files. GNU sed
documents -i as implying -s, so enable separate-file processing for
--in-place as well as --separate.

There was a second bug when automatic output was suppressed. At the end
of a file, process_file cleared a pending N action only when
!context.quiet. With -n, the action carried into the next file and
joined with its first line. Clear the action in either mode, but write
the pattern space only when automatic output is enabled.

The same bug can be reproduced without -i. With f1 containing a1, a2,
a3 and f2 containing b1, b2, b3:

sed -n -s -e 'N;s/\n/+/;p' f1 f2

Before this change it prints a1+a2, a3+b1, b2+b3. GNU prints a1+a2,
b1+b2.

Add coverage for line numbers, $, ranges, N across file boundaries,
hold space, Q, backup suffixes, and -n.
@SichenLiang

Copy link
Copy Markdown
Contributor Author

Done - the ten two-file cases are now a single table, with the arguments, the two
inputs and the two expected results per row. The assertions name the arguments,
so a failing row still says which one it was.

Two cases stayed separate: the backup test, which also checks the two .bak
files, and the -s one, which checks standard output rather than the files.

This brings the test additions down from 260 lines to 134. Reverting the two
source changes fails all three tests.

@sylvestre
sylvestre merged commit ab2f0a8 into uutils:main Sep 25, 2026
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sed -i treats multiple files as one continuous stream

2 participants