Skip to content

Commit

Permalink
BUG FIX: port4me --prepend=<ports> would sort <ports>
Browse files Browse the repository at this point in the history
BUG FIX: port4me --include=<invalid> would not give an error [#69]
  • Loading branch information
HenrikBengtsson committed Jan 10, 2024
1 parent d58b54e commit dd3d39b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 26 deletions.
4 changes: 3 additions & 1 deletion bash/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@

* `port4me --test=0` would output a random port, instead of giving an
out-of-range error.


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


# Version 0.6.0 [2023-07-13]

Expand Down
32 changes: 26 additions & 6 deletions bash/incl/port4me.bash
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ _p4m_error() {
exit 1
}

_p4m_signal_error() {
if grep -q -E "ERROR<<<(.*)>>>" <<< "$*"; then
_p4m_error "$(sed -E "s/.*ERROR<<<(.*)>>>,*/\1/g" <<< "$*")"
fi
}

#' Check if TCP port can be opened
#'
#' Examples:
Expand Down Expand Up @@ -173,9 +179,10 @@ _p4m_string_to_uint() {

_p4m_parse_ports() {
local spec=${1:?}
local sort=${2:-true}
local specs
local -a ports

## Prune and pre-parse input
spec=${spec//\{chrome\}/${PORT4ME_EXCLUDE_UNSAFE_CHROME}}
spec=${spec//\{firefox\}/${PORT4ME_EXCLUDE_UNSAFE_FIREFOX}}
Expand All @@ -184,7 +191,10 @@ _p4m_parse_ports() {
spec=${spec## }
spec=${spec%% }
spec=${spec// /$'\n'}
spec=$(sort -n -u <<< "${spec}")
spec=$(uniq <<< "${spec}")
if $sort; then
spec=$(sort -u <<< "${spec}")
fi

## Split input into lines
mapfile -t specs <<< "${spec}"
Expand All @@ -198,6 +208,11 @@ _p4m_parse_ports() {
ports+=($(seq "$from" "$to"))
elif grep -q -E "^${pattern}$" <<< "$spec"; then
ports+=("$spec")
elif grep -q -E "^[[:blank:]]*$" <<< "$spec"; then
true
else
echo "ERROR<<<Unknown port specification: ${spec}>>>"
return 0
fi
done

Expand Down Expand Up @@ -251,7 +266,7 @@ port4me() {
local -i list=${PORT4ME_LIST:-0}
local -i test=${PORT4ME_TEST:-0}

local -i exclude include prepend
local -a exclude include prepend
local -i count tries tmp_int

## Assert Bash (>= 4)
Expand All @@ -270,10 +285,15 @@ port4me() {
_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}")
mapfile -t prepend < <(_p4m_parse_ports "${PORT4ME_PREPEND},${PORT4ME_PREPEND_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
tmp_int=${PORT4ME_INCLUDE_MIN}
Expand Down Expand Up @@ -342,7 +362,7 @@ port4me() {
${PORT4ME_DEBUG:-false} && >&2 printf "Port drawn: %d\n" "$port"
fi

## Skip?
## Exclude?
if (( ${#exclude[@]} > 0 )); then
if [[ " ${exclude[*]} " == *" $port "* ]]; then
${PORT4ME_DEBUG:-false} && >&2 printf "Port excluded: %d\n" "$port"
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-9013
#' Version: 0.6.0-9014
#' Copyright: Henrik Bengtsson (2022-2024)
#' License: MIT

Expand Down
12 changes: 6 additions & 6 deletions bash/tests/port4me.bats
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ setup() {
assert_output "19654"
}

@test "<CLI call> --user=alice --prepend=4321,11001 --list=5" {
run "${cli_call[@]}" --user=alice --prepend=4321,11001 --list=5
@test "<CLI call> --user=alice --prepend=11001,4321 --list=5" {
run "${cli_call[@]}" --user=alice --prepend=11001,4321 --list=5
assert_success
truth=(4321 11001 30845 19654 32310)
truth=(11001 4321 30845 19654 32310)
[[ "${lines[*]}" == "${truth[*]}" ]]
}

@test "<CLI call> --user=alice --list=5 with PORT4ME_PREPEND=4321,11001" {
export PORT4ME_PREPEND=4321,11001
@test "<CLI call> --user=alice --list=5 with PORT4ME_PREPEND=11001,4321" {
export PORT4ME_PREPEND=11001,4321
run "${cli_call[@]}" --user=alice --list=5
assert_success
truth=(4321 11001 30845 19654 32310)
truth=(11001 4321 30845 19654 32310)
[[ "${lines[*]}" == "${truth[*]}" ]]
}

Expand Down
12 changes: 6 additions & 6 deletions python/tests/port4me.bats
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ setup() {
assert_output "19654"
}

@test "<CLI call> --user=alice --prepend=4321,11001 --list=5" {
run "${cli_call[@]}" --user=alice --prepend=4321,11001 --list=5
@test "<CLI call> --user=alice --prepend=11001,4321 --list=5" {
run "${cli_call[@]}" --user=alice --prepend=11001,4321 --list=5
assert_success
truth=(4321 11001 30845 19654 32310)
truth=(11001 4321 30845 19654 32310)
[[ "${lines[*]}" == "${truth[*]}" ]]
}

@test "<CLI call> --user=alice --list=5 with PORT4ME_PREPEND=4321,11001" {
export PORT4ME_PREPEND=4321,11001
@test "<CLI call> --user=alice --list=5 with PORT4ME_PREPEND=11001,4321" {
export PORT4ME_PREPEND=11001,4321
run "${cli_call[@]}" --user=alice --list=5
assert_success
truth=(4321 11001 30845 19654 32310)
truth=(11001 4321 30845 19654 32310)
[[ "${lines[*]}" == "${truth[*]}" ]]
}

Expand Down
12 changes: 6 additions & 6 deletions r/tests/port4me.bats
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ setup() {
assert_output "19654"
}

@test "<CLI call> --user=alice --prepend=4321,11001 --list=5" {
run "${cli_call[@]}" --user=alice --prepend=4321,11001 --list=5
@test "<CLI call> --user=alice --prepend=11001,4321 --list=5" {
run "${cli_call[@]}" --user=alice --prepend=11001,4321 --list=5
assert_success
truth=(4321 11001 30845 19654 32310)
truth=(11001 4321 30845 19654 32310)
[[ "${lines[*]}" == "${truth[*]}" ]]
}

@test "<CLI call> --user=alice --list=5 with PORT4ME_PREPEND=4321,11001" {
export PORT4ME_PREPEND=4321,11001
@test "<CLI call> --user=alice --list=5 with PORT4ME_PREPEND=11001,4321" {
export PORT4ME_PREPEND=11001,4321
run "${cli_call[@]}" --user=alice --list=5
assert_success
truth=(4321 11001 30845 19654 32310)
truth=(11001 4321 30845 19654 32310)
[[ "${lines[*]}" == "${truth[*]}" ]]
}

Expand Down

0 comments on commit dd3d39b

Please sign in to comment.