Skip to content

Commit

Permalink
respect GAE_ENV=localdev
Browse files Browse the repository at this point in the history
  • Loading branch information
zevdg committed Jul 12, 2022
1 parent 0c7f7a8 commit 23e4728
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/identity_vm.go
Expand Up @@ -131,5 +131,5 @@ func fullyQualifiedAppID(_ netcontext.Context) string {
}

func IsDevAppServer() bool {
return os.Getenv("RUN_WITH_DEVAPPSERVER") != ""
return os.Getenv("RUN_WITH_DEVAPPSERVER") != "" || os.Getenv("GAE_ENV") == "localdev"
}
2 changes: 1 addition & 1 deletion v2/internal/identity.go
Expand Up @@ -167,5 +167,5 @@ func fullyQualifiedAppID(_ netcontext.Context) string {
}

func IsDevAppServer() bool {
return os.Getenv("RUN_WITH_DEVAPPSERVER") != ""
return os.Getenv("RUN_WITH_DEVAPPSERVER") != "" || os.Getenv("GAE_ENV") == "localdev"
}
47 changes: 47 additions & 0 deletions v2/internal/identity_test.go
@@ -0,0 +1,47 @@
package internal

import (
"os"
"testing"
)

func TestIsDevAppServer(t *testing.T) {
tests := []struct {
desc string // See http://go/gotip/episodes/25 for naming guidance.
env map[string]string
want bool
}{
{desc: "empty", env: map[string]string{}, want: false},
{desc: "legacy", env: map[string]string{"RUN_WITH_DEVAPPSERVER": "1"}, want: true},
{desc: "new", env: map[string]string{"GAE_ENV": "localdev"}, want: true},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
for key, value := range test.env {
defer setenv(t, key, value)()
}
if got := IsDevAppServer(); got != test.want {
t.Errorf("env=%v IsDevAppServer() got %v, want %v", test.env, got, test.want)
}
})
}
}

// setenv is a backport of https://pkg.go.dev/testing#T.Setenv
func setenv(t *testing.T, key, value string) func() {
t.Helper()
prevValue, ok := os.LookupEnv(key)

if err := os.Setenv(key, value); err != nil {
t.Fatalf("cannot set environment variable: %v", err)
}

if ok {
return func() {
os.Setenv(key, prevValue)
}
}
return func() {
os.Unsetenv(key)
}
}

0 comments on commit 23e4728

Please sign in to comment.