Commit ace9d949 authored by Leo's avatar Leo

superfluous-cd-builddir improvements

parent 7eec8f64
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: CHANGES with 11.2:
- make duplicate and upper package detection remove tab chars ('\t') before sorting - make duplicate and upper package detection remove tab chars ('\t') before sorting
......
...@@ -150,6 +150,9 @@ superfluous_cd_builddir() { ...@@ -150,6 +150,9 @@ superfluous_cd_builddir() {
# if the previous line had a 'cd "$builddir"' statement # if the previous line had a 'cd "$builddir"' statement
prevcd=0 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 # 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 # the for construct, since the while loop will create a subshell that
# prevents the value of the prevcd variable from being propagated # prevents the value of the prevcd variable from being propagated
...@@ -161,14 +164,16 @@ superfluous_cd_builddir() { ...@@ -161,14 +164,16 @@ superfluous_cd_builddir() {
linenum="$(printf "%s\\n" "$line" | awk '{ print $1 }')" linenum="$(printf "%s\\n" "$line" | awk '{ print $1 }')"
statement="$(printf "%s\\n" "$line" | awk '{ $1="" ; print $0 }')" statement="$(printf "%s\\n" "$line" | awk '{ $1="" ; print $0 }')"
[ -z "$statement" ] && continue [ -z "$statement" ] && continue
if echo "$statement" | grep -q 'cd "$builddir"\($\| \)'; then if echo "$statement" | grep -E -q 'cd ["]?\$[{]?builddir["}]?+($| )' ; then
if [ "$prevcd" -eq 1 ] || [ "$cdscount" -eq 1 ]; 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" printf "%s:%s: cd \"\$builddir\" can be removed in phase '%s'\\n" "$apkbuild" "$linenum" "$phase"
fi fi
prevcd=1 prevcd=1
else else
prevcd=0 prevcd=0
fi fi
# Can be set to 0 in the first loop and the re-set it to 0 in any next loops
firstcd=0
done done
IFS="$OLDIFS" IFS="$OLDIFS"
} }
......
...@@ -60,6 +60,68 @@ is_travis() { ...@@ -60,6 +60,68 @@ is_travis() {
[[ $status -eq 0 ]] [[ $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' { @test 'unnecessary || return 1 can be removed' {
cat <<-"EOF" >$apkbuild cat <<-"EOF" >$apkbuild
pkgname=a pkgname=a
......
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