Skip to content

Commit 828bf9f

Browse files
Move validator tests into their own explicit file (#11590) (#8115)
[upstream:94d7abdcfa1c272f1e62d3859edca54e181959aa] Signed-off-by: Modular Magician <[email protected]>
1 parent 38cced9 commit 828bf9f

File tree

3 files changed

+90
-76
lines changed

3 files changed

+90
-76
lines changed

.changelog/11590.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,14 @@
11
// Copyright (c) HashiCorp, Inc.
22
// SPDX-License-Identifier: MPL-2.0
3-
package fwprovider
3+
package fwprovider_test
44

55
import (
6-
"context"
7-
"io/ioutil"
86
"testing"
97

10-
"github.com/hashicorp/terraform-plugin-framework/diag"
118
"github.com/hashicorp/terraform-plugin-framework/provider"
12-
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
13-
"github.com/hashicorp/terraform-plugin-framework/types"
14-
15-
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
9+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/fwprovider"
1610
)
1711

1812
func TestFrameworkProvider_impl(t *testing.T) {
19-
var _ provider.ProviderWithMetaSchema = New()
20-
}
21-
22-
func TestFrameworkProvider_CredentialsValidator(t *testing.T) {
23-
cases := map[string]struct {
24-
ConfigValue func(t *testing.T) types.String
25-
ExpectedWarningCount int
26-
ExpectedErrorCount int
27-
}{
28-
"configuring credentials as a path to a credentials JSON file is valid": {
29-
ConfigValue: func(t *testing.T) types.String {
30-
return types.StringValue(transport_tpg.TestFakeCredentialsPath) // Path to a test fixture
31-
},
32-
},
33-
"configuring credentials as a path to a non-existant file is NOT valid": {
34-
ConfigValue: func(t *testing.T) types.String {
35-
return types.StringValue("./this/path/doesnt/exist.json") // Doesn't exist
36-
},
37-
ExpectedErrorCount: 1,
38-
},
39-
"configuring credentials as a credentials JSON string is valid": {
40-
ConfigValue: func(t *testing.T) types.String {
41-
contents, err := ioutil.ReadFile(transport_tpg.TestFakeCredentialsPath)
42-
if err != nil {
43-
t.Fatalf("Unexpected error: %s", err)
44-
}
45-
stringContents := string(contents)
46-
return types.StringValue(stringContents)
47-
},
48-
},
49-
"configuring credentials as an empty string is not valid": {
50-
ConfigValue: func(t *testing.T) types.String {
51-
return types.StringValue("")
52-
},
53-
ExpectedErrorCount: 1,
54-
},
55-
"leaving credentials unconfigured is valid": {
56-
ConfigValue: func(t *testing.T) types.String {
57-
return types.StringNull()
58-
},
59-
},
60-
}
61-
62-
for tn, tc := range cases {
63-
t.Run(tn, func(t *testing.T) {
64-
// Arrange
65-
req := validator.StringRequest{
66-
ConfigValue: tc.ConfigValue(t),
67-
}
68-
69-
resp := validator.StringResponse{
70-
Diagnostics: diag.Diagnostics{},
71-
}
72-
73-
cv := CredentialsValidator()
74-
75-
// Act
76-
cv.ValidateString(context.Background(), req, &resp)
77-
78-
// Assert
79-
if resp.Diagnostics.WarningsCount() > tc.ExpectedWarningCount {
80-
t.Errorf("Expected %d warnings, got %d", tc.ExpectedWarningCount, resp.Diagnostics.WarningsCount())
81-
}
82-
if resp.Diagnostics.ErrorsCount() > tc.ExpectedErrorCount {
83-
t.Errorf("Expected %d errors, got %d", tc.ExpectedErrorCount, resp.Diagnostics.ErrorsCount())
84-
}
85-
})
86-
}
13+
var _ provider.ProviderWithMetaSchema = fwprovider.New()
8714
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package fwprovider_test
4+
5+
import (
6+
"context"
7+
"io/ioutil"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-framework/diag"
11+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
12+
"github.com/hashicorp/terraform-plugin-framework/types"
13+
14+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/fwprovider"
15+
16+
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
17+
)
18+
19+
func TestFrameworkProvider_CredentialsValidator(t *testing.T) {
20+
cases := map[string]struct {
21+
ConfigValue func(t *testing.T) types.String
22+
ExpectedWarningCount int
23+
ExpectedErrorCount int
24+
}{
25+
"configuring credentials as a path to a credentials JSON file is valid": {
26+
ConfigValue: func(t *testing.T) types.String {
27+
return types.StringValue(transport_tpg.TestFakeCredentialsPath) // Path to a test fixture
28+
},
29+
},
30+
"configuring credentials as a path to a non-existant file is NOT valid": {
31+
ConfigValue: func(t *testing.T) types.String {
32+
return types.StringValue("./this/path/doesnt/exist.json") // Doesn't exist
33+
},
34+
ExpectedErrorCount: 1,
35+
},
36+
"configuring credentials as a credentials JSON string is valid": {
37+
ConfigValue: func(t *testing.T) types.String {
38+
contents, err := ioutil.ReadFile(transport_tpg.TestFakeCredentialsPath)
39+
if err != nil {
40+
t.Fatalf("Unexpected error: %s", err)
41+
}
42+
stringContents := string(contents)
43+
return types.StringValue(stringContents)
44+
},
45+
},
46+
"configuring credentials as an empty string is not valid": {
47+
ConfigValue: func(t *testing.T) types.String {
48+
return types.StringValue("")
49+
},
50+
ExpectedErrorCount: 1,
51+
},
52+
"leaving credentials unconfigured is valid": {
53+
ConfigValue: func(t *testing.T) types.String {
54+
return types.StringNull()
55+
},
56+
},
57+
}
58+
59+
for tn, tc := range cases {
60+
t.Run(tn, func(t *testing.T) {
61+
// Arrange
62+
req := validator.StringRequest{
63+
ConfigValue: tc.ConfigValue(t),
64+
}
65+
66+
resp := validator.StringResponse{
67+
Diagnostics: diag.Diagnostics{},
68+
}
69+
70+
cv := fwprovider.CredentialsValidator()
71+
72+
// Act
73+
cv.ValidateString(context.Background(), req, &resp)
74+
75+
// Assert
76+
if resp.Diagnostics.WarningsCount() > tc.ExpectedWarningCount {
77+
t.Errorf("Expected %d warnings, got %d", tc.ExpectedWarningCount, resp.Diagnostics.WarningsCount())
78+
}
79+
if resp.Diagnostics.ErrorsCount() > tc.ExpectedErrorCount {
80+
t.Errorf("Expected %d errors, got %d", tc.ExpectedErrorCount, resp.Diagnostics.ErrorsCount())
81+
}
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)