Skip to content

Commit

Permalink
fix: Map Firebase FIREBASE_STORAGE_EMULATOR_HOST to the Cloud API `…
Browse files Browse the repository at this point in the history
…STORAGE_EMULATOR_HOST` (#588)

* Mapped firebase storage emulator variable to gcloud equivalent

* Added unit test

* fix: also unset STORAGE_EMULATOR_HOST after test

* fix: unset STORAGE_EMULATOR_HOST before test as well
  • Loading branch information
jonathanedey committed Oct 3, 2023
1 parent 5e861fd commit 96101fd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package storage
import (
"context"
"errors"
"os"

"cloud.google.com/go/storage"
"firebase.google.com/go/v4/internal"
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package storage

import (
"context"
"os"
"testing"

"firebase.google.com/go/v4/internal"
Expand All @@ -38,6 +39,25 @@ 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")
os.Unsetenv("STORAGE_EMULATOR_HOST")
defer os.Unsetenv("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,
Expand Down

0 comments on commit 96101fd

Please sign in to comment.