diff --git a/NEWS b/NEWS index 499c48ffb1dd8af3e9ecf3cd4a7963bee26d5e42..451d466dd99766def7e0c7520376c076806aa80a 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +CHANGES with 12: + +- superfluous-cd-builddir now detects variations of cd "$builddir" such as with brackets + without quotes and any in between + CHANGES with 11.2: - make duplicate and upper package detection remove tab chars ('\t') before sorting diff --git a/apkbuild-lint b/apkbuild-lint index c8771cb969277edcde296d0b59be4370a6b43676..2b9d497aa9cbc324a35d6b4764ecb9fb51d6ad48 100755 --- a/apkbuild-lint +++ b/apkbuild-lint @@ -150,6 +150,9 @@ superfluous_cd_builddir() { # if the previous line had a 'cd "$builddir"' statement prevcd=0 + # If it is the first cd of the program + firstcd=1 + # Use newline as our IFS delimiter, so we can iterate over lines with # the for construct, since the while loop will create a subshell that # prevents the value of the prevcd variable from being propagated @@ -161,14 +164,16 @@ superfluous_cd_builddir() { linenum="$(printf "%s\\n" "$line" | awk '{ print $1 }')" statement="$(printf "%s\\n" "$line" | awk '{ $1="" ; print $0 }')" [ -z "$statement" ] && continue - if echo "$statement" | grep -q 'cd "$builddir"\($\| \)'; then - if [ "$prevcd" -eq 1 ] || [ "$cdscount" -eq 1 ]; then + if echo "$statement" | grep -E -q 'cd ["]?\$[{]?builddir["}]?+($| )' ; then + if [ "$prevcd" -eq 1 ] || [ "$cdscount" -eq 1 ] || [ "$firstcd" -eq 1 ]; then printf "%s:%s: cd \"\$builddir\" can be removed in phase '%s'\\n" "$apkbuild" "$linenum" "$phase" fi prevcd=1 else prevcd=0 fi + # Can be set to 0 in the first loop and the re-set it to 0 in any next loops + firstcd=0 done IFS="$OLDIFS" } diff --git a/tests/apkbuild-lint.bats b/tests/apkbuild-lint.bats index 775b991780eadb85af06164ad8faf20ce777b563..72a93a09362cff396a3f4e968015260f91221031 100644 --- a/tests/apkbuild-lint.bats +++ b/tests/apkbuild-lint.bats @@ -60,6 +60,68 @@ is_travis() { [[ $status -eq 0 ]] } +@test 'cd \"\$builddir\" with brackets should be detected' { + cat <<-"EOF" >$apkbuild + pkgname=a + pkgver=1 + + build() { + cd "${builddir}" + } + EOF + + run $cmd $apkbuild + [[ $status -eq 1 ]] + assert_match "${lines[0]}" "builddir.*can be removed" +} + +@test 'cd \"\$builddir\" with brackets and no quotes should be detected' { + cat <<-"EOF" >$apkbuild + pkgname=a + pkgver=1 + + build() { + cd ${builddir} + } + EOF + + run $cmd $apkbuild + [[ $status -eq 1 ]] + assert_match "${lines[0]}" "builddir.*can be removed" +} + +@test 'cd \"\$builddir\" without quotes should be detected' { + cat <<-"EOF" >$apkbuild + pkgname=a + pkgver=1 + + build() { + cd $builddir + } + EOF + + run $cmd $apkbuild + [[ $status -eq 1 ]] + assert_match "${lines[0]}" "builddir.*can be removed" +} + +@test 'cd \"\$builddir\" should be highlighted if it is also the first' { + cat <<-"EOF" >$apkbuild + pkgname=a + pkgver=1 + + build() { + cd $builddir + cd ${builddir} + } + EOF + + run $cmd $apkbuild + [[ $status -eq 1 ]] + assert_match "${lines[0]}" "builddir.*can be removed" + assert_match "${lines[1]}" "builddir.*can be removed" +} + @test 'unnecessary || return 1 can be removed' { cat <<-"EOF" >$apkbuild pkgname=a