-
Notifications
You must be signed in to change notification settings - Fork 4
/
test
executable file
·133 lines (103 loc) · 3.4 KB
/
test
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
131
132
133
#!/bin/bash
# dotfiles/test
# test
# Find shell scripts and run shellcheck on them
# Adapted from jessfraz/dotfiles/bin/test.sh
# https://github.com/jessfraz/dotfiles/blob/master/test.sh
set -euo pipefail
# -e exit if any command returns non-zero status code
# -u prevent using undefined variables
# -o pipefail force pipelines to fail on first non-zero status code
/usr/bin/tput sgr0;
# reset colors
function check_shellcheck {
# check_shellcheck
# Check if shellcheck is installed
if ! [[ -x "$(command -v shellcheck)" ]]; then
echo "[FAIL] shellcheck not installed"
echo "[INFO] macOS: brew install shellcheck"
echo "[INFO] Linux: apt install shellcheck"
exit 1
fi
}
function find_files {
# find_files
# Find all regular files in source directory
while IFS=$'\n' read -r file; do
FILES+=("${file}");
done < <(/usr/bin/find . -maxdepth 1 -type f \
-not -iwholename '*.git*' \
-not -iwholename '*venv*' \
-not -iwholename '*.tar.xz' \
| /usr/bin/sort -u)
}
function analyse_shell_scripts {
# analyse_shell_scripts
# Iterate over $FILES to find bash and zsh scripts
# Call shellcheck on them via analyse function
for file in "${FILES[@]}"; do
if /usr/bin/file "${file}" | /usr/bin/grep --quiet "shell" || \
/usr/bin/file "${file}" | /usr/bin/grep --quiet "bash" ; then
# Find bash scripts
# Running file on a script with the shebang "#!/usr/bin/env bash" returns
# "a /usr/bin/env bash script, ASCII text executable"
# Versus a script with the shebang "#!/bin/bash" which returns
# "Bourne-Again shell script, ASCII text executable"
analyse "${file}"
elif /usr/bin/file "${file}" | /usr/bin/grep --quiet "zsh"; then
# Find zsh scripts
# Running file on a script with shebang "#!/usr/bin/env zsh" returns
# "a /usr/bin/env zsh script text executable"
analyse "${file}"
fi
done
}
function analyse {
# analyse
# Wrapper for shellcheck to handle errors
# Always invokes shellcheck in bash mode
# as shellcheck does not support zsh
local shell_file=${1:?shell_file not passed to lint_shell_file}
if shellcheck --shell=bash "${shell_file}" ; then
# Run shellcheck on the file
# Always uses bash mode as bittersweet now has a zsh shebang
# Shelllcheck doesn't support zsh.
echo "[PASS] $(/usr/bin/basename "${shell_file}")"
else
echo "[FAIL] $(/usr/bin/basename "${shell_file}")"
ERRORS+=("${shell_file}")
# If shellcheck fails add failing file name to array
fi
}
function lint_plist_mobileconfig {
local -a FILES
while IFS=$'\n' read -r file; do
FILES+=("${file}");
done < <(/usr/bin/find . -maxdepth 4 \
-type f \
\( -name "*.plist" -o -name "*.mobileconfig" \))
for file in "${FILES[@]}"; do
if ! /usr/bin/plutil -lint "${file}" ; then
echo "[FAIL] $(/usr/bin/basename "${file}")"
ERRORS+=("${file}")
fi
done
}
function main {
check_shellcheck
declare -a ERRORS
declare -a FILES
find_files
analyse_shell_scripts
lint_plist_mobileconfig
if [[ ${#ERRORS[@]} -eq 0 ]]; then
# If ERRORS empty then
echo "[PASS] No errors, hooray"
exit 0
else
# If ERRORS not empty, print the names of files which failed
echo "[FAIL] These files failed linting: ${ERRORS[*]}"
exit 1
fi
}
main "$@"