Files
NTrace-core/util/env_test.go
T
tsosunchia aa3df62875 Add tests for DN42 and PTR record functionalities
- Added focused unit tests for listener parsing helpers so buildListenInfo and isDigitsOnly are now validated without invoking the full CLI stack (cmd/listen_info_test.go:10-32).
  - Replaced placeholder DN42 tests with deterministic coverage for geofeed parsing and PTR matching, using temp CSV fixtures to check both positive and negative paths (dn42/geofeed_test.go:13-57, dn42/ptr_test.go:13-56).
  - Rebuilt the ipgeo tests to assert provider lookup mappings, DN42 integration (including PTR fallback and “unknown” defaults), and the disableGeoIP shim (ipgeo/ipgeo_test.go:12-53, ipgeo/dn42_test.go:14-78).
  - Introduced environment helper tests that exercise trimming, boolean, default, and integer reads without touching external DNS (util/env_test.go:9-46).
  - Removed legacy network-dependent tests that only printed output, ensuring the suite is deterministic.
2025-10-29 13:27:37 +08:00

47 lines
1.1 KiB
Go

package util
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetEnvTrimmed(t *testing.T) {
t.Setenv("TEST_TRIMMED_KEY", " value ")
val, ok := GetEnvTrimmed("TEST_TRIMMED_KEY")
assert.True(t, ok)
assert.Equal(t, "value", val)
_, ok = GetEnvTrimmed("TEST_TRIMMED_MISSING")
assert.False(t, ok)
}
func TestGetEnvBool(t *testing.T) {
t.Setenv("TEST_BOOL_TRUE", "1")
assert.True(t, GetEnvBool("TEST_BOOL_TRUE", false))
t.Setenv("TEST_BOOL_FALSE", "0")
assert.False(t, GetEnvBool("TEST_BOOL_FALSE", true))
t.Setenv("TEST_BOOL_INVALID", "maybe")
assert.True(t, GetEnvBool("TEST_BOOL_INVALID", true))
}
func TestGetEnvDefault(t *testing.T) {
t.Setenv("TEST_DEFAULT_KEY", " custom ")
assert.Equal(t, "custom", GetEnvDefault("TEST_DEFAULT_KEY", "fallback"))
assert.Equal(t, "fallback", GetEnvDefault("TEST_DEFAULT_MISSING", "fallback"))
}
func TestGetEnvInt(t *testing.T) {
t.Setenv("TEST_INT_VALID", " 42 ")
assert.Equal(t, 42, GetEnvInt("TEST_INT_VALID", 7))
t.Setenv("TEST_INT_INVALID", "NaN")
assert.Equal(t, 5, GetEnvInt("TEST_INT_INVALID", 5))
assert.Equal(t, 9, GetEnvInt("TEST_INT_MISSING", 9))
}