toybox/tests/cmp.test
<<
>>
Prefs
   1#!/bin/bash
   2
   3[ -f testing.sh ] && . testing.sh
   4
   5# TODO: coreutils cmp uses stdin if only one file is given
   6testcmd "one argument match" 'input && echo yes' "yes\n" \
   7  "one\ntwo\nthree" "one\ntwo\nthree"
   8# posix says ""%s %s differ: char %d, line %d\n" but diffutils says "byte"
   9testcmd "one argument diff" 'input | sed s/byte/char/' \
  10  "input - differ: char 5, line 2\n" "one\ntwo\nthree" "one\nboing\nthree"
  11
  12testcmd "missing file1 [fail]" 'file1 input 2>/dev/null || echo $?' "2\n" "foo" ""
  13
  14#mkdir dir
  15#testing "directory [fail]" "cmp dir dir 2>/dev/null || echo yes" \
  16        #"yes\n" "" ""
  17#rmdir dir
  18
  19echo "ab
  20c" > input2
  21
  22testcmd "identical files, stdout" "input input2" "" "ab\nc\n" ""
  23testcmd "identical files, return code" "input input2 && echo yes" "yes\n" "ab\nc\n" ""
  24
  25testcmd "EOF, stderr" "input input2 2>&1" \
  26  "cmp: EOF on input2 after byte 5, line 2\n" "ab\nc\nx" ""
  27testcmd "EOF, return code" "input input2 2>/dev/null || echo yes" "yes\n" "ab\nc\nx" ""
  28# The gnu/dammit version fails this because posix says "char" and they don't.
  29testcmd "diff, stdout" "input input2 | sed s/byte/char/" \
  30  "input input2 differ: char 4, line 2\n" "ab\nx\nx" ""
  31testcmd "diff, return code" "input input2 > /dev/null || echo yes" "yes\n" "ab\nx\nx" ""
  32
  33testcmd "-s EOF, return code" "-s input input2 2>&1 || echo yes"  "yes\n" "ab\nc\nx" ""
  34testcmd "-s diff, return code" "-s input input2 2>&1 || echo yes" "yes\n" "ab\nx\nx" ""
  35
  36testcmd "-l EOF, stderr" "-l input input2 2>&1" \
  37  "cmp: EOF on input2 after byte 5\n" "ab\nc\nx" ""
  38testcmd "-l diff and EOF, stdout and stderr" "-l input input2 2>&1 | sort" \
  39  "4 170 143\ncmp: EOF on input2 after byte 5\n" "ab\nx\nx" ""
  40
  41testcmd "-s not exist" "-s input doesnotexist 2>&1 || echo yes" "yes\n" "x" ""
  42
  43rm input2
  44
  45testcmd "stdin and file" "input - | sed s/byte/char/" \
  46  "input - differ: char 4, line 2\n" "ab\nc\n" "ab\nx\n"
  47testcmd "-n skip1 skip2" "-n 3 input - 3 5 && echo yes" "yes\n" "abcdef123" "vwxyzdef987"
  48
  49