Skip to content

Add shellcheck to bats files#2549

Merged
mrunalp merged 13 commits intoopencontainers:masterfrom
kolyshkin:bats-shellcheck
Aug 18, 2020
Merged

Add shellcheck to bats files#2549
mrunalp merged 13 commits intoopencontainers:masterfrom
kolyshkin:bats-shellcheck

Conversation

@kolyshkin
Copy link
Copy Markdown
Contributor

@kolyshkin kolyshkin commented Aug 9, 2020

This PR

  • fixes (or adds annotations to ignore) all the shellcheck warnings for .bats files;
  • add shellcheck to the CI cycle.

TODO (not in this PR):

  • fix shellcheck warnings for helpers.bash

This currently includes #2548; I'll rebase once it's merged. rebased

The ending EOF should be
 - all by itself (i.e. no extra characters on the same line);
 - with no whitespace before it.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
It's not a regex but a substring, so use a substring match.

Fixes the following warning by shellcheck:

> In mounts.bats line 20:
> 	[[ "${lines[0]}" =~ '/tmp/bind/config.json' ]]
>                           ^---------------------^ SC2076: Don't quote right-hand side of =~, it'll match literally rather than as a regex.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1. cd is useless as all the paths are absolute
2. run is redundant, does not make sense to use it
3. use mkdir -p to save a line of code

This also eliminates shellcheck warnings like this one:

> In spec.bats line 8:
>   cd "$INTEGRATION_ROOT"
>   ^--------------------^ SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Fixes the following warning:

> In cgroups.bats line 58:
>     if [ "$KERNEL_MAJOR" -lt 4 ] || [ "$KERNEL_MAJOR" -eq 4 -a "$KERNEL_MINOR" -le 5 ]; then
>                                                             ^-- SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Fixes the following warning:

> In update.bats line 422:
>     local root_period=$(cat "${CGROUP_CPU_BASE_PATH}/cpu.rt_period_us")
>           ^---------^ SC2155: Declare and assign separately to avoid masking return values.
>
>
> In update.bats line 423:
>     local root_runtime=$(cat "${CGROUP_CPU_BASE_PATH}/cpu.rt_runtime_us")
>           ^----------^ SC2155: Declare and assign separately to avoid masking return values.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This fixes the following warning, and implements a suggestion:

> In update.bats line 426:
>     IFS='/' read -r -a dirs <<< $(echo ${CGROUP_CPU} | sed -e s@^${CGROUP_CPU_BASE_PATH}/@@)
>                                 ^-- SC2046: Quote this to prevent word splitting.
>                                   ^-- SC2001: See if you can use ${variable//search/replace} instead.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Fix all warnings like this one:

> In checkpoint.bats line 197:
>   cat ./work-dir/restore.log | grep -B 5 Error || true
>       ^--------------------^ SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Those are pretty simple to allow shellcheck to fix these, so
this commit is courtesy of

> shellcheck -i SC2086 -i SC2006 -f diff *.bats > fix.diff
> patch -p1 < fix.diff

repeated 3 times ;)

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Fix or ignore warnings like this one:

> In cgroups.bats line 107:
>             if [ $(id -u) = "0" ]; then
>                  ^------^ SC2046: Quote this to prevent word splitting.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Ignore warnings like this:

> In checkpoint.bats line 169:
>   PIDS_TO_KILL=($cpt_pid)
>                 ^------^ SC2206: Quote to prevent word splitting/globbing, or split robustly with mapfile or read -a.

Since in all the cases we deal with either pids or fds, and they don't
have spaces.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Ignore the shellcheck warnings like this one:

> In tty.bats line 32:
> 	update_config '(.. | select(.[]? == "sh")) += ["-c", "stat -c %u:%g $(tty) | tr : \\\\n"]'
>                     ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.

While at it, fix some minor whitespace issues in tty.bats.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Currently all the shellcheck warnings are fixed, and we'd like it to
stay thay way. So, add shellcheck call to validate target in Makefile,
which is run on Travis CI.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants