~dricottone/my-utils

my-utils/core/unittest -rwxr-xr-x 2.5 KiB
ef6d4cd1Dominic Ricottone Incomplete work on hardware/media utils 4 days 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh

name="unittest"
version="1.1"
help_message=$(/usr/bin/cat <<-EOF
	Wrapper around Python's 'unittest' module
	Usage: unittest [OPTIONS] TARGET
	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
	 -d, --working-directory  working directory for tests
EOF
)

target=""
single_target=0
color=0
verbose=0
working_directory="."
python_executable="/usr/bin/env python3"

while [ "$#" -gt 0 ]; do
  case "$1" in
  -c|--color)
    color=1
    shift
    ;;

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

  -d|--working-directory)
    working_directory="$2"
    shift
    shift
    ;;

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

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

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

# unittest subroutine
unittest_subroutine() {
  if [ "$single_target" -eq 1 ]; then
    unittest_single_subroutine
  else
    unittest_discover_subroutine
  fi
}

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

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

error_msg() {
  (>&2 /usr/bin/printf "%s: %s\n" "$name" "$1")
}

# check directory
if [ -z "${target+x}" ]; then
  (>&2 /usr/bin/printf "Usage: unittest TARGET [OPTIONS]\n")
  exit 1
elif [ ! -d "$target" ]; then
  if /usr/bin/printf "%s\n" "$target" | /usr/bin/grep -q -e ".*\.py"; then
    single_target=1
  else
    error_msg "No such directory '${target}'"
    exit 1
  fi
elif [ ! -d "$working_directory" ]; then
  error_msg "No such directory '${working_directory}'"
  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"