Skip to content

Commit

Permalink
TESTS: Add tests for PORT4ME_nnn environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Jun 21, 2023
1 parent e8d9024 commit ca8d83c
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 6 deletions.
58 changes: 57 additions & 1 deletion bash/tests/port4me.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,96 @@ setup() {
assert_success
}

@test "port4me --user=alice" {
run port4me --user=alice
assert_success
assert_output "30845"
}

@test "port4me with PORT4ME_USER=alice" {
export PORT4ME_USER=alice
run port4me
assert_success
assert_output "30845"
}

@test "port4me --user=bob" {
run port4me --user=bob
assert_success
assert_output "54242"
}

@test "port4me --user=alice --tool=rstudio" {
run port4me --user=alice --tool=rstudio
assert_success
assert_output "22486"
}

@test "port4me --user=alice with PORT4ME_TOOL=rstudio" {
export PORT4ME_TOOL=rstudio
run port4me --user=alice
assert_success
assert_output "22486"
}

@test "port4me --user=alice --exclude=30845,32310" {
run port4me --user=alice --exclude=30845,32310
assert_success
assert_output "19654"
}

@test "port4me --user=alice with PORT4ME_EXCLUDE=30845,32310" {
export PORT4ME_EXCLUDE=30845,32310
run port4me --user=alice
assert_success
assert_output "19654"
}

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

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

@test "port4me --user=alice --include=2000-2123,4321,10000-10999" {
run port4me --user=alice --include=2000-2123,4321,10000-10999
assert_success
assert_output "10451"
}

@test "port4me --user=alice with PORT4ME_INCLUDE=2000-2123,4321,10000-10999" {
export PORT4ME_INCLUDE=2000-2123,4321,10000-10999
run port4me --user=alice
assert_success
assert_output "10451"
}

@test "port4me --user=alice --tool=jupyter-notebook" {
run port4me --user=alice --tool=jupyter-notebook
assert_success
assert_output "29525"
}

