Repository: caicloud/nirvana
Pull request #403 (2020-11-30 05:10:57)

[release-0.3] fix(*): make modifier ProduceAllIfProducesIsEmpty also modify ErrorProduces
This is an automated cherry-pick of #402

/assign iawia002

caicloud-bot: @caicloud-bot: Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our [release note process](https://git.k8s.io/community/contributors/guide/release-notes.md) to remove it.

<details>

Instructions for interacting with me using PR comments are available [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md).  If you have questions or suggestions related to my behavior, please file an issue against the [caicloud/tools](https://github.com/caicloud/tools/issues/new?title=Prow%20issue:) repository.
</details>
caicloud-bot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: 
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: **iawia002**

Assign the PR to them by writing `/assign @iawia002` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md#bot-commands).

The pull request process is described [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md#typical-workflow)

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/caicloud/nirvana/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["iawia002"]} -->
iawia002: /release-note-none
/lgtm
caicloud-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/caicloud/nirvana/pull/403#issuecomment-735567094" title="LGTM">iawia002</a>*

The full list of commands accepted by this bot can be found [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md#bot-commands).

The pull request process is described [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md#typical-workflow)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/caicloud/nirvana/blob/master/OWNERS)~~ [iawia002]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/service/modifier.go b/service/modifier.go
index 53eee95..73dadc1 100644
--- a/service/modifier.go
+++ b/service/modifier.go
@@ -69,13 +69,16 @@ func ConsumeAllIfConsumesIsEmpty() DefinitionModifier {
 	}
 }
 
-// ProduceAllIfProducesIsEmpty adds definition.MIMEAll to consumes if consumes
-// is empty.
+// ProduceAllIfProducesIsEmpty adds definition.MIMEAll to produces and errorProduces if
+// they are empty.
 func ProduceAllIfProducesIsEmpty() DefinitionModifier {
 	return func(d *definition.Definition) {
-		if len(d.Produces) <= 0 {
+		if len(d.Produces) == 0 {
 			d.Produces = []string{definition.MIMEAll}
 		}
+		if len(d.ErrorProduces) == 0 {
+			d.ErrorProduces = []string{definition.MIMEAll}
+		}
 	}
 }
 


Test patch:
diff --git a/service/modfier_test.go b/service/modfier_test.go
new file mode 100644
index 0000000..56314ad
--- /dev/null
+++ b/service/modfier_test.go
@@ -0,0 +1,42 @@
+/*
+Copyright 2020 Caicloud Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package service
+
+import (
+	"testing"
+
+	"github.com/caicloud/nirvana/definition"
+)
+
+func TestProduceAllIfProducesIsEmpty(t *testing.T) {
+	d := definition.Definition{
+		Summary:     "Test",
+		Method:      definition.Create,
+		Description: "test",
+		Results:     definition.DataErrorResults("data"),
+		Function: func() (interface{}, error) {
+			return nil, nil
+		},
+	}
+	ProduceAllIfProducesIsEmpty()(&d)
+	if len(d.Produces) == 0 {
+		t.Errorf("Produces should not be empty")
+	}
+	if len(d.ErrorProduces) == 0 {
+		t.Errorf("ErrorProduces should not be empty")
+	}
+}