diff --git a/Makefile b/Makefile index 23b36978..42a96e2a 100644 --- a/Makefile +++ b/Makefile @@ -74,7 +74,7 @@ fmt: .PHONY: test # we say code is not worth testing unless it's formatted test: fmt codegen - go test -v -cover -coverprofile ./coverage.out ./cloud/... $(TEST_ARGS) + go test -v -cover -coverprofile ./coverage.out ./cloud/... ./sentry/... $(TEST_ARGS) .PHONY: build-linux build-linux: codegen diff --git a/sentry/sentry_test.go b/sentry/sentry_test.go new file mode 100644 index 00000000..b26dc9d7 --- /dev/null +++ b/sentry/sentry_test.go @@ -0,0 +1,195 @@ +package sentry + +import ( + "context" + "testing" + + "github.com/getsentry/sentry-go" + "github.com/stretchr/testify/assert" +) + +func TestInitialize(t *testing.T) { + // Reset the initialized flag before each test + initialized = false + + tests := []struct { + name string + dsn string + environment string + release string + wantErr bool + }{ + { + name: "successful initialization", + dsn: "https://test@sentry.io/123", + environment: "test", + release: "1.0.0", + wantErr: false, + }, + { + name: "empty DSN", + dsn: "", + environment: "test", + release: "1.0.0", + wantErr: true, + }, + { + name: "double initialization", + dsn: "https://test@sentry.io/123", + environment: "test", + release: "1.0.0", + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := Initialize(tt.dsn, tt.environment, tt.release) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.True(t, initialized) + } + }) + } +} + +func TestSetHubOnContext(t *testing.T) { + // Reset the initialized flag + initialized = false + _ = Initialize("https://test@sentry.io/123", "test", "1.0.0") + + ctx := context.Background() + newCtx := SetHubOnContext(ctx) + + assert.True(t, sentry.HasHubOnContext(newCtx)) + assert.NotNil(t, sentry.GetHubFromContext(newCtx)) +} + +func TestGetHubFromContext(t *testing.T) { + tests := []struct { + name string + setupFunc func() context.Context + initialized bool + wantNil bool + }{ + { + name: "valid hub in context", + setupFunc: func() context.Context { + ctx := context.Background() + return SetHubOnContext(ctx) + }, + initialized: true, + wantNil: false, + }, + { + name: "no hub in context", + setupFunc: func() context.Context { + return context.Background() + }, + initialized: true, + wantNil: true, + }, + { + name: "sentry not initialized", + setupFunc: func() context.Context { + return context.Background() + }, + initialized: false, + wantNil: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Reset the initialized flag + initialized = false + if tt.initialized { + _ = Initialize("https://test@sentry.io/123", "test", "1.0.0") + } + + ctx := tt.setupFunc() + hub := getHubFromContext(ctx) + + if tt.wantNil { + assert.Nil(t, hub) + } else { + assert.NotNil(t, hub) + } + }) + } +} + +func TestSetTag(t *testing.T) { + // Reset the initialized flag + initialized = false + _ = Initialize("https://test@sentry.io/123", "test", "1.0.0") + + tests := []struct { + name string + setupFunc func() context.Context + key string + value string + }{ + { + name: "set tag with valid hub", + setupFunc: func() context.Context { + return SetHubOnContext(context.Background()) + }, + key: "test-key", + value: "test-value", + }, + { + name: "set tag with no hub", + setupFunc: func() context.Context { + return context.Background() + }, + key: "test-key", + value: "test-value", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := tt.setupFunc() + // This should not panic + SetTag(ctx, tt.key, tt.value) + }) + } +} + +func TestCaptureError(t *testing.T) { + // Reset the initialized flag + initialized = false + _ = Initialize("https://test@sentry.io/123", "test", "1.0.0") + + tests := []struct { + name string + setupFunc func() context.Context + err error + }{ + { + name: "capture error with valid hub", + setupFunc: func() context.Context { + return SetHubOnContext(context.Background()) + }, + err: assert.AnError, + }, + { + name: "capture error with no hub", + setupFunc: func() context.Context { + return context.Background() + }, + err: assert.AnError, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := tt.setupFunc() + // This should not panic + CaptureError(ctx, tt.err) + }) + } +}