@test "port4me --list=10" {
@test "port4me --user=alice --list=10" {
run port4me --user=alice --list=10
assert_success
truth=(30845 19654 32310 63992 15273 31420 62779 55372 24143 41300)
[[ "${lines[*]}" == "${truth[*]}" ]]
}

@test "port4me --user=alice with PORT4ME_LIST=10" {
export PORT4ME_LIST=10
run port4me --user=alice
assert_success
truth=(30845 19654 32310 63992 15273 31420 62779 55372 24143 41300)
[[ "${lines[*]}" == "${truth[*]}" ]]
}
45 changes: 42 additions & 3 deletions python/tests/test_port4me.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,71 @@
from port4me import port4me
from os import environ

def test_alice():
assert port4me('', 'alice') == 30845


def test_alice_env():
environ['PORT4ME_USER'] = 'alice'
assert port4me('') == 30845
environ.pop('PORT4ME_USER')

def test_alice():

def test_alice_list_one():
assert port4me('', 'alice', list=1) == [30845]


def test_alice():
def test_alice_list_five():
assert port4me('', 'alice', list=5) == [30845, 19654, 32310, 63992, 15273]


## FIXME: HB /2023-06-21
def test_alice_list_five_env():
environ['PORT4ME_LIST'] = '5'
# assert port4me('', 'alice') == [30845, 19654, 32310, 63992, 15273]
environ.pop('PORT4ME_LIST')


def test_alice_tool():
assert port4me('jupyter-notebook', 'alice', list=1) == [29525]
assert port4me('jupyter-notebook', 'alice') == 29525


def test_alice_tool_env():
environ['PORT4ME_TOOL'] = 'jupyter-notebook'
assert port4me('', 'alice') == 29525
environ.pop('PORT4ME_TOOL')


def test_alice_exclude():
assert port4me('', 'alice', exclude=[30845, 32310]) == 19654


def test_alice_exclude_env():
environ['PORT4ME_EXCLUDE'] = '30845,32310'
assert port4me('', 'alice') == 19654
environ.pop('PORT4ME_EXCLUDE')


def test_alice_exclude():
assert port4me('', 'alice', include=list(range(2000, 2123+1)) + [4321] + list(range(10000, 10999+1))) == 10451


def test_alice_include_env():
include = list(range(2000, 2123+1)) + [4321] + list(range(10000, 10999+1))
environ['PORT4ME_INCLUDE'] = ','.join(map(str, include))
assert port4me('', 'alice') == 10451
environ.pop('PORT4ME_INCLUDE')


def test_alice_prepend():
assert port4me('', 'alice', list=5, prepend=[4321, 11001]) == [4321, 11001, 30845, 19654, 32310]


def test_alice_prepend_env():
environ['PORT4ME_PREPEND'] = '4321,11001'
assert port4me('', 'alice', list=5) == [4321, 11001, 30845, 19654, 32310]
environ.pop('PORT4ME_PREPEND')


def test_bob():
assert port4me('', 'bob', list=5) == [54242, 4930, 42139, 14723, 55707]
Expand Down
114 changes: 112 additions & 2 deletions r/tests/port4me.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
library(port4me)

Sys.setenv(PORT4ME_USER = "alice")
message('- port4me(user = "alice")')
truth <- 30845L
ports <- port4me(user = "alice")
print(ports)
stopifnot(
is.integer(ports),
all(is.finite(ports)),
all(ports > 0L),
all(ports <= 65535L),
all(ports >= 1024L),
length(ports) == length(truth),
all(ports == truth)
)


message('- port4me(user = "bob")')
truth <- 54242L
ports <- port4me(user = "bob")
print(ports)
stopifnot(
is.integer(ports),
all(is.finite(ports)),
all(ports > 0L),
all(ports <= 65535L),
all(ports >= 1024L),
length(ports) == length(truth),
all(ports == truth)
)


message('- port4me(user = "alice", tool = "rstudio")')
Expand All @@ -17,6 +44,24 @@ stopifnot(
all(ports == truth)
)


message('- port4me(user = "alice") with PORT4ME_TOOL=rstudio)')
Sys.setenv(PORT4ME_TOOL = "rstudio")
truth <- 22486L
ports <- port4me(user = "alice")
print(ports)
stopifnot(
is.integer(ports),
all(is.finite(ports)),
all(ports > 0L),
all(ports <= 65535L),
all(ports >= 1024L),
length(ports) == length(truth),
all(ports == truth)
)
Sys.unsetenv("PORT4ME_TOOL")


message('- port4me(user = "alice", tool = "jupyter-notebook")')
truth <- 29525L
ports <- port4me(user = "alice", tool="jupyter-notebook")
Expand All @@ -31,6 +76,21 @@ stopifnot(
all(ports == truth)
)

message('- port4me() with PORT4ME_USER=alice')
Sys.setenv(PORT4ME_USER = "alice")
truth <- 30845L
ports <- port4me()
print(ports)
stopifnot(
is.integer(ports),
all(is.finite(ports)),
all(ports > 0L),
all(ports <= 65535L),
all(ports >= 1024L),
length(ports) == length(truth),
all(ports == truth)
)

truth <- c(30845, 19654, 32310, 63992, 15273, 31420, 62779, 55372, 24143, 41300)
message(sprintf("- port4me(list = %d)", length(truth)))
ports <- port4me(list = length(truth))
Expand All @@ -45,6 +105,23 @@ stopifnot(
all(ports == truth)
)


message('- port4me(user = "alice") with PORT4ME_LIST=10)')
Sys.setenv(PORT4ME_LIST = "10")
ports <- port4me(list = length(truth))
print(ports)
stopifnot(
is.integer(ports),
all(is.finite(ports)),
all(ports > 0L),
all(ports <= 65535L),
all(ports >= 1024L),
length(ports) == length(truth),
all(ports == truth)
)
Sys.unsetenv("PORT4ME_LIST")


exclude <- c(30845, 32310)
message(sprintf("- port4me(exclude = c(%s))", paste(exclude, collapse = ", ")))
port <- port4me(exclude = exclude)
Expand All @@ -60,6 +137,22 @@ stopifnot(
)


message(sprintf("- port4me() with PORT4ME_EXCLUDE=%s", paste(exclude, collapse = ", ")))
Sys.setenv(PORT4ME_EXCLUDE = paste(exclude, collapse = ","))
port <- port4me()
print(port)
stopifnot(
length(port) == 1L,
is.integer(port),
is.finite(port),
port > 0L,
port <= 65535L,
port >= 1024L,
port == setdiff(truth, exclude)[1]
)
Sys.unsetenv("PORT4ME_EXCLUDE")


include <- c(2000:2123, 4321, 10000:10999)
message("- port4me(include = c(2000:2123, 4321, 10000:10999))")
port <- port4me(include = include)
Expand All @@ -75,6 +168,23 @@ stopifnot(
)


include <- c(2000:2123, 4321, 10000:10999)
message("- port4me() with PORT4ME_INCLUDE=...")
Sys.setenv(PORT4ME_INCLUDE = paste(include, collapse = ","))
port <- port4me()
print(port)
stopifnot(
length(port) == 1L,
is.integer(port),
is.finite(port),
port > 0L,
port <= 65535L,
port >= 1024L,
port == 10451L
)
Sys.unsetenv("PORT4ME_INCLUDE")


prepend <- c(4321, 11001)
message("- port4me(prepend = c(4321, 11001))")
ports <- port4me(prepend = prepend, list = 5L)
Expand All @@ -85,7 +195,7 @@ stopifnot(
all(ports > 0L),
all(ports <= 65535L),
all(ports >= 1024L),
all(ports = c(prepend, truth))
all(ports == c(prepend, head(truth, n = 5L - length(prepend))))
)


Expand Down

0 comments on commit ca8d83c

Please sign in to comment.