Skip to content

Commit

Permalink
many: make shell scripts shellcheck-clean
Browse files Browse the repository at this point in the history
Also make run-checks run shellcheck on the data completion scripts as
well as on tests/lib/*.sh. Includes making pinentry-fake.sh an actual
script (so shellcheck can check it too) instead of a here-document
inside another one.
  • Loading branch information
chipaca committed May 22, 2017
1 parent 78336ac commit c5733ff
Show file tree
Hide file tree
Showing 21 changed files with 190 additions and 160 deletions.
6 changes: 3 additions & 3 deletions data/completion/complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ _complete_from_snap() {
{
# De-serialize the output of 'snap run --command=complete ...' into the format
# bash expects:
read -a opts
read -r -a opts
# opts is expected to be a series of compopt options
if [[ ${#opts[@]} -gt 0 ]]; then
if [[ "${opts[0]}" == "cannot" ]]; then
Expand All @@ -56,7 +56,7 @@ _complete_from_snap() {
done
fi

read bounced
read -r bounced
case "$bounced" in
""|"alias"|"export"|"job"|"variable")
# OK
Expand All @@ -67,7 +67,7 @@ _complete_from_snap() {
;;
esac

read sep
read -r sep
if [ -n "$sep" ]; then
# non-blank separator? madness!
return 2
Expand Down
2 changes: 1 addition & 1 deletion data/completion/etelpmoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ _die() {
exit 1
}

if [[ "$BASH_SOURCE" != "$0" ]]; then
if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then
_die "ERROR: this is meant to be run, not sourced."
fi

Expand Down
11 changes: 7 additions & 4 deletions get-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

set -eu

if [ -z "$(which govendor)" ];then
echo Installing govendor
go get -u github.com/kardianos/govendor
if ! which govendor >/dev/null;then
export PATH="$PATH:${GOPATH%%:*}/bin"

if ! which govendor >/dev/null;then
echo Installing govendor
go get -u github.com/kardianos/govendor
fi
fi
export PATH=$PATH:$GOPATH/bin

echo Obtaining dependencies
govendor sync
Expand Down
62 changes: 36 additions & 26 deletions run-checks
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash
# bash because of the codecov integration
#!/bin/sh

if [ "$TRAVIS_BUILD_NUMBER" ]; then
echo travis_fold:start:env
Expand All @@ -16,6 +15,9 @@ if which goctest >/dev/null; then
else
goctest="go test"
fi
COVERMODE=${COVERMODE:-atomic}
export GOPATH="${GOPATH:-$(realpath "$(dirname "$0")"/../../../../)}"
export PATH="$PATH:${GOPATH%%:*}/bin"

STATIC=
UNIT=
Expand Down Expand Up @@ -52,6 +54,7 @@ exit_with_exit_code() {
}
addtrap() {
CURRENTTRAP="$CURRENTTRAP ; $1"
# shellcheck disable=SC2064
trap "store_exit_code; $CURRENTTRAP ; exit_with_exit_code" EXIT
}

Expand All @@ -64,7 +67,7 @@ endmsg() {
m="Crushing failure and despair."
fi
echo
if [ -t 1 -a -z "$STATIC" ]; then
if [ -t 1 ] && [ -z "$STATIC" ]; then
cat "data/$p"
else
echo "$m"
Expand All @@ -76,7 +79,7 @@ addtrap endmsg
append_coverage() (
profile="$1"
if [ -f "$profile" ]; then
grep -v "mode: set" -- "$profile" >> .coverage/coverage.out
grep -v "^mode:" -- "$profile" >> .coverage/coverage.out
rm "$profile"
fi
)
Expand All @@ -86,12 +89,12 @@ if [ "$STATIC" = 1 ]; then

# Run static tests.
echo Checking docs
./mdlint.py docs/*.md
./mdlint.py ./*.md docs/*.md

echo Checking formatting
fmt=""
for pkg in $(go list ./... | grep -v '/vendor/' ); do
s="$(gofmt -s -l $GOPATH/src/$pkg)"
for dir in $(go list -f '{{.Dir}}' ./... | grep -v '/vendor/' ); do
s="$(gofmt -s -l "$dir")"
if [ -n "$s" ]; then
fmt="$s\n$fmt"
fi
Expand All @@ -105,26 +108,33 @@ if [ "$STATIC" = 1 ]; then

# go vet
echo Running vet
for pkg in $(go list ./... | grep -v '/vendor/' ); do
go vet $pkg
done
# shellcheck disable=SC2046
go vet $(go list ./... | grep -v '/vendor/' )

if which shellcheck 2>/dev/null; then
if which shellcheck >/dev/null; then
echo Checking shell scripts...
shellcheck -s bash data/completion/*.sh
shellcheck -s sh run-checks get-deps.sh
shellcheck tests/lib/*.sh
shellcheck -s bash -e SC1090,SC1091 data/completion/*.sh
fi

echo Checking spelling errors
go get -u github.com/client9/misspell/cmd/misspell
for file in $(ls . | grep -v 'vendor\|po'); do
${GOBIN:-$GOPATH/bin}/misspell -error -i auther $file
if ! which misspell >/dev/null; then
go get -u github.com/client9/misspell/cmd/misspell
fi
for file in *; do
if [ "$file" = "vendor" ] || [ "$file" = "po" ]; then
continue
fi
misspell -error -i auther "$file"
done

echo Checking for ineffective assignments
go get -u github.com/gordonklaus/ineffassign
for file in $(ls . | grep -v 'vendor\|po'); do
${GOBIN:-$GOPATH/bin}/ineffassign $file
done
if ! which ineffassign >/dev/null; then
go get -u github.com/gordonklaus/ineffassign
fi
# ineffassign knows about ignoring vendor/ \o/
ineffassign .
fi

if [ "$UNIT" = 1 ]; then
Expand All @@ -133,22 +143,22 @@ if [ "$UNIT" = 1 ]; then
# Prepare the coverage output profile.
rm -rf .coverage
mkdir .coverage
echo "mode: set" > .coverage/coverage.out
echo "mode: $COVERMODE" > .coverage/coverage.out

echo Building
go build -v github.com/snapcore/snapd/...

# tests
echo Running tests from $(pwd)
echo Running tests from "$PWD"
for pkg in $(go list ./... | grep -v '/vendor/' ); do
go test -i $pkg
$goctest -v -coverprofile=.coverage/profile.out -covermode=atomic $pkg
go test -i "$pkg"
$goctest -v -coverprofile=.coverage/profile.out -covermode="$COVERMODE" "$pkg"
append_coverage .coverage/profile.out
done

# upload to codecov.io if on travis
if [ "${TRAVIS_BUILD_NUMBER:-}" ]; then
bash <(curl -s https://codecov.io/bash) -f .coverage/coverage.out
curl -s https://codecov.io/bash | bash /dev/stdin -f .coverage/coverage.out
fi
fi

Expand All @@ -157,7 +167,7 @@ if [ "$SPREAD" = 1 ]; then
addtrap "rm -rf \"$TMP_SPREAD\""

export PATH=$TMP_SPREAD:$PATH
( cd $TMP_SPREAD && curl -s -O https://niemeyer.s3.amazonaws.com/spread-amd64.tar.gz && tar xzvf spread-amd64.tar.gz )
( cd "$TMP_SPREAD" && curl -s -O https://niemeyer.s3.amazonaws.com/spread-amd64.tar.gz && tar xzvf spread-amd64.tar.gz )

spread -v linode:

Expand All @@ -170,7 +180,7 @@ if [ "$DEPRECATED" = 1 ]; then

fi

UNCLEAN="$(git status -s|grep ^??)" || true
UNCLEAN="$(git status -s|grep '^??')" || true
if [ -n "$UNCLEAN" ]; then
cat <<EOF
Expand Down
6 changes: 3 additions & 3 deletions tests/lib/apt.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

. $TESTSLIB/pkgdb.sh
# shellcheck source=tests/lib/pkgdb.sh
. "$TESTSLIB"/pkgdb.sh

install_build_snapd(){
if [ "$SRU_VALIDATION" = "1" ]; then
Expand All @@ -12,7 +13,6 @@ install_build_snapd(){
mv sources.list.back /etc/apt/sources.list
apt update
else
packages="${GOPATH}/snapd_*.deb"
distro_install_local_package $packages
distro_install_local_package "${GOPATH%%:*}"/snapd_*.deb
fi
}
3 changes: 2 additions & 1 deletion tests/lib/boot.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!bash
#!/bin/bash

bootenv() {
if [ $# -eq 0 ]; then
if command -v grub-editenv >/dev/null; then
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/changes.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

change_id() {
# takes <summary pattern> [<status>]
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/dirs.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh

# Default applies for: Ubuntu, Debian
SNAPMOUNTDIR=/snap
LIBEXECDIR=/usr/lib
export SNAPMOUNTDIR=/snap
export LIBEXECDIR=/usr/lib
26 changes: 3 additions & 23 deletions tests/lib/mkpinentry.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
#!/bin/sh
echo "setup fake gpg pinentry environment"
cat > /tmp/pinentry-fake <<'EOF'
#!/bin/sh
set -e
echo "OK Pleased to meet you"
while true; do
read line
case $line in
GETPIN)
echo "D pass"
echo "OK"
;;
BYE)
exit 0
;;
*)
echo "OK I'm not very smart"
;;
esac
done
EOF
chmod +x /tmp/pinentry-fake
mkdir -pm 0700 $HOME/.snap/gnupg/
echo pinentry-program /tmp/pinentry-fake > $HOME/.snap/gnupg/gpg-agent.conf
mkdir -p ~/.snap/gnupg/
echo pinentry-program "$TESTSLIB/pinentry-fake.sh" > ~/.snap/gnupg/gpg-agent.conf
chmod -R go-rwx ~/.snap
4 changes: 2 additions & 2 deletions tests/lib/network.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
get_default_iface(){
echo "$(ip route get 8.8.8.8 | awk '{ print $5; exit }')"
ip route get 8.8.8.8 | awk '{ print $5; exit }'
}
18 changes: 18 additions & 0 deletions tests/lib/pinentry-fake.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
set -e
echo "OK Pleased to meet you"
while true; do
read -r line
case $line in
GETPIN)
echo "D pass"
echo "OK"
;;
BYE)
exit 0
;;
*)
echo "OK I'm not very smart"
;;
esac
done
2 changes: 2 additions & 0 deletions tests/lib/pkgdb.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

# shellcheck source=tests/lib/quiet.sh
. "$TESTSLIB/quiet.sh"

debian_name_package() {
Expand Down Expand Up @@ -46,6 +47,7 @@ distro_install_local_package() {
if [ "$allow_downgrades" = "true" ]; then
flags="$flags --allow-downgrades"
fi
# shellcheck disable=SC2086
apt install $flags "$@"
;;
*)
Expand Down
Loading

0 comments on commit c5733ff

Please sign in to comment.