Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/sed/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub struct ProcessingContext {
pub substitution_made: bool,
/// Elements to append at the end of each command processing cycle
pub append_elements: Vec<AppendElement>,
/// True if a delete command was executed (prevents automatic printing)
pub pattern_deleted: bool,
}

#[derive(Clone, Debug)]
Expand Down
1 change: 1 addition & 0 deletions src/sed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ fn build_context(matches: &ArgMatches) -> ProcessingContext {
label_to_command_map: HashMap::new(),
substitution_made: false,
append_elements: Vec::new(),
pattern_deleted: false,
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/sed/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ fn process_file(
context.last_line = last_line;
context.line_number += 1;
context.substitution_made = false;
context.pattern_deleted = false;
// Set the script command from which to start.
let mut current: Option<Rc<RefCell<Command>>> =
if let Some(action) = context.input_action.take() {
Expand Down Expand Up @@ -494,6 +495,7 @@ fn process_file(
// At range end replace pattern space with text and
// start the next cycle.
pattern.clear();
context.pattern_deleted = true;
if command.addr2.is_none() || context.last_address || context.last_line {
let text = extract_variant!(command, Text);
output.write_str(text.as_ref())?;
Expand All @@ -503,6 +505,7 @@ fn process_file(
'd' => {
// Delete the pattern space and start the next cycle.
pattern.clear();
context.pattern_deleted = true;
break;
}
'D' => {
Expand All @@ -515,6 +518,7 @@ fn process_file(
} else {
// Same as d
pattern.clear();
context.pattern_deleted = true;
break;
}
}
Expand Down Expand Up @@ -652,7 +656,7 @@ fn process_file(
current = command.next.clone();
}

if !context.quiet {
if !context.quiet && !context.pattern_deleted {
write_chunk(output, context, &pattern)?;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,3 +1128,45 @@ fn test_print_command_adds_newline() {
.succeeds()
.stdout_is("foo\nfoo");
}

////////////////////////////////////////////////////////////
// Test for delete command preventing automatic pattern printing
#[test]
fn test_delete_command_prevents_automatic_printing() {
// Test 'd' command - delete line 2
new_ucmd!()
.args(&["2d"])
.pipe_in("line1\nline2\nline3")
.succeeds()
.stdout_is("line1\nline3");
}

#[test]
fn test_delete_range_prevents_automatic_printing() {
// Test 'd' command on range - delete lines 2-3
new_ucmd!()
.args(&["2,3d"])
.pipe_in("line1\nline2\nline3\nline4")
.succeeds()
.stdout_is("line1\nline4");
}

#[test]
fn test_change_command_prevents_automatic_printing() {
// Test 'c' command - change line 2
new_ucmd!()
.args(&["2c\\replaced"])
.pipe_in("line1\nline2\nline3")
.succeeds()
.stdout_is("line1\nreplaced\nline3");
}

#[test]
fn test_uppercase_delete_prevents_automatic_printing() {
// Test 'D' command - delete up to newline and restart
new_ucmd!()
.args(&["-e", "N", "-e", "D"])
.pipe_in("line1\nline2\nline3")
.succeeds()
.stdout_is("line3\n");
}
44 changes: 30 additions & 14 deletions util/run-gnu-testsuite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ run_sed_test() {
local input_text="$3"
local expected_output="$4"
local flags="$5"
local interpret_escapes="${6:-true}" # Default to true for basic tests

TOTAL_TESTS=$((TOTAL_TESTS + 1))

Expand All @@ -232,31 +233,46 @@ run_sed_test() {
cd "$test_dir"

# Write input to file
echo -n "$input_text" > input.txt
echo -n "$expected_output" > expected.txt
if [[ "$interpret_escapes" == "true" ]]; then
echo -e "$input_text" > input.txt
echo -e "$expected_output" > expected.txt
else
echo -n "$input_text" > input.txt
echo -n "$expected_output" > expected.txt
fi

# Run Rust sed
local rust_exit_code=0
local rust_output=""
if [[ -n "$flags" ]]; then
rust_output=$("$RUST_SED_BIN" "$flags" "$sed_script" input.txt 2>/dev/null) || rust_exit_code=$?
else
rust_output=$(echo -n "$input_text" | "$RUST_SED_BIN" "$sed_script" 2>/dev/null) || rust_exit_code=$?
if [[ "$interpret_escapes" == "true" ]]; then
rust_output=$(echo -e "$input_text" | "$RUST_SED_BIN" "$sed_script" 2>/dev/null) || rust_exit_code=$?
else
rust_output=$(echo -n "$input_text" | "$RUST_SED_BIN" "$sed_script" 2>/dev/null) || rust_exit_code=$?
fi
fi

local test_result=""
local test_status=""
local error_message=""

# Compare with expected output
if [[ "$rust_output" == "$expected_output" && $rust_exit_code -eq 0 ]]; then
local expected_interpreted
if [[ "$interpret_escapes" == "true" ]]; then
expected_interpreted=$(echo -e "$expected_output")
else
expected_interpreted="$expected_output"
fi
if [[ "$rust_output" == "$expected_interpreted" && $rust_exit_code -eq 0 ]]; then
log_success "$test_name"
PASSED_TESTS=$((PASSED_TESTS + 1))
test_status="PASS"
else
log_error "$test_name"
if [[ "$VERBOSE" == "true" ]]; then
echo " | Expected: '$expected_output'"
echo " | Expected: '$expected_interpreted'"
echo " | Got: '$rust_output' (exit: $rust_exit_code)"
fi
FAILED_TESTS=$((FAILED_TESTS + 1))
Expand Down Expand Up @@ -301,15 +317,15 @@ run_basic_tests() {
run_sed_test "delete_range" "2,3d" "line1\nline2\nline3\nline4" "line1\nline4"

# Print command
run_sed_test "print_line" "-n 2p" "line1\nline2\nline3" "line2" "-n"
run_sed_test "print_line" "2p" "line1\nline2\nline3" "line2" "-n"

# Append and insert
run_sed_test "append" "2a\\inserted" "line1\nline2\nline3" "line1\nline2\ninserted\nline3"
run_sed_test "insert" "2i\\inserted" "line1\nline2\nline3" "line1\ninserted\nline2\nline3"

# Character classes
run_sed_test "digit_class" "s/[0-9]/X/g" "abc123def" "abcXXXdef"
run_sed_test "word_class" "s/[a-z]/X/g" "Hello123" "XXXXX123"
run_sed_test "word_class" "s/[a-z]/X/g" "Hello123" "HXXXX123"
}

# Run tests from specific GNU testsuite files that have .inp/.good/.sed triplets
Expand All @@ -334,7 +350,7 @@ run_gnu_testsuite_tests() {
expected_content=$(cat "$good_file")

log_verbose "Found complete triplet: $basename"
run_sed_test "${basename}_triplet" "$sed_file" "$input_content" "$expected_content" "-f"
run_sed_test "${basename}_triplet" "-f $sed_file" "$input_content" "$expected_content" "-f" "false"
tests_found=$((tests_found + 1))
fi
fi
Expand Down Expand Up @@ -427,7 +443,7 @@ extract_sed_commands_from_script() {
local expected_content
input_content=$(cat "$input_file")
expected_content=$(cat "$expected_file")
run_sed_test "${basename}_extracted" "$sed_script" "$input_content" "$expected_content"
run_sed_test "${basename}_extracted" "$sed_script" "$input_content" "$expected_content" "" "false"
fi
fi
done < "$script_file"
Expand Down Expand Up @@ -457,7 +473,7 @@ extract_simple_tests_from_script() {
local input_text="${BASH_REMATCH[1]}"
local sed_script="${BASH_REMATCH[2]}"
if [[ -n "$input_text" && -n "$sed_script" && ${#sed_script} -lt 80 ]]; then
run_sed_test "${basename}_echo_${extracted_count}" "$sed_script" "$input_text" "" ""
run_sed_test "${basename}_echo_${extracted_count}" "$sed_script" "$input_text" "" "" "false"
extracted_count=$((extracted_count + 1))
continue
fi
Expand All @@ -467,7 +483,7 @@ extract_simple_tests_from_script() {
if [[ $line =~ sed[[:space:]]+-e[[:space:]]+[\'\"]([^\'\"]+)[\'\"] ]] && [[ $extracted_count -lt 2 ]]; then
local sed_script="${BASH_REMATCH[1]}"
if [[ -n "$sed_script" && ${#sed_script} -lt 80 ]]; then
run_sed_test "${basename}_dash_e_${extracted_count}" "$sed_script" "line1\nline2\nline3" "" ""
run_sed_test "${basename}_dash_e_${extracted_count}" "$sed_script" "line1\nline2\nline3" "" "" "true"
extracted_count=$((extracted_count + 1))
continue
fi
Expand All @@ -480,7 +496,7 @@ extract_simple_tests_from_script() {
if [[ -n "$pattern" && ${#pattern} -lt 30 ]]; then
local input_text="This is $pattern in text"
local sed_script="s/$pattern/$replacement/"
run_sed_test "${basename}_subst_${extracted_count}" "$sed_script" "$input_text" "" ""
run_sed_test "${basename}_subst_${extracted_count}" "$sed_script" "$input_text" "" "" "true"
extracted_count=$((extracted_count + 1))
continue
fi
Expand All @@ -497,7 +513,7 @@ extract_simple_tests_from_script() {
continue
;;
*)
run_sed_test "${basename}_cmd_${extracted_count}" "$sed_script" "test\ndata\nline" "" ""
run_sed_test "${basename}_cmd_${extracted_count}" "$sed_script" "test\ndata\nline" "" "" "true"
extracted_count=$((extracted_count + 1))
;;
esac
Expand All @@ -511,7 +527,7 @@ extract_simple_tests_from_script() {
if [[ -n "$input_text" && -n "$sed_script" && ${#sed_script} -lt 60 ]]; then
# Convert \n to actual newlines
input_text=$(echo -e "$input_text")
run_sed_test "${basename}_printf_${extracted_count}" "$sed_script" "$input_text" "" ""
run_sed_test "${basename}_printf_${extracted_count}" "$sed_script" "$input_text" "" "" "false"
extracted_count=$((extracted_count + 1))
continue
fi
Expand Down
Loading