Commit 0af1494b authored by Kevin Daudt's avatar Kevin Daudt Committed by Leo

initd-lint: add linting tool for openrc initd files

parent 58fa234c
exec >&2
echo ">>> Testing apkbuild-lint"
bats --tap tests/apkbuild-lint.bats
echo ">>> Testing initd-lint"
bats --tap tests/initd-lint.bats
#!/bin/sh
lint_msg() {
local msg="$1" tag="$2" severity="$3"
printf '%s:[%s]:%s:%s\n' "$severity" "$tag" "$service" "$msg"
}
scan() {
local rx="$1" msg="$2" tag="$3" severity="$4"
grep -P -Hn -e "$rx" "$service" |
sed "s/^\([^:]*:[^:]*:\)\(.*\)/$severity:[$tag]:\1$msg/"
}
unexpected_shebang_line() {
shebang=$(awk 'NR == 1' "$service")
if [ "$shebang" != "#!/sbin/openrc-run" ]; then
lint_msg "openrc init files should use '#!/sbin/openrc-run' as shebang line" "AL33" "IC"
fi
}
custom_start_stop_function() {
scan '(start|stop)\s*\(\)\s*\{' "Don't define a custom start\/stop function" 'AL34' 'IC'
}
_ret=0
for service; do
if [ ! -f "$service" ]; then
echo "error: no such file: '$service'"
exit 1
fi
unexpected_shebang_line
custom_start_stop_function
done | sort -t: -V | grep . && _ret=1
exit $_ret
#!/usr/bin/env bats
cmd=./initd-lint
service=$BATS_TMPDIR/service.initd
assert_match() {
output=$1
expected=$2
echo "$output" | grep -qE "$expected"
}
@test 'warn about non-standard shebang' {
cat <<-"EOF" >$service
#!/bin/sh
cmd_run="my_service"
EOF
run $cmd $service
[[ $status -eq 1 ]]
assert_match "${lines[0]}" "should use '#!/sbin/openrc-run'"
}
@test "don't warn about standard shebang" {
cat <<-"EOF" >$service
#!/sbin/openrc-run
cmd_run="my_service"
EOF
run $cmd $service
[[ $status -eq 0 ]]
}
@test "warn about custom start function" {
cat <<-"EOF" >$service
#!/sbin/openrc-run
start() {
start_command
}
EOF
run $cmd $service
[[ $status -eq 1 ]]
assert_match "${lines[0]}" "custom start/stop function"
}
@test "warn about custom stop function" {
cat <<-"EOF" >$service
#!/sbin/openrc-run
stop () {
stop_command
}
EOF
run $cmd $service
[[ $status -eq 1 ]]
assert_match "${lines[0]}" "custom start/stop function"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment