From fed64b7d6a24095917ea7823cf7f2886a95c39d2 Mon Sep 17 00:00:00 2001 From: Jonathan Edey Date: Mon, 2 Oct 2023 13:14:40 -0400 Subject: [PATCH 1/4] Mapped firebase storage emulator variable to gcloud equivalent --- storage/storage.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/storage/storage.go b/storage/storage.go index 3963a81f..3f1b9d1b 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -18,6 +18,7 @@ package storage import ( "context" "errors" + "os" "cloud.google.com/go/storage" "firebase.google.com/go/v4/internal" @@ -34,6 +35,9 @@ type Client struct { // This function can only be invoked from within the SDK. Client applications should access the // the Storage service through firebase.App. func NewClient(ctx context.Context, c *internal.StorageConfig) (*Client, error) { + if os.Getenv("STORAGE_EMULATOR_HOST") == "" && os.Getenv("FIREBASE_STORAGE_EMULATOR_HOST") != "" { + os.Setenv("STORAGE_EMULATOR_HOST", os.Getenv("FIREBASE_STORAGE_EMULATOR_HOST")) + } client, err := storage.NewClient(ctx, c.Opts...) if err != nil { return nil, err From f10290fa27f89ccb065908d85ff86b39ac20a441 Mon Sep 17 00:00:00 2001 From: Jonathan Edey Date: Mon, 2 Oct 2023 13:15:13 -0400 Subject: [PATCH 2/4] Added unit test --- storage/storage_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/storage/storage_test.go b/storage/storage_test.go index ab57f1dd..020ed28b 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -16,6 +16,7 @@ package storage import ( "context" + "os" "testing" "firebase.google.com/go/v4/internal" @@ -38,6 +39,23 @@ func TestNewClientError(t *testing.T) { } } +func TestNewClientEmulatorHostEnvVar(t *testing.T) { + emulatorHost := "localhost:9099" + os.Setenv("FIREBASE_STORAGE_EMULATOR_HOST", emulatorHost) + defer os.Unsetenv("FIREBASE_STORAGE_EMULATOR_HOST") + + _, err := NewClient(context.Background(), &internal.StorageConfig{ + Opts: opts, + }) + if err != nil { + t.Fatal(err) + } + + if host := os.Getenv("STORAGE_EMULATOR_HOST"); host != emulatorHost { + t.Errorf("emulator host: %q; want: %q", host, emulatorHost) + } +} + func TestNoBucketName(t *testing.T) { client, err := NewClient(context.Background(), &internal.StorageConfig{ Opts: opts, From b515b9e220ad0b0f33754f34cbbcb56ce3ef5b3e Mon Sep 17 00:00:00 2001 From: Jonathan Edey Date: Tue, 3 Oct 2023 12:22:56 -0400 Subject: [PATCH 3/4] fix: also unset STORAGE_EMULATOR_HOST after test --- storage/storage_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/storage/storage_test.go b/storage/storage_test.go index 020ed28b..a1530d1f 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -43,6 +43,7 @@ func TestNewClientEmulatorHostEnvVar(t *testing.T) { emulatorHost := "localhost:9099" os.Setenv("FIREBASE_STORAGE_EMULATOR_HOST", emulatorHost) defer os.Unsetenv("FIREBASE_STORAGE_EMULATOR_HOST") + defer os.Unsetenv("STORAGE_EMULATOR_HOST") _, err := NewClient(context.Background(), &internal.StorageConfig{ Opts: opts, From 4bd2b179b57e4968af70572deea2e9b66ee19fc5 Mon Sep 17 00:00:00 2001 From: Jonathan Edey Date: Tue, 3 Oct 2023 12:27:49 -0400 Subject: [PATCH 4/4] fix: unset STORAGE_EMULATOR_HOST before test as well --- storage/storage_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/storage/storage_test.go b/storage/storage_test.go index a1530d1f..f0eec7a5 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -43,6 +43,7 @@ func TestNewClientEmulatorHostEnvVar(t *testing.T) { emulatorHost := "localhost:9099" os.Setenv("FIREBASE_STORAGE_EMULATOR_HOST", emulatorHost) defer os.Unsetenv("FIREBASE_STORAGE_EMULATOR_HOST") + os.Unsetenv("STORAGE_EMULATOR_HOST") defer os.Unsetenv("STORAGE_EMULATOR_HOST") _, err := NewClient(context.Background(), &internal.StorageConfig{