Skip to content

Commit

Permalink
fix: Remove == inside [ (#1421)
Browse files Browse the repository at this point in the history
* lint(checkstyle): Miscellaneous tweaks
* lint(checkstyle): Add 'no-double-equals' rule
* lint: Remove double equals from `[`
* chore: Remove final double equals

Co-authored-by: Trevor Brown <Stratus3D@users.noreply.github.com>
  • Loading branch information
hyperupcall and Stratus3D authored Jan 14, 2023
1 parent 27c8a10 commit d81b81f
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 83 deletions.
2 changes: 1 addition & 1 deletion lib/functions/versions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ latest_command() {
local query=$2
local plugin_path

if [ "$plugin_name" == "--all" ]; then
if [ "$plugin_name" = "--all" ]; then
latest_all
fi

Expand Down
2 changes: 1 addition & 1 deletion lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ initialize_or_update_repository() {
local repository_path

disable_plugin_short_name_repo="$(get_asdf_config_value "disable_plugin_short_name_repository")"
if [ "yes" == "$disable_plugin_short_name_repo" ]; then
if [ "yes" = "$disable_plugin_short_name_repo" ]; then
printf "Short-name plugin repository is disabled\n" >&2
exit 1
fi
Expand Down
32 changes: 27 additions & 5 deletions scripts/checkstyle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
import re
import sys
import os
import argparse
from pathlib import Path
Expand Down Expand Up @@ -39,18 +38,23 @@ def utilGetStrs(line, m):

# Before: printf '%s\\n' '^w^'
# After: printf '%s\n' '^w^'
def noDoubleBackslashFixer(line: str, rule: Dict[str, str], m) -> str:
def noDoubleBackslashFixer(line: str, m) -> str:
prestr, midstr, poststr = utilGetStrs(line, m)

return f'{prestr}{midstr[1:]}{poststr}'

# Before: $(pwd)
# After: $PWD
def noPwdCapture(line: str, rule: Dict[str, str], m) -> str:
def noPwdCaptureFixer(line: str, m) -> str:
prestr, midstr, poststr = utilGetStrs(line, m)

return f'{prestr}$PWD{poststr}'

def noTestDoubleEqualsFixer(line: str, m) -> str:
prestr, midstr, poststr = utilGetStrs(line, m)

return f'{prestr}={poststr}'

def lintfile(filepath: Path, rules: List[Dict[str, str]], options: Dict[str, Any]):
content_arr = filepath.read_text().split('\n')

Expand All @@ -72,7 +76,7 @@ def lintfile(filepath: Path, rules: List[Dict[str, str]], options: Dict[str, Any
print()

if options['fix']:
content_arr[line_i] = rule['fixerFn'](line, rule, m)
content_arr[line_i] = rule['fixerFn'](line, m)

rule['found'] += 1

Expand Down Expand Up @@ -100,7 +104,7 @@ def main():
'name': 'no-pwd-capture',
'regex': '(?P<match>\\$\\(pwd\\))',
'reason': '$PWD is essentially equivalent to $(pwd) without the overhead of a subshell',
'fixerFn': noPwdCapture,
'fixerFn': noPwdCaptureFixer,
'testPositiveMatches': [
'$(pwd)'
],
Expand All @@ -109,6 +113,23 @@ def main():
],
'found': 0
},
{
'name': 'no-test-double-equals',
'regex': '(?<!\\[)\\[[^[]*?(?P<match>==).*?\\]',
'reason': 'Although it is valid Bash, it reduces consistency and copy-paste-ability',
'fixerFn': noTestDoubleEqualsFixer,
'testPositiveMatches': [
'[ a == b ]',
],
'testNegativeMatches': [
'[ a = b ]',
'[[ a = b ]]',
'[[ a == b ]]',
'[ a = b ] || [[ a == b ]]',
'[[ a = b ]] || [[ a == b ]]'
],
'found': 0
}
]

parser = argparse.ArgumentParser()
Expand All @@ -132,6 +153,7 @@ def main():
print(f'{c.MAGENTA}{rule["name"]}{c.RESET}: Failed {c.YELLOW}negative{c.RESET} test:')
print(f'=> {negativeMatch}')
print()
print('Done.')
return

options = {
Expand Down
6 changes: 3 additions & 3 deletions test/asdf_nu.bats
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cleaned_path() {

[ "$?" -eq 0 ]
output=$(echo "$result" | grep "asdf")
[ "$output" == $PWD ]
[ "$output" = $PWD ]
}

@test "adds asdf dirs to PATH" {
Expand All @@ -45,9 +45,9 @@ cleaned_path() {
")
[ "$?" -eq 0 ]
output_bin=$(echo "$result" | grep "asdf/bin")
[ "$output_bin" == "$PWD/bin" ]
[ "$output_bin" = "$PWD/bin" ]
output_shims=$(echo "$result" | grep "/shims")
[ "$output_shims" == "$HOME/.asdf/shims" ]
[ "$output_shims" = "$HOME/.asdf/shims" ]
}

@test "does not add paths to PATH more than once" {
Expand Down
4 changes: 2 additions & 2 deletions test/banned_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ teardown() {
fi

[ "$status" -eq 1 ]
[ "" == "$output" ]
[ "" = "$output" ]
done

for cmd in "${banned_commands_regex[@]}"; do
Expand All @@ -96,6 +96,6 @@ teardown() {
fi

[ "$status" -eq 1 ]
[ "" == "$output" ]
[ "" = "$output" ]
done
}
4 changes: 2 additions & 2 deletions test/help_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ EOF

run asdf help "sunny"
[ "$status" -eq 1 ]
[ "$output" == "No plugin named sunny" ]
[ "$output" = "No plugin named sunny" ]
}

@test "help should fail when plugin doesn't have documentation callback" {
cd $PROJECT_DIR

run asdf help "legacy-dummy"
[ "$status" -eq 1 ]
[ "$output" == "No documentation for plugin legacy-dummy" ]
[ "$output" = "No documentation for plugin legacy-dummy" ]
}

@test "help should show asdf help when no plugin name is provided" {
Expand Down
14 changes: 7 additions & 7 deletions test/install_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ teardown() {
# execute the generated shim
run $ASDF_DIR/shims/dummy world hello
[ "$status" -eq 0 ]
[ "$output" == "This is Dummy 1.0.0! hello world" ]
[ "$output" = "This is Dummy 1.0.0! hello world" ]
}

@test "install_command fails when tool is specified but no version of the tool is configured" {
Expand Down Expand Up @@ -180,7 +180,7 @@ teardown() {
run asdf install

# execute the generated shim
[ "$($ASDF_DIR/shims/dummy world hello)" == "This is Dummy 1.0.0! hello world" ]
[ "$($ASDF_DIR/shims/dummy world hello)" = "This is Dummy 1.0.0! hello world" ]
[ "$status" -eq 0 ]
}

Expand Down Expand Up @@ -208,7 +208,7 @@ pre_asdf_install_dummy = echo will install dummy $1
EOM

run asdf install dummy 1.0.0
[ "$output" == "will install dummy 1.0.0" ]
[ "$output" = "will install dummy 1.0.0" ]
}

@test "install command executes configured post plugin install hook" {
Expand All @@ -217,7 +217,7 @@ post_asdf_install_dummy = echo HEY $version FROM $plugin_name
EOM

run asdf install dummy 1.0.0
[ "$output" == "HEY 1.0.0 FROM dummy" ]
[ "$output" = "HEY 1.0.0 FROM dummy" ]
}

@test "install command without arguments installs versions from legacy files" {
Expand All @@ -226,7 +226,7 @@ EOM
cd $PROJECT_DIR
run asdf install
[ "$status" -eq 0 ]
[ "$output" == "" ]
[ "$output" = "" ]
[ -f $ASDF_DIR/installs/dummy/1.2.0/version ]
}

Expand All @@ -239,7 +239,7 @@ EOM

run asdf install
[ "$status" -eq 0 ]
[ "$output" == "" ]
[ "$output" = "" ]
[ -f $ASDF_DIR/installs/dummy/1.2.0/version ]
}

Expand Down Expand Up @@ -284,5 +284,5 @@ EOM
[ "$status" -eq 1 ]
[ ! -d $ASDF_DIR/downloads/dummy-broken/1.1.0 ]
[ ! -d $ASDF_DIR/installs/dummy-broken/1.1.0 ]
[ "$output" == "Download failed!" ]
[ "$output" = "Download failed!" ]
}
20 changes: 10 additions & 10 deletions test/latest_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ teardown() {
####################################################
@test "[latest_command - dummy_plugin] shows latest stable version" {
run asdf latest dummy
[ "$(echo "2.0.0")" == "$output" ]
[ "$(echo "2.0.0")" = "$output" ]
[ "$status" -eq 0 ]
}

@test "[latest_command - dummy_plugin] shows latest stable version that matches the given string" {
run asdf latest dummy 1
[ "$(echo "1.1.0")" == "$output" ]
[ "$(echo "1.1.0")" = "$output" ]
[ "$status" -eq 0 ]
}

@test "[latest_command - dummy_plugin] an invalid version should return an error" {
run asdf latest dummy 3
[ "$(echo "No compatible versions available (dummy 3)")" == "$output" ]
[ "$(echo "No compatible versions available (dummy 3)")" = "$output" ]
[ "$status" -eq 1 ]
}

Expand All @@ -40,15 +40,15 @@ teardown() {
run asdf latest legacy-dummy
echo "status: $status"
echo "output: $output"
[ "$(echo "5.1.0")" == "$output" ]
[ "$(echo "5.1.0")" = "$output" ]
[ "$status" -eq 0 ]
}

@test "[latest_command - dummy_legacy_plugin] shows latest stable version that matches the given string" {
run asdf latest legacy-dummy 1
echo "status: $status"
echo "output: $output"
[ "$(echo "1.1.0")" == "$output" ]
[ "$(echo "1.1.0")" = "$output" ]
[ "$status" -eq 0 ]
}

Expand All @@ -64,23 +64,23 @@ teardown() {
run asdf latest legacy-dummy 4
echo "status: $status"
echo "output: $output"
[ "$(echo "4.0.0")" == "$output" ]
[ "$(echo "4.0.0")" = "$output" ]
[ "$status" -eq 0 ]
}

@test "[latest_command - dummy_legacy_plugin] do not show latest unstable version with capital characters that matches the given string" {
run asdf latest legacy-dummy 5
echo "status: $status"
echo "output: $output"
[ "$(echo "5.1.0")" == "$output" ]
[ "$(echo "5.1.0")" = "$output" ]
[ "$status" -eq 0 ]
}

@test "[latest_command - dummy_legacy_plugin] an invalid version should return an error" {
run asdf latest legacy-dummy 6
echo "status: $status"
echo "output: $output"
[ "$(echo "No compatible versions available (legacy-dummy 6)")" == "$output" ]
[ "$(echo "No compatible versions available (legacy-dummy 6)")" = "$output" ]
[ "$status" -eq 1 ]
}

Expand All @@ -92,12 +92,12 @@ teardown() {
run asdf install legacy-dummy 4.0.0
run asdf latest --all
echo "output $output"
[ "$(echo -e "dummy\t2.0.0\tinstalled\nlegacy-dummy\t5.1.0\tmissing\n")" == "$output" ]
[ "$(echo -e "dummy\t2.0.0\tinstalled\nlegacy-dummy\t5.1.0\tmissing\n")" = "$output" ]
[ "$status" -eq 0 ]
}

@test "[latest_command - all plugins] not installed plugin should return missing" {
run asdf latest --all
[ "$(echo -e "dummy\t2.0.0\tmissing\nlegacy-dummy\t5.1.0\tmissing\n")" == "$output" ]
[ "$(echo -e "dummy\t2.0.0\tmissing\nlegacy-dummy\t5.1.0\tmissing\n")" = "$output" ]
[ "$status" -eq 0 ]
}
12 changes: 6 additions & 6 deletions test/list_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ teardown() {
run asdf install dummy 1.0.0
run asdf install dummy 1.1.0
run asdf list dummy
[ "$(echo -e " 1.0.0\n 1.1.0")" == "$output" ]
[ "$(echo -e " 1.0.0\n 1.1.0")" = "$output" ]
[ "$status" -eq 0 ]
}

Expand All @@ -63,33 +63,33 @@ teardown() {
run asdf install dummy 1.1
run asdf install dummy 2.0
run asdf list dummy 1
[ "$(echo -e " 1.0\n 1.1")" == "$output" ]
[ "$(echo -e " 1.0\n 1.1")" = "$output" ]
[ "$status" -eq 0 ]
}

@test "list_command with an invalid version should return an error" {
run asdf install dummy 1.0
run asdf install dummy 1.1
run asdf list dummy 2
[ "$(echo "No compatible versions installed (dummy 2)")" == "$output" ]
[ "$(echo "No compatible versions installed (dummy 2)")" = "$output" ]
[ "$status" -eq 1 ]
}

@test "list_all_command lists available versions" {
run asdf list-all dummy
[ "$(echo -e "1.0.0\n1.1.0\n2.0.0")" == "$output" ]
[ "$(echo -e "1.0.0\n1.1.0\n2.0.0")" = "$output" ]
[ "$status" -eq 0 ]
}

@test "list_all_command with version filters available versions" {
run asdf list-all dummy 1
[ "$(echo -e "1.0.0\n1.1.0")" == "$output" ]
[ "$(echo -e "1.0.0\n1.1.0")" = "$output" ]
[ "$status" -eq 0 ]
}

@test "list_all_command with an invalid version should return an error" {
run asdf list-all dummy 3
[ "$(echo "No compatible versions available (dummy 3)")" == "$output" ]
[ "$(echo "No compatible versions available (dummy 3)")" = "$output" ]
[ "$status" -eq 1 ]
}

Expand Down
4 changes: 2 additions & 2 deletions test/reshim_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pre_asdf_reshim_dummy = echo RESHIM
EOM

run asdf reshim dummy 1.0
[ "$output" == "RESHIM" ]
[ "$output" = "RESHIM" ]
}

@test "reshim command executes configured post hook" {
Expand All @@ -138,7 +138,7 @@ post_asdf_reshim_dummy = echo RESHIM
EOM

run asdf reshim dummy 1.0
[ "$output" == "RESHIM" ]
[ "$output" = "RESHIM" ]
}

# Fixes https://github.com/asdf-vm/asdf/issues/1115
Expand Down
8 changes: 4 additions & 4 deletions test/shim_env_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ teardown() {

run asdf env dummy which dummy
[ "$status" -eq 0 ]
[ "$output" == "$ASDF_DIR/installs/dummy/1.0/bin/dummy" ]
[ "$output" = "$ASDF_DIR/installs/dummy/1.0/bin/dummy" ]
}

@test "asdf env should execute under plugin custom environment used for a shim" {
Expand Down Expand Up @@ -58,11 +58,11 @@ teardown() {
[ "$status" -eq 0 ]

run grep 'FOO=bar' <(echo $output)
[ "$output" == "" ]
[ "$output" = "" ]
[ "$status" -eq 1 ]

run asdf env dummy which dummy
[ "$output" == "$ASDF_DIR/shims/dummy" ]
[ "$output" = "$ASDF_DIR/shims/dummy" ]
[ "$status" -eq 0 ]
}

Expand All @@ -79,5 +79,5 @@ teardown() {

# Should not contain duplicate colon
run grep '::' <(echo "$path_line")
[ "$duplicate_colon" == "" ]
[ "$duplicate_colon" = "" ]
}
Loading

0 comments on commit d81b81f

Please sign in to comment.