@@ -57,48 +57,69 @@ func TestFrameworkProvider_impl(t *testing.T) {
57
57
var _ provider.ProviderWithMetaSchema = New ("test" )
58
58
}
59
59
60
- func TestFrameworkProvider_loadCredentialsFromFile (t * testing.T ) {
61
- cv := CredentialsValidator ()
62
-
63
- req := validator.StringRequest {
64
- ConfigValue : types .StringValue (testFakeCredentialsPath ),
65
- }
66
-
67
- resp := validator.StringResponse {
68
- Diagnostics : diag.Diagnostics {},
69
- }
70
-
71
- cv .ValidateString (context .Background (), req , & resp )
72
-
73
- if resp .Diagnostics .WarningsCount () > 0 {
74
- t .Errorf ("Expected 0 warnings, got %d" , resp .Diagnostics .WarningsCount ())
75
- }
76
- if resp .Diagnostics .HasError () {
77
- t .Errorf ("Expected 0 errors, got %d" , resp .Diagnostics .ErrorsCount ())
78
- }
79
- }
80
-
81
- func TestFrameworkProvider_loadCredentialsFromJSON (t * testing.T ) {
82
- contents , err := ioutil .ReadFile (testFakeCredentialsPath )
83
- if err != nil {
84
- t .Fatalf ("Unexpected error: %s" , err )
85
- }
86
- cv := CredentialsValidator ()
87
-
88
- req := validator.StringRequest {
89
- ConfigValue : types .StringValue (string (contents )),
90
- }
91
-
92
- resp := validator.StringResponse {
93
- Diagnostics : diag.Diagnostics {},
60
+ func TestFrameworkProvider_CredentialsValidator (t * testing.T ) {
61
+ cases := map [string ]struct {
62
+ ConfigValue func (t * testing.T ) types.String
63
+ ExpectedWarningCount int
64
+ ExpectedErrorCount int
65
+ }{
66
+ "configuring credentials as a path to a credentials JSON file is valid" : {
67
+ ConfigValue : func (t * testing.T ) types.String {
68
+ return types .StringValue (testFakeCredentialsPath ) // Path to a test fixture
69
+ },
70
+ },
71
+ "configuring credentials as a path to a non-existant file is NOT valid" : {
72
+ ConfigValue : func (t * testing.T ) types.String {
73
+ return types .StringValue ("./this/path/doesnt/exist.json" ) // Doesn't exist
74
+ },
75
+ ExpectedErrorCount : 1 ,
76
+ },
77
+ "configuring credentials as a credentials JSON string is valid" : {
78
+ ConfigValue : func (t * testing.T ) types.String {
79
+ contents , err := ioutil .ReadFile (testFakeCredentialsPath )
80
+ if err != nil {
81
+ t .Fatalf ("Unexpected error: %s" , err )
82
+ }
83
+ stringContents := string (contents )
84
+ return types .StringValue (stringContents )
85
+ },
86
+ },
87
+ "configuring credentials as an empty string is valid" : {
88
+ ConfigValue : func (t * testing.T ) types.String {
89
+ return types .StringValue ("" )
90
+ },
91
+ },
92
+ "leaving credentials unconfigured is valid" : {
93
+ ConfigValue : func (t * testing.T ) types.String {
94
+ return types .StringNull ()
95
+ },
96
+ },
94
97
}
95
98
96
- cv .ValidateString (context .Background (), req , & resp )
97
- if resp .Diagnostics .WarningsCount () > 0 {
98
- t .Errorf ("Expected 0 warnings, got %d" , resp .Diagnostics .WarningsCount ())
99
- }
100
- if resp .Diagnostics .HasError () {
101
- t .Errorf ("Expected 0 errors, got %d" , resp .Diagnostics .ErrorsCount ())
99
+ for tn , tc := range cases {
100
+ t .Run (tn , func (t * testing.T ) {
101
+ // Arrange
102
+ req := validator.StringRequest {
103
+ ConfigValue : tc .ConfigValue (t ),
104
+ }
105
+
106
+ resp := validator.StringResponse {
107
+ Diagnostics : diag.Diagnostics {},
108
+ }
109
+
110
+ cv := CredentialsValidator ()
111
+
112
+ // Act
113
+ cv .ValidateString (context .Background (), req , & resp )
114
+
115
+ // Assert
116
+ if resp .Diagnostics .WarningsCount () > tc .ExpectedWarningCount {
117
+ t .Errorf ("Expected %d warnings, got %d" , tc .ExpectedWarningCount , resp .Diagnostics .WarningsCount ())
118
+ }
119
+ if resp .Diagnostics .ErrorsCount () > tc .ExpectedErrorCount {
120
+ t .Errorf ("Expected %d errors, got %d" , tc .ExpectedErrorCount , resp .Diagnostics .ErrorsCount ())
121
+ }
122
+ })
102
123
}
103
124
}
104
125
0 commit comments