Skip to content

Commit df1d435

Browse files
ei-gradclaude
andcommitted
test: Add SDK validation proving Optional is required for DiffSuppressFunc
The SDK explicitly rejects DiffSuppressFunc on Computed-only fields (InternalValidate returns: "DiffSuppressFunc is for suppressing differences between config and state representation. There is no config for computed-only field, nothing to compare."). Add tests demonstrating this SDK constraint, and verify all 29 resources pass InternalValidate with their current schema. Co-Authored-By: Claude <[email protected]>
1 parent 1dafd39 commit df1d435

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

github/resource_github_etag_unit_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -71,6 +72,60 @@ func TestEtagDiffSuppressFunction(t *testing.T) {
7172
}
7273
}
7374

75+
// TestEtagComputedOnlyRejectsSuppress verifies that the SDK rejects
76+
// DiffSuppressFunc on Computed-only fields, proving Optional is required.
77+
func TestEtagComputedOnlyRejectsSuppress(t *testing.T) {
78+
p := &schema.Provider{
79+
ResourcesMap: map[string]*schema.Resource{
80+
"test": {
81+
Schema: map[string]*schema.Schema{
82+
"etag": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
DiffSuppressFunc: func(k, o, n string, d *schema.ResourceData) bool {
86+
return true
87+
},
88+
DiffSuppressOnRefresh: true,
89+
},
90+
},
91+
},
92+
},
93+
}
94+
err := p.InternalValidate()
95+
if err == nil {
96+
t.Fatal("expected SDK to reject DiffSuppressFunc on Computed-only field")
97+
}
98+
if !strings.Contains(err.Error(), "DiffSuppressFunc") {
99+
t.Fatalf("unexpected error: %s", err)
100+
}
101+
}
102+
103+
// TestEtagOptionalComputedAcceptsSuppress verifies that the SDK accepts
104+
// DiffSuppressFunc on Optional+Computed fields.
105+
func TestEtagOptionalComputedAcceptsSuppress(t *testing.T) {
106+
p := &schema.Provider{
107+
ResourcesMap: map[string]*schema.Resource{
108+
"test": {
109+
Schema: map[string]*schema.Schema{
110+
"etag": {
111+
Type: schema.TypeString,
112+
Optional: true,
113+
Computed: true,
114+
DiffSuppressFunc: func(k, o, n string, d *schema.ResourceData) bool {
115+
return true
116+
},
117+
DiffSuppressOnRefresh: true,
118+
},
119+
},
120+
},
121+
},
122+
}
123+
err := p.InternalValidate()
124+
if err != nil {
125+
t.Fatalf("Optional+Computed with DiffSuppressFunc should be valid, got: %s", err)
126+
}
127+
}
128+
74129
// TestEtagSchemaConsistency ensure DiffSuppressFunc and DiffSuppressOnRefresh are consistently applied.
75130
func TestEtagSchemaConsistency(t *testing.T) {
76131
resourcesWithEtag := map[string]*schema.Resource{
@@ -136,6 +191,16 @@ func TestEtagSchemaConsistency(t *testing.T) {
136191
t.Errorf("DiffSuppressFunc should return true in %s", resourceName)
137192
}
138193
}
194+
195+
// Verify the schema passes SDK internal validation
196+
p := &schema.Provider{
197+
ResourcesMap: map[string]*schema.Resource{
198+
resourceName: resource,
199+
},
200+
}
201+
if err := p.InternalValidate(); err != nil {
202+
t.Errorf("SDK validation failed for %s: %s", resourceName, err)
203+
}
139204
})
140205
}
141206
}

0 commit comments

Comments
 (0)