~dricottone/my-utils

ref: cfa246b763c17400a5f078a5733043fa52e5d104 my-utils/src/unittest -rwxr-xr-x 1.5 KiB
cfa246b7Dominic Ricottone Initial commit of cleaned-up dev branch 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh

name="unittest"
version="1.0"
help_message=$(/usr/bin/cat <<-EOF
	Wrapper around Python's 'unittest' module
	Usage: unittest TARGET [OPTIONS]
	Options:
	 -c, --color       print with color
	 -h, --help        print this message and exit
	 -v, --verbose     show verbose 'unittest' output
	 -V, --version     print version number and exit
EOF
)

target=""
color=0
verbose=0
python_executable="/usr/bin/env python3"
for arg; do
  case "$arg" in
  -c|--color)
    color=1
    ;;

  -h|--help)
    printf "%s\n" "$help_message"
    exit 0
    ;;

  -v|--verbose)
    verbose=1
    ;;

  -V|--version)
    printf "%s %s\n" "$name" "$version"
    exit 0
    ;;

  *)
    if [ -z "$target" ]; then
      target="$arg"
    fi
    ;;
  esac
done

# unittest subroutine
unittest_subroutine() {
  if [ "$verbose" -eq 1 ]; then
    if ! $python_executable -m unittest discover -v -s "$target" 2>&1; then
      code=1
    fi
  else
    if ! $python_executable -m unittest discover -s "$target" 2>&1 | /usr/bin/tail -n 1; then
      code=1
    fi
  fi
}

# check directory
if [ -z "${target+x}" ]; then
  (>&2 printf "Usage: unittest TARGET [OPTIONS]")
  exit 1
elif [ ! -d "$target" ]; then
  (>&2 printf "%s: No such directory '%s'\n" "$name" "$target")
  exit 1
fi

# main routine
code=0
if [ "$color" -eq 0 ] || { [ -n "${NO_COLOR+x}" ] && [ "$NO_COLOR" -eq 1 ]; }; then
  unittest_subroutine
else
  unittest_subroutine | /usr/local/lib/unittest-color.awk
fi

# return stored code
exit "$code"