Skip to content

Commit 05799fe

Browse files
factor out a runTestMatrix function
1 parent 6058e94 commit 05799fe

File tree

2 files changed

+38
-92
lines changed

2 files changed

+38
-92
lines changed
Lines changed: 4 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package tests
22

33
import (
4-
"strings"
54
"testing"
65

76
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8-
"github.com/hexops/autogold/v2"
97
"github.com/zclconf/go-cty/cty"
10-
11-
crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests"
128
)
139

1410
func TestSDKv2DetailedDiffString(t *testing.T) {
@@ -18,28 +14,7 @@ func TestSDKv2DetailedDiffString(t *testing.T) {
1814
schemaValueMakerPairs, scenarios := generateBaseTests(
1915
schema.TypeString, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
2016

21-
for _, schemaValueMakerPair := range schemaValueMakerPairs {
22-
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
23-
t.Parallel()
24-
for _, scenario := range scenarios {
25-
t.Run(scenario.name, func(t *testing.T) {
26-
if strings.Contains(schemaValueMakerPair.name, "required") &&
27-
(scenario.initialValue == nil || scenario.changeValue == nil) {
28-
t.Skip("Required fields cannot be unset")
29-
}
30-
t.Parallel()
31-
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
32-
autogold.ExpectFile(t, testOutput{
33-
initialValue: scenario.initialValue,
34-
changeValue: scenario.changeValue,
35-
tfOut: diff.TFOut,
36-
pulumiOut: diff.PulumiOut,
37-
detailedDiff: diff.PulumiDiff.DetailedDiff,
38-
})
39-
})
40-
}
41-
})
42-
}
17+
runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
4318
}
4419

4520
func TestSDKv2DetailedDiffBool(t *testing.T) {
@@ -49,28 +24,7 @@ func TestSDKv2DetailedDiffBool(t *testing.T) {
4924
schemaValueMakerPairs, scenarios := generateBaseTests(
5025
schema.TypeBool, cty.BoolVal, true, false, true, false, nilVal)
5126

52-
for _, schemaValueMakerPair := range schemaValueMakerPairs {
53-
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
54-
t.Parallel()
55-
for _, scenario := range scenarios {
56-
t.Run(scenario.name, func(t *testing.T) {
57-
if strings.Contains(schemaValueMakerPair.name, "required") &&
58-
(scenario.initialValue == nil || scenario.changeValue == nil) {
59-
t.Skip("Required fields cannot be unset")
60-
}
61-
t.Parallel()
62-
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
63-
autogold.ExpectFile(t, testOutput{
64-
initialValue: scenario.initialValue,
65-
changeValue: scenario.changeValue,
66-
tfOut: diff.TFOut,
67-
pulumiOut: diff.PulumiOut,
68-
detailedDiff: diff.PulumiDiff.DetailedDiff,
69-
})
70-
})
71-
}
72-
})
73-
}
27+
runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
7428
}
7529

7630
func TestSDKv2DetailedDiffInt(t *testing.T) {
@@ -80,28 +34,7 @@ func TestSDKv2DetailedDiffInt(t *testing.T) {
8034
schemaValueMakerPairs, scenarios := generateBaseTests(
8135
schema.TypeInt, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
8236

83-
for _, schemaValueMakerPair := range schemaValueMakerPairs {
84-
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
85-
t.Parallel()
86-
for _, scenario := range scenarios {
87-
t.Run(scenario.name, func(t *testing.T) {
88-
if strings.Contains(schemaValueMakerPair.name, "required") &&
89-
(scenario.initialValue == nil || scenario.changeValue == nil) {
90-
t.Skip("Required fields cannot be unset")
91-
}
92-
t.Parallel()
93-
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
94-
autogold.ExpectFile(t, testOutput{
95-
initialValue: scenario.initialValue,
96-
changeValue: scenario.changeValue,
97-
tfOut: diff.TFOut,
98-
pulumiOut: diff.PulumiOut,
99-
detailedDiff: diff.PulumiDiff.DetailedDiff,
100-
})
101-
})
102-
}
103-
})
104-
}
37+
runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
10538
}
10639

10740
func TestSDKv2DetailedDiffFloat(t *testing.T) {
@@ -111,26 +44,5 @@ func TestSDKv2DetailedDiffFloat(t *testing.T) {
11144
schemaValueMakerPairs, scenarios := generateBaseTests(
11245
schema.TypeFloat, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
11346

114-
for _, schemaValueMakerPair := range schemaValueMakerPairs {
115-
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
116-
t.Parallel()
117-
for _, scenario := range scenarios {
118-
t.Run(scenario.name, func(t *testing.T) {
119-
if strings.Contains(schemaValueMakerPair.name, "required") &&
120-
(scenario.initialValue == nil || scenario.changeValue == nil) {
121-
t.Skip("Required fields cannot be unset")
122-
}
123-
t.Parallel()
124-
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
125-
autogold.ExpectFile(t, testOutput{
126-
initialValue: scenario.initialValue,
127-
changeValue: scenario.changeValue,
128-
tfOut: diff.TFOut,
129-
pulumiOut: diff.PulumiOut,
130-
detailedDiff: diff.PulumiDiff.DetailedDiff,
131-
})
132-
})
133-
}
134-
})
135-
}
47+
runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
13648
}

pkg/tests/diff_test/value_makers.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package tests
22

33
import (
44
"context"
5+
"strings"
6+
"testing"
57

68
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
79
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hexops/autogold/v2"
811
"github.com/zclconf/go-cty/cty"
12+
13+
crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests"
914
)
1015

1116
func ref[T any](v T) *T {
@@ -24,6 +29,35 @@ type diffScenario[T any] struct {
2429
changeValue *T
2530
}
2631

32+
func runSDKv2TestMatrix[T any](
33+
t *testing.T, schemaValueMakerPairs []diffSchemaValueMakerPair[T], scenarios []diffScenario[T],
34+
) {
35+
for _, schemaValueMakerPair := range schemaValueMakerPairs {
36+
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
37+
t.Parallel()
38+
for _, scenario := range scenarios {
39+
t.Run(scenario.name, func(t *testing.T) {
40+
t.Parallel()
41+
if strings.Contains(schemaValueMakerPair.name, "required") &&
42+
(scenario.initialValue == nil || scenario.changeValue == nil) {
43+
t.Skip("Required fields cannot be unset")
44+
}
45+
diff := crosstests.Diff(
46+
t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue),
47+
schemaValueMakerPair.valueMaker(scenario.changeValue))
48+
autogold.ExpectFile(t, testOutput{
49+
initialValue: scenario.initialValue,
50+
changeValue: scenario.changeValue,
51+
tfOut: diff.TFOut,
52+
pulumiOut: diff.PulumiOut,
53+
detailedDiff: diff.PulumiDiff.DetailedDiff,
54+
})
55+
})
56+
}
57+
})
58+
}
59+
}
60+
2761
func generateBaseTests[T any](
2862
typ schema.ValueType, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T,
2963
) ([]diffSchemaValueMakerPair[T], []diffScenario[T]) {

0 commit comments

Comments
 (0)