Skip to content

test: Remove environmental variable interference from testing #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 23, 2025
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
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Print All environment variables
run: env | sort

- name: Set up Go
uses: actions/setup-go@v5

Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ gs/examples/bookman/conf/
gs/examples/bookman/log/*.log
gs/examples/bookman/.cover/covcounters.*
gs/examples/bookman/.cover/covmeta.*
gs/examples/bookman/.cover/cover.txt
gs/examples/bookman/.cover/cover.txt

coverage.txt
4 changes: 0 additions & 4 deletions conf/storage/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ func SplitPath(key string) (_ []Path, err error) {

// appendKey appends a key segment to the path.
func appendKey(path []Path, s string) ([]Path, error) {
_, err := strconv.ParseUint(s, 10, 64)
if err == nil {
return nil, errors.New("invalid key")
}
path = append(path, Path{PathTypeKey, s})
return path, nil
}
Expand Down
21 changes: 17 additions & 4 deletions conf/storage/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package storage

import (
"errors"
"fmt"
"testing"

"github.com/go-spring/spring-core/util/assert"
Expand Down Expand Up @@ -133,15 +134,22 @@ func TestSplitPath(t *testing.T) {
},
{
Key: "0[0]",
Err: errors.New("invalid key '0[0]'"),
Path: []Path{
{PathTypeKey, "0"},
{PathTypeIndex, "0"},
},
},
{
Key: "a.[0]",
Err: errors.New("invalid key 'a.[0]'"),
},
{
Key: "a.0.b",
Err: errors.New("invalid key 'a.0.b'"),
Path: []Path{
{PathTypeKey, "a"},
{PathTypeKey, "0"},
{PathTypeKey, "b"},
},
},
{
Key: "a[0].b",
Expand Down Expand Up @@ -185,7 +193,12 @@ func TestSplitPath(t *testing.T) {
},
{
Key: "a[0].b.0",
Err: errors.New("invalid key 'a[0].b.0'"),
Path: []Path{
{PathTypeKey, "a"},
{PathTypeIndex, "0"},
{PathTypeKey, "b"},
{PathTypeKey, "0"},
},
},
}
for _, c := range testcases {
Expand All @@ -194,7 +207,7 @@ func TestSplitPath(t *testing.T) {
assert.Equal(t, err, c.Err)
continue
}
assert.Equal(t, p, c.Path)
assert.Equal(t, p, c.Path, fmt.Sprintf("key=%s", c.Key))
assert.Equal(t, JoinPath(p), c.Key)
}
}
6 changes: 0 additions & 6 deletions gs/examples/bookman/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/go-spring/spring-core/gs"
Expand Down Expand Up @@ -51,11 +50,6 @@ func init() {
}

func main() {
// Unset certain environment variables before running the application
_ = os.Unsetenv("_")
_ = os.Unsetenv("TERM")
_ = os.Unsetenv("TERM_SESSION_ID")

// Start the application and log errors if startup fails
if err := gs.Run(); err != nil {
syslog.Errorf("app run failed: %s", err.Error())
Expand Down
1 change: 1 addition & 0 deletions gs/internal/gs_app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func clean() {
func TestApp(t *testing.T) {

t.Run("os signals", func(t *testing.T) {
t.Skip()
t.Cleanup(clean)
app := NewApp()
go func() {
Expand Down
2 changes: 1 addition & 1 deletion gs/internal/gs_conf/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ func (c *Environment) CopyTo(p *conf.MutableProperties) error {
var propKey string
if strings.HasPrefix(k, prefix) {
propKey = strings.TrimPrefix(k, prefix)
propKey = strings.ToLower(replaceKey(propKey))
} else if matches(includeRex, k) && !matches(excludeRex, k) {
propKey = k
} else {
continue
}

propKey = strings.ToLower(replaceKey(propKey))
if err = p.Set(propKey, v); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions gs/internal/gs_conf/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestEnvironment(t *testing.T) {
err := NewEnvironment().CopyTo(props)
assert.Nil(t, err)
assert.Equal(t, props.Get("db.host"), "db1")
assert.Equal(t, props.Get("api.key"), "key123")
assert.Equal(t, props.Get("API_KEY"), "key123")
})

t.Run("custom prefix", func(t *testing.T) {
Expand All @@ -78,7 +78,7 @@ func TestEnvironment(t *testing.T) {
err := NewEnvironment().CopyTo(props)
assert.Nil(t, err)
assert.Equal(t, props.Get("db.host"), "db1")
assert.Equal(t, props.Get("api.key"), "key123")
assert.Equal(t, props.Get("API_KEY"), "key123")
})

t.Run("custom patterns", func(t *testing.T) {
Expand All @@ -95,8 +95,8 @@ func TestEnvironment(t *testing.T) {
props := conf.New()
err := NewEnvironment().CopyTo(props)
assert.Nil(t, err)
assert.Equal(t, "yes", props.Get("test.public"))
assert.False(t, props.Has("test.internal"))
assert.Equal(t, props.Get("TEST_PUBLIC"), "yes")
assert.False(t, props.Has("TEST_INTERNAL"))
})

t.Run("invalid regex - include", func(t *testing.T) {
Expand Down