4 # Copyright 2017 The Rust Project Developers. See the COPYRIGHT
5 # file at the top-level directory of this distribution and at
6 # http://rust-lang.org/COPYRIGHT.
8 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
9 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
10 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
11 # option. This file may not be copied, modified, or distributed
12 # except according to those terms.
14 # Performs `cat` and `grep` simultaneously for `run-make` tests in the Rust CI.
16 # This program will read lines from stdin and print them to stdout immediately.
17 # At the same time, it will check if the input line contains the substring or
18 # regex specified in the command line. If any match is found, the program will
19 # set the exit code to 0, otherwise 1.
21 # This is written to simplify debugging runmake tests. Since `grep` swallows all
22 # output, when a test involving `grep` failed, it is impossible to know the
23 # reason just by reading the failure log. While it is possible to `tee` the
24 # output into another stream, it becomes pretty annoying to do this for all test
28 cat-and-grep.sh [-v] [-e] [-i] s1 s2 s3 ... < input.txt
30 Prints the stdin, and exits successfully only if all of `sN` can be found in
31 some lines of the input.
34 -v Invert match, exits successfully only if all of `sN` cannot be found
35 -e Regex search, search using extended Regex instead of fixed string
36 -i Case insensitive search.
42 while getopts ':vieh' OPTION; do
46 ERROR_MSG='should not be found'
49 GREPFLAGS="i$GREPFLAGS"
66 # use gnu version of tool if available (for bsd)
67 if command -v "g${GREPPER}"; then
71 LOG=$(mktemp -t cgrep.XXXXXX)
72 trap "rm -f $LOG" EXIT
74 printf "[[[ begin stdout ]]]\n\033[90m"
76 echo >> "$LOG" # ensure at least 1 line of output, otherwise `grep -v` may unconditionally fail.
77 printf "\033[0m\n[[[ end stdout ]]]\n"
81 if "$GREPPER" "-$GREPFLAGS" -- "$MATCH" "$LOG"; then
82 if [ "$INVERT" = 1 ]; then
83 printf "\033[1;31mError: should not match: %s\033[0m\n" "$MATCH"
87 if [ "$INVERT" = 0 ]; then
88 printf "\033[1;31mError: cannot match: %s\033[0m\n" "$MATCH"