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
   6testing "one argument match" 'cmp 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"
   9testing "one argument diff" 'cmp input | sed s/byte/char/' \
  10  "input - differ: char 5, line 2\n" "one\ntwo\nthree" "one\nboing\nthree"
  11
  12testing "missing file1 [fail]" 'cmp 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
  22testing "identical files, stdout" "cmp input input2" "" "ab\nc\n" ""
  23testing "identical files, return code" "cmp input input2 && echo yes" "yes\n" "ab\nc\n" ""
  24
  25testing "EOF, stderr" "cmp input input2 2>&1" "cmp: EOF on input2\n" "ab\nc\nx" ""
  26testing "EOF, return code" "cmp 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.
  28testing "diff, stdout" "cmp input input2 | sed s/byte/char/" \
  29  "input input2 differ: char 4, line 2\n" "ab\nx\nx" ""
  30testing "diff, return code" "cmp input input2 > /dev/null || echo yes" "yes\n" "ab\nx\nx" ""
  31
  32testing "-s EOF, return code" "cmp -s input input2 2>&1 || echo yes"  "yes\n" "ab\nc\nx" ""
  33testing "-s diff, return code" "cmp -s input input2 2>&1 || echo yes" "yes\n" "ab\nx\nx" ""
  34
  35testing "-l EOF, stderr" "cmp -l input input2 2>&1" "cmp: EOF on input2\n" "ab\nc\nx" ""
  36testing "-l diff and EOF, stdout and stderr" "cmp -l input input2 2>&1 | sort" "4 170 143\ncmp: EOF on input2\n" "ab\nx\nx" ""
  37
  38testing "-s not exist" "cmp -s input doesnotexist 2>&1 || echo yes" "yes\n" "" ""
  39
  40rm input2
  41
  42testing "stdin and file" "cmp input - | sed s/byte/char/" \
  43  "input - differ: char 4, line 2\n" "ab\nc\n" "ab\nx\n"
  44#testing "stdin and stdin" "cmp input -" "" "" "ab\nc\n"
  45
  46