Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/agentbbs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ func env(k, def string) string {
return def
}

// envInt reads an integer environment variable, falling back to def.
// envInt reads a positive integer environment variable, falling back to def.
func envInt(k string, def int) int {
if v := os.Getenv(k); v != "" {
if n, err := strconv.Atoi(v); err == nil {
if n, err := strconv.Atoi(v); err == nil && n > 0 {
return n
}
}
Expand Down
16 changes: 16 additions & 0 deletions cmd/agentbbs/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ package main

import "testing"

func TestEnvIntRequiresPositiveValue(t *testing.T) {
const key = "AGENTBBS_TEST_POSITIVE_INT"

for _, value := range []string{"", "invalid", "0", "-1"} {
t.Setenv(key, value)
if got := envInt(key, 15); got != 15 {
t.Errorf("envInt(%q, 15) with %q = %d, want 15", key, value, got)
}
}

t.Setenv(key, "30")
if got := envInt(key, 15); got != 30 {
t.Errorf("envInt(%q, 15) = %d, want 30", key, got)
}
}

func TestValidIRCServerPortRange(t *testing.T) {
for _, server := range []string{
"irc.example.com",
Expand Down
Loading