Skip to content

Commit

Permalink
BUG FIX: port4me --skip=<invalid> would not given an error [#69]
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Jan 10, 2024
1 parent dd3d39b commit b941134
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 11 deletions.
2 changes: 2 additions & 0 deletions bash/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

* `port4me --prepend=<ports>` would sort `<ports>`.

* `port4me --skip=<invalid>` would not given an error.


# Version 0.6.0 [2023-07-13]

Expand Down
40 changes: 30 additions & 10 deletions bash/incl/port4me.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -274,48 +280,62 @@ 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}"
fi
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}"
fi
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:-<not set>}"
Expand Down
2 changes: 1 addition & 1 deletion bash/port4me
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#' Requirements:
#' * Bash (>= 4)
#'
#' Version: 0.6.0-9014
#' Version: 0.6.0-9015
#' Copyright: Henrik Bengtsson (2022-2024)
#' License: MIT

Expand Down
59 changes: 59 additions & 0 deletions bash/tests/port4me.bats
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,62 @@ setup() {
@test "<CLI call> --test=<BUSY_PORT> works" {
assert_busy_port "${cli_call[@]}"
}


## -------------------------------------------------------------
## Errors
## -------------------------------------------------------------
@test "<CLI call> --test=65536 gives error" {
run "${cli_call[@]}" --test=65536
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --test=foo gives error" {
run "${cli_call[@]}" --test=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --list=-1 gives error" {
run "${cli_call[@]}" --list=-1
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --list=foo gives error" {
run "${cli_call[@]}" --list=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --skip=-1 gives error" {
run "${cli_call[@]}" --skip=-1
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --skip=foo gives error" {
run "${cli_call[@]}" --skip=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --exclude=foo gives error" {
run "${cli_call[@]}" --exclude=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --include=foo gives error" {
run "${cli_call[@]}" --include=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --prepend=foo gives error" {
run "${cli_call[@]}" --prepend=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

59 changes: 59 additions & 0 deletions python/tests/port4me.bats
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,62 @@ setup() {
@test "<CLI call> --test=<BUSY_PORT> works" {
assert_busy_port "${cli_call[@]}"
}


## -------------------------------------------------------------
## Errors
## -------------------------------------------------------------
@test "<CLI call> --test=65536 gives error" {
run "${cli_call[@]}" --test=65536
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --test=foo gives error" {
run "${cli_call[@]}" --test=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --list=-1 gives error" {
run "${cli_call[@]}" --list=-1
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --list=foo gives error" {
run "${cli_call[@]}" --list=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --skip=-1 gives error" {
run "${cli_call[@]}" --skip=-1
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --skip=foo gives error" {
run "${cli_call[@]}" --skip=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --exclude=foo gives error" {
run "${cli_call[@]}" --exclude=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --include=foo gives error" {
run "${cli_call[@]}" --include=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --prepend=foo gives error" {
run "${cli_call[@]}" --prepend=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

59 changes: 59 additions & 0 deletions r/tests/port4me.bats
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,62 @@ setup() {
@test "<CLI call> --test=<BUSY_PORT> works" {
assert_busy_port "${cli_call[@]}"
}


## -------------------------------------------------------------
## Errors
## -------------------------------------------------------------
@test "<CLI call> --test=65536 gives error" {
run "${cli_call[@]}" --test=65536
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --test=foo gives error" {
run "${cli_call[@]}" --test=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --list=-1 gives error" {
run "${cli_call[@]}" --list=-1
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --list=foo gives error" {
run "${cli_call[@]}" --list=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --skip=-1 gives error" {
run "${cli_call[@]}" --skip=-1
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --skip=foo gives error" {
run "${cli_call[@]}" --skip=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --exclude=foo gives error" {
run "${cli_call[@]}" --exclude=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --include=foo gives error" {
run "${cli_call[@]}" --include=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

@test "<CLI call> --prepend=foo gives error" {
run "${cli_call[@]}" --prepend=foo
assert_failure
assert_output --regexp "(error|ERROR|Error)"
}

0 comments on commit b941134

Please sign in to comment.