#!/bin/sh # tests/test_diff.sh - Tests for diff applet _test_name="diff" . "$(dirname "$0")/harness.sh" _srcdir="$(cd "$(dirname "$0")/../src/diff/tests" && pwd)" setup_tmpdir DIFF="$VX diff" echo "Running diff tests..." # --- simple: normal, ed, unified, nreverse, brief, ignore-case, whitespace --- check_exit "simple: normal diff" 1 \ $DIFF "$_srcdir/input1.in" "$_srcdir/input2.in" _got="$($DIFF "$_srcdir/input1.in" "$_srcdir/input2.in" 2>/dev/null)" || true _exp="$(cat "$_srcdir/simple.out")" check_eq "$_got" "$_exp" "simple: normal output matches" _got="$($DIFF -e "$_srcdir/input1.in" "$_srcdir/input2.in" 2>/dev/null)" || true _exp="$(cat "$_srcdir/simple_e.out")" check_eq "$_got" "$_exp" "simple: ed output matches" _got="$($DIFF -u -L input1 -L input2 "$_srcdir/input1.in" "$_srcdir/input2.in" 2>/dev/null)" || true _exp="$(cat "$_srcdir/simple_u.out")" check_eq "$_got" "$_exp" "simple: unified output matches" _got="$($DIFF -n "$_srcdir/input1.in" "$_srcdir/input2.in" 2>/dev/null)" || true _exp="$(cat "$_srcdir/simple_n.out")" check_eq "$_got" "$_exp" "simple: nreverse output matches" # brief mode $DIFF -q "$_srcdir/input1.in" "$_srcdir/input2.in" >/dev/null 2>&1 check_eq "$?" "1" "simple: brief exit 1 on different" check_exit "simple: brief exit 0 on identical" 0 \ $DIFF -q "$_srcdir/input1.in" "$_srcdir/input1.in" # ignore case _got="$($DIFF -i "$_srcdir/input_c1.in" "$_srcdir/input_c2.in" 2>/dev/null)" || true _exp="$(cat "$_srcdir/simple_i.out")" check_eq "$_got" "$_exp" "simple: ignore-case output matches" # ignore whitespace _got="$($DIFF -w "$_srcdir/input_c1.in" "$_srcdir/input_c2.in" 2>/dev/null)" || true _exp="$(cat "$_srcdir/simple_w.out")" check_eq "$_got" "$_exp" "simple: ignore-whitespace output matches" # ignore space changes _got="$($DIFF -b "$_srcdir/input_c1.in" "$_srcdir/input_c2.in" 2>/dev/null)" || true _exp="$(cat "$_srcdir/simple_b.out")" check_eq "$_got" "$_exp" "simple: space-change output matches" # --- unified with function names --- _got="$($DIFF -up -L input_c1.in -L input_c2.in "$_srcdir/input_c1.in" "$_srcdir/input_c2.in" 2>/dev/null)" || true _exp="$(cat "$_srcdir/unified_p.out")" check_eq "$_got" "$_exp" "unified: -p output matches" # --- strip trailing CR --- printf 'a\nb\r\nc\n' > cr_a.in printf 'a\r\nb\r\nc\r\n' > cr_b.in check_exit "stripcr: identical after strip" 0 \ $DIFF -up --strip-trailing-cr -L a -L b cr_a.in cr_b.in # --- -B (ignore blank lines) --- printf "A\nB\n" > bflag_a printf "A\n\nB\n" > bflag_b check_exit "Bflag: ignore blank lines" 0 $DIFF -B bflag_a bflag_b # --- -w (ignore all whitespace) --- printf 'a b\n' > ws_a printf 'a b\n' > ws_b check_exit "whitespace: -qw identical" 0 $DIFF -qw ws_a ws_b # --- side-by-side --- printf "A\nB\nC\n" > sby_a printf "D\nB\nE\n" > sby_b $DIFF -y sby_a sby_b > sby_out 2>&1 || true check_grep "B" sby_out "side-by-side: contains common line" check_grep "|" sby_out "side-by-side: contains change marker" # --- brief recursive --- mkdir -p rA rB echo 1 > rA/test-file echo 2 > rB/test-file _got="$($DIFF -rq rA rB 2>/dev/null)" || true check_eq "$_got" "Files rA/test-file and rB/test-file differ" "brief: recursive diff" # --- identical files --- check_exit "identical: exit 0" 0 $DIFF "$_srcdir/input1.in" "$_srcdir/input1.in" # --- Nflag: treat absent files as empty --- printf "foo" > nflag_a check_exit "Nflag: absent second file" 1 $DIFF -N nflag_a NOFILE # --- conflicting format flags --- printf "A\n" > cfmt_a printf "B\n" > cfmt_b check_exit "conflicting: -c -u exits 2" 2 $DIFF -c -u cfmt_a cfmt_b summary