-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tfprotov5+tfprotov6: Cleanup and unit test fromproto/toproto packages (…
…#367) Reference: #365 Adjusts the following packages: * `tfprotov5/internal/fromproto` * `tfprotov5/internal/tf5server` * `tfprotov5/internal/toproto` * `tfprotov6/internal/fromproto` * `tfprotov6/internal/tf6server` * `tfprotov6/internal/toproto` With the following changes: - Removed unnecessary response types from `fromproto` - Removed unnecessary request types from `toproto` - Added unit testing for all functionality - Added nil panic protection for all functionality and removed extraneous nil checking in callers (e.g. around `DynamicValue` handling) - Clarified protocol request/response naming in the servers - Added vertical whitespace for readability and consistency As noted in some of the new unit testing, many of the response types can also have their error returns removed, but this will be saved for a followup change to somewhat reduce this already large refactoring.
- Loading branch information
Showing
82 changed files
with
9,740 additions
and
2,366 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package fromproto_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/fromproto" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5" | ||
) | ||
|
||
func TestReadDataSourceRequest(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
in *tfplugin5.ReadDataSource_Request | ||
expected *tfprotov5.ReadDataSourceRequest | ||
}{ | ||
"nil": { | ||
in: nil, | ||
expected: nil, | ||
}, | ||
"zero": { | ||
in: &tfplugin5.ReadDataSource_Request{}, | ||
expected: &tfprotov5.ReadDataSourceRequest{}, | ||
}, | ||
"Config": { | ||
in: &tfplugin5.ReadDataSource_Request{ | ||
Config: testTfplugin5DynamicValue(), | ||
}, | ||
expected: &tfprotov5.ReadDataSourceRequest{ | ||
Config: testTfprotov5DynamicValue(), | ||
}, | ||
}, | ||
"ProviderMeta": { | ||
in: &tfplugin5.ReadDataSource_Request{ | ||
ProviderMeta: testTfplugin5DynamicValue(), | ||
}, | ||
expected: &tfprotov5.ReadDataSourceRequest{ | ||
ProviderMeta: testTfprotov5DynamicValue(), | ||
}, | ||
}, | ||
"TypeName": { | ||
in: &tfplugin5.ReadDataSource_Request{ | ||
TypeName: "test", | ||
}, | ||
expected: &tfprotov5.ReadDataSourceRequest{ | ||
TypeName: "test", | ||
}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
name, testCase := name, testCase | ||
|
||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := fromproto.ReadDataSourceRequest(testCase.in) | ||
|
||
if diff := cmp.Diff(got, testCase.expected); diff != "" { | ||
t.Errorf("unexpected difference: %s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestValidateDataSourceConfigRequest(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
in *tfplugin5.ValidateDataSourceConfig_Request | ||
expected *tfprotov5.ValidateDataSourceConfigRequest | ||
}{ | ||
"nil": { | ||
in: nil, | ||
expected: nil, | ||
}, | ||
"zero": { | ||
in: &tfplugin5.ValidateDataSourceConfig_Request{}, | ||
expected: &tfprotov5.ValidateDataSourceConfigRequest{}, | ||
}, | ||
"Config": { | ||
in: &tfplugin5.ValidateDataSourceConfig_Request{ | ||
Config: testTfplugin5DynamicValue(), | ||
}, | ||
expected: &tfprotov5.ValidateDataSourceConfigRequest{ | ||
Config: testTfprotov5DynamicValue(), | ||
}, | ||
}, | ||
"TypeName": { | ||
in: &tfplugin5.ValidateDataSourceConfig_Request{ | ||
TypeName: "test", | ||
}, | ||
expected: &tfprotov5.ValidateDataSourceConfigRequest{ | ||
TypeName: "test", | ||
}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
name, testCase := name, testCase | ||
|
||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := fromproto.ValidateDataSourceConfigRequest(testCase.in) | ||
|
||
if diff := cmp.Diff(got, testCase.expected); diff != "" { | ||
t.Errorf("unexpected difference: %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
// Package fromproto converts Protocol Buffers generated tfplugin5 types into | ||
// terraform-plugin-go tfprotov5 types. | ||
package fromproto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package fromproto_test | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/toproto" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func testTfplugin5DynamicValue() *tfplugin5.DynamicValue { | ||
return toproto.DynamicValue(testTfprotov5DynamicValue()) | ||
} | ||
|
||
func testTfprotov5DynamicValue() *tfprotov5.DynamicValue { | ||
dynamicValue, err := tfprotov5.NewDynamicValue( | ||
tftypes.Object{}, | ||
tftypes.NewValue(tftypes.Object{}, nil), | ||
) | ||
|
||
if err != nil { | ||
panic("unable to create DynamicValue: " + err.Error()) | ||
} | ||
|
||
return &dynamicValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.