From b94113448494ee430405196f0c2bfd4c5b6996b8 Mon Sep 17 00:00:00 2001 From: Henrik Bengtsson Date: Wed, 10 Jan 2024 00:00:53 -0800 Subject: [PATCH] BUG FIX: port4me --skip= would not given an error [#69] --- bash/NEWS.md | 2 ++ bash/incl/port4me.bash | 40 +++++++++++++++++++------- bash/port4me | 2 +- bash/tests/port4me.bats | 59 +++++++++++++++++++++++++++++++++++++++ python/tests/port4me.bats | 59 +++++++++++++++++++++++++++++++++++++++ r/tests/port4me.bats | 59 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 210 insertions(+), 11 deletions(-) diff --git a/bash/NEWS.md b/bash/NEWS.md index da7572c..3ce2966 100644 --- a/bash/NEWS.md +++ b/bash/NEWS.md @@ -36,6 +36,8 @@ * `port4me --prepend=` would sort ``. + * `port4me --skip=` would not given an error. + # Version 0.6.0 [2023-07-13] diff --git a/bash/incl/port4me.bash b/bash/incl/port4me.bash index b0f43e4..a3b7c33 100644 --- a/bash/incl/port4me.bash +++ b/bash/incl/port4me.bash @@ -34,7 +34,7 @@ #' Requirements: #' * Bash (>= 4) #' -#' Version: 0.6.0-9013 +#' Version: 0.6.0-9015 #' Copyright: Henrik Bengtsson (2022-2024) #' License: MIT #' Source code: https://github.com/HenrikBengtsson/port4me @@ -63,6 +63,12 @@ _p4m_signal_error() { fi } +_p4m_assert_integer() { + if grep -q -E "^[[:digit:]]+$" <<< "$*"; then + _p4m_error "Not an integer: $*" + fi +} + #' Check if TCP port can be opened #' #' Examples: @@ -274,28 +280,27 @@ port4me() { _p4m_error "port4me requires Bash (>= 4): ${BASH_VERSION}. As a workaround, you could install the Python version (python -m pip port4me) and define a Bash function as: port4me() { python -m port4me \"\$@\"; }" fi + ## Validate arguments if [[ -n ${PORT4ME_TEST} ]]; then + if ! grep -q -E "^[[:digit:]]+$" <<< "${PORT4ME_TEST}"; then + _p4m_error "PORT4ME_TEST is not an integer: ${PORT4ME_TEST}" + fi tmp_int=${PORT4ME_TEST} if (( tmp_int < 1 || tmp_int > 65535 )); then _p4m_error "PORT4ME_TEST is out of range [1,65535]: ${tmp_int}" fi fi + ## Check port availability? if [[ $test -ne 0 ]]; then _p4m_can_port_be_opened "${test}" return $? fi - mapfile -t exclude < <(_p4m_parse_ports "${PORT4ME_EXCLUDE},${PORT4ME_EXCLUDE_SITE},${PORT4ME_EXCLUDE_UNSAFE}") - _p4m_signal_error "${exclude[@]}" - - mapfile -t include < <(_p4m_parse_ports "${PORT4ME_INCLUDE},${PORT4ME_INCLUDE_SITE}") - _p4m_signal_error "${include[@]}" - - mapfile -t prepend < <(_p4m_parse_ports "${PORT4ME_PREPEND},${PORT4ME_PREPEND_SITE}" false) - _p4m_signal_error "${prepend[@]}" - if [[ -n ${PORT4ME_INCLUDE_MIN} ]]; then + if ! grep -q -E "^[[:digit:]]+$" <<< "${PORT4ME_INCLUDE_MIN}"; then + _p4m_error "PORT4ME_INCLUDE_MIN is not an integer: ${PORT4ME_INCLUDE_MIN}" + fi tmp_int=${PORT4ME_INCLUDE_MIN} if (( tmp_int < 1 || tmp_int > 65535 )); then _p4m_error "PORT4ME_INCLUDE_MIN is out of range [1,65535]: ${tmp_int}" @@ -303,6 +308,9 @@ port4me() { fi if [[ -n ${PORT4ME_LIST} ]]; then + if ! grep -q -E "^[[:digit:]]+$" <<< "${PORT4ME_LIST}"; then + _p4m_error "PORT4ME_LIST is not an integer: ${PORT4ME_LIST}" + fi tmp_int=${PORT4ME_LIST} if (( tmp_int < 1 )); then _p4m_error "PORT4ME_LIST must not be postive: ${tmp_int}" @@ -310,12 +318,24 @@ port4me() { fi if [[ -n ${PORT4ME_SKIP} ]]; then + if ! grep -q -E "^[[:digit:]]+$" <<< "${PORT4ME_SKIP}"; then + _p4m_error "PORT4ME_SKIP is not an integer: ${PORT4ME_SKIP}" + fi tmp_int=${PORT4ME_SKIP} if (( tmp_int < 0 )); then _p4m_error "PORT4ME_SKIP must not be negative: ${tmp_int}" fi fi + mapfile -t exclude < <(_p4m_parse_ports "${PORT4ME_EXCLUDE},${PORT4ME_EXCLUDE_SITE},${PORT4ME_EXCLUDE_UNSAFE}") + _p4m_signal_error "${exclude[@]}" + + mapfile -t include < <(_p4m_parse_ports "${PORT4ME_INCLUDE},${PORT4ME_INCLUDE_SITE}") + _p4m_signal_error "${include[@]}" + + mapfile -t prepend < <(_p4m_parse_ports "${PORT4ME_PREPEND},${PORT4ME_PREPEND_SITE}" false) + _p4m_signal_error "${prepend[@]}" + if ${PORT4ME_DEBUG:-false}; then { echo "PORT4ME_EXCLUDE=${PORT4ME_EXCLUDE:-}" diff --git a/bash/port4me b/bash/port4me index 75054a2..db8346f 100755 --- a/bash/port4me +++ b/bash/port4me @@ -39,7 +39,7 @@ #' Requirements: #' * Bash (>= 4) #' -#' Version: 0.6.0-9014 +#' Version: 0.6.0-9015 #' Copyright: Henrik Bengtsson (2022-2024) #' License: MIT diff --git a/bash/tests/port4me.bats b/bash/tests/port4me.bats index d291c18..7a2582c 100644 --- a/bash/tests/port4me.bats +++ b/bash/tests/port4me.bats @@ -174,3 +174,62 @@ setup() { @test " --test= works" { assert_busy_port "${cli_call[@]}" } + + +## ------------------------------------------------------------- +## Errors +## ------------------------------------------------------------- +@test " --test=65536 gives error" { + run "${cli_call[@]}" --test=65536 + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --test=foo gives error" { + run "${cli_call[@]}" --test=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --list=-1 gives error" { + run "${cli_call[@]}" --list=-1 + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --list=foo gives error" { + run "${cli_call[@]}" --list=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --skip=-1 gives error" { + run "${cli_call[@]}" --skip=-1 + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --skip=foo gives error" { + run "${cli_call[@]}" --skip=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --exclude=foo gives error" { + run "${cli_call[@]}" --exclude=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --include=foo gives error" { + run "${cli_call[@]}" --include=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --prepend=foo gives error" { + run "${cli_call[@]}" --prepend=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + diff --git a/python/tests/port4me.bats b/python/tests/port4me.bats index d291c18..7a2582c 100644 --- a/python/tests/port4me.bats +++ b/python/tests/port4me.bats @@ -174,3 +174,62 @@ setup() { @test " --test= works" { assert_busy_port "${cli_call[@]}" } + + +## ------------------------------------------------------------- +## Errors +## ------------------------------------------------------------- +@test " --test=65536 gives error" { + run "${cli_call[@]}" --test=65536 + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --test=foo gives error" { + run "${cli_call[@]}" --test=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --list=-1 gives error" { + run "${cli_call[@]}" --list=-1 + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --list=foo gives error" { + run "${cli_call[@]}" --list=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --skip=-1 gives error" { + run "${cli_call[@]}" --skip=-1 + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --skip=foo gives error" { + run "${cli_call[@]}" --skip=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --exclude=foo gives error" { + run "${cli_call[@]}" --exclude=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --include=foo gives error" { + run "${cli_call[@]}" --include=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --prepend=foo gives error" { + run "${cli_call[@]}" --prepend=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + diff --git a/r/tests/port4me.bats b/r/tests/port4me.bats index d291c18..7a2582c 100644 --- a/r/tests/port4me.bats +++ b/r/tests/port4me.bats @@ -174,3 +174,62 @@ setup() { @test " --test= works" { assert_busy_port "${cli_call[@]}" } + + +## ------------------------------------------------------------- +## Errors +## ------------------------------------------------------------- +@test " --test=65536 gives error" { + run "${cli_call[@]}" --test=65536 + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --test=foo gives error" { + run "${cli_call[@]}" --test=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --list=-1 gives error" { + run "${cli_call[@]}" --list=-1 + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --list=foo gives error" { + run "${cli_call[@]}" --list=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --skip=-1 gives error" { + run "${cli_call[@]}" --skip=-1 + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --skip=foo gives error" { + run "${cli_call[@]}" --skip=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --exclude=foo gives error" { + run "${cli_call[@]}" --exclude=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --include=foo gives error" { + run "${cli_call[@]}" --include=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} + +@test " --prepend=foo gives error" { + run "${cli_call[@]}" --prepend=foo + assert_failure + assert_output --regexp "(error|ERROR|Error)" +} +