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" "cmp: EOF on input2\n" "ab\nc\nx" ""
  26testcmd "EOF, return code" "input input2 2>/dev/null || echo yes" "yes\n" "ab\nc\nx" ""
  27# The gnu/dammit version fails this because posix says "char" and they don't.
  28testcmd "diff, stdout" "input input2 | sed s/byte/char/" \
  29  "input input2 differ: char 4, line 2\n" "ab\nx\nx" ""
  30testcmd "diff, return code" "input input2 > /dev/null || echo yes" "yes\n" "ab\nx\nx" ""
  31
  32testcmd "-s EOF, return code" "-s input input2 2>&1 || echo yes"  "yes\n" "ab\nc\nx" ""
  33testcmd "-s diff, return code" "-s input input2 2>&1 || echo yes" "yes\n" "ab\nx\nx" ""
  34
  35testcmd "-l EOF, stderr" "-l input input2 2>&1" "cmp: EOF on input2\n" "ab\nc\nx" ""
  36testcmd "-l diff and EOF, stdout and stderr" "-l input input2 2>&1 | sort" "4 170 143\ncmp: EOF on input2\n" "ab\nx\nx" ""
  37
  38testcmd "-s not exist" "-s input doesnotexist 2>&1 || echo yes" "yes\n" "x" ""
  39
  40rm input2
  41
  42testcmd "stdin and file" "input - | sed s/byte/char/" \
  43  "input - differ: char 4, line 2\n" "ab\nc\n" "ab\nx\n"
  44testcmd "-n skip1 skip2" "-n 3 input - 3 5 && echo yes" "yes\n" "abcdef123" "vwxyzdef987"
  45
  46