Repository: zeromicro/go-zero
Pull request #3009 (2023-03-10 12:31:10)

chore: add more tests

codecov[bot]: ## [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3009](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (153ce70) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/3a493cd6a6898c0efd9c481557d9d2d661210770?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (3a493cd) will **increase** coverage by `0.04%`.
> The diff coverage is `86.36%`.

> :exclamation: Current head 153ce70 differs from pull request most recent head e61bd1d. Consider uploading reports for the commit e61bd1d to get more accurate results


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/fs/files.go](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9mcy9maWxlcy5nbw==) | `100.00% <ø> (ø)` | |
| [core/proc/goroutines.go](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9wcm9jL2dvcm91dGluZXMuZ28=) | `78.57% <ø> (ø)` | |
| [core/proc/profile.go](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9wcm9jL3Byb2ZpbGUuZ28=) | `78.78% <ø> (ø)` | |
| [core/proc/shutdown.go](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9wcm9jL3NodXRkb3duLmdv) | `72.72% <ø> (ø)` | |
| [core/proc/signals.go](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9wcm9jL3NpZ25hbHMuZ28=) | `43.33% <ø> (ø)` | |
| [core/stat/alert.go](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdGF0L2FsZXJ0Lmdv) | `70.00% <ø> (ø)` | |
| [core/stat/internal/cgroup\_linux.go](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdGF0L2ludGVybmFsL2Nncm91cF9saW51eC5nbw==) | `28.16% <0.00%> (ø)` | |
| [gateway/internal/descriptorsource.go](https://codecov.io/gh/zeromicro/go-zero/pull/3009?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Z2F0ZXdheS9pbnRlcm5hbC9kZXNjcmlwdG9yc291cmNlLmdv) | `88.23% <90.47%> (+11.76%)` | :arrow_up: |


Patch:
diff --git a/gateway/internal/descriptorsource.go b/gateway/internal/descriptorsource.go
index 8e1895bc..9b3abbd3 100644
--- a/gateway/internal/descriptorsource.go
+++ b/gateway/internal/descriptorsource.go
@@ -37,52 +37,51 @@ func GetMethods(source grpcurl.DescriptorSource) ([]Method, error) {
 			for _, method := range svcMethods {
 				rpcPath := fmt.Sprintf("%s/%s", svc, method.GetName())
 				ext := proto.GetExtension(method.GetMethodOptions(), annotations.E_Http)
-				if ext == nil {
-					methods = append(methods, Method{
-						RpcPath: rpcPath,
-					})
-					continue
-				}
-
-				httpExt, ok := ext.(*annotations.HttpRule)
-				if !ok {
-					methods = append(methods, Method{
-						RpcPath: rpcPath,
-					})
-					continue
-				}
+				switch rule := ext.(type) {
+				case *annotations.HttpRule:
+					if rule == nil {
+						methods = append(methods, Method{
+							RpcPath: rpcPath,
+						})
+						continue
+					}
 
-				switch rule := httpExt.GetPattern().(type) {
-				case *annotations.HttpRule_Get:
-					methods = append(methods, Method{
-						HttpMethod: http.MethodGet,
-						HttpPath:   adjustHttpPath(rule.Get),
-						RpcPath:    rpcPath,
-					})
-				case *annotations.HttpRule_Post:
-					methods = append(methods, Method{
-						HttpMethod: http.MethodPost,
-						HttpPath:   adjustHttpPath(rule.Post),
-						RpcPath:    rpcPath,
-					})
-				case *annotations.HttpRule_Put:
-					methods = append(methods, Method{
-						HttpMethod: http.MethodPut,
-						HttpPath:   adjustHttpPath(rule.Put),
-						RpcPath:    rpcPath,
-					})
-				case *annotations.HttpRule_Delete:
-					methods = append(methods, Method{
-						HttpMethod: http.MethodDelete,
-						HttpPath:   adjustHttpPath(rule.Delete),
-						RpcPath:    rpcPath,
-					})
-				case *annotations.HttpRule_Patch:
-					methods = append(methods, Method{
-						HttpMethod: http.MethodPatch,
-						HttpPath:   adjustHttpPath(rule.Patch),
-						RpcPath:    rpcPath,
-					})
+					switch httpRule := rule.GetPattern().(type) {
+					case *annotations.HttpRule_Get:
+						methods = append(methods, Method{
+							HttpMethod: http.MethodGet,
+							HttpPath:   adjustHttpPath(httpRule.Get),
+							RpcPath:    rpcPath,
+						})
+					case *annotations.HttpRule_Post:
+						methods = append(methods, Method{
+							HttpMethod: http.MethodPost,
+							HttpPath:   adjustHttpPath(httpRule.Post),
+							RpcPath:    rpcPath,
+						})
+					case *annotations.HttpRule_Put:
+						methods = append(methods, Method{
+							HttpMethod: http.MethodPut,
+							HttpPath:   adjustHttpPath(httpRule.Put),
+							RpcPath:    rpcPath,
+						})
+					case *annotations.HttpRule_Delete:
+						methods = append(methods, Method{
+							HttpMethod: http.MethodDelete,
+							HttpPath:   adjustHttpPath(httpRule.Delete),
+							RpcPath:    rpcPath,
+						})
+					case *annotations.HttpRule_Patch:
+						methods = append(methods, Method{
+							HttpMethod: http.MethodPatch,
+							HttpPath:   adjustHttpPath(httpRule.Patch),
+							RpcPath:    rpcPath,
+						})
+					default:
+						methods = append(methods, Method{
+							RpcPath: rpcPath,
+						})
+					}
 				default:
 					methods = append(methods, Method{
 						RpcPath: rpcPath,


Test patch:
diff --git a/gateway/internal/descriptorsource_test.go b/gateway/internal/descriptorsource_test.go
index d0957132..2b84452e 100644
--- a/gateway/internal/descriptorsource_test.go
+++ b/gateway/internal/descriptorsource_test.go
@@ -2,11 +2,13 @@ package internal
 
 import (
 	"encoding/base64"
+	"errors"
 	"net/http"
 	"os"
 	"testing"
 
 	"github.com/fullstorydev/grpcurl"
+	"github.com/jhump/protoreflect/desc"
 	"github.com/stretchr/testify/assert"
 	"github.com/zeromicro/go-zero/core/hash"
 )
@@ -75,3 +77,50 @@ func TestGetMethodsWithAnnotations(t *testing.T) {
 		},
 	}, methods)
 }
+
+func TestGetMethodsBadCases(t *testing.T) {
+	t.Run("no services", func(t *testing.T) {
+		source := &mockDescriptorSource{
+			servicesErr: errors.New("no services"),
+		}
+		_, err := GetMethods(source)
+		assert.NotNil(t, err)
+	})
+
+	t.Run("no symbol in services", func(t *testing.T) {
+		source := &mockDescriptorSource{
+			services:  []string{"hello.Hello"},
+			symbolErr: errors.New("no symbol"),
+		}
+		_, err := GetMethods(source)
+		assert.NotNil(t, err)
+	})
+
+	t.Run("no symbol in services", func(t *testing.T) {
+		source := &mockDescriptorSource{
+			services:  []string{"hello.Hello"},
+			symbolErr: errors.New("no symbol"),
+		}
+		_, err := GetMethods(source)
+		assert.NotNil(t, err)
+	})
+}
+
+type mockDescriptorSource struct {
+	symbolDesc  desc.Descriptor
+	symbolErr   error
+	services    []string
+	servicesErr error
+}
+
+func (m *mockDescriptorSource) AllExtensionsForType(_ string) ([]*desc.FieldDescriptor, error) {
+	return nil, nil
+}
+
+func (m *mockDescriptorSource) FindSymbol(_ string) (desc.Descriptor, error) {
+	return m.symbolDesc, m.symbolErr
+}
+
+func (m *mockDescriptorSource) ListServices() ([]string, error) {
+	return m.services, m.servicesErr
+}