-
Notifications
You must be signed in to change notification settings - Fork 45
Added test cases for BinaryParser #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RudharSingh
wants to merge
3
commits into
dineshba:main
Choose a base branch
from
RudharSingh:Rudhar_test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,160 @@ | ||
| // binary_parser_test.go | ||
| package parser | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/dineshba/tf-summarize/parser/mocks" | ||
| "github.com/golang/mock/gomock" | ||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| tfjson "github.com/hashicorp/terraform-json" | ||
| ) | ||
|
|
||
| // MockCommandExecutor mocks the command execution. | ||
| type MockCommandExecutor struct { | ||
| Output []byte | ||
| Err error | ||
| ActualName string | ||
| ActualArgs []string | ||
| } | ||
|
|
||
| func (e *MockCommandExecutor) CombinedOutput(name string, args ...string) ([]byte, error) { | ||
| e.ActualName = name | ||
| e.ActualArgs = args | ||
| return e.Output, e.Err | ||
| } | ||
|
|
||
| func TestBinaryParser_Parse_Success(t *testing.T) { | ||
| // Initialize GoMock controller | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| // Prepare mock output | ||
| mockPlan := tfjson.Plan{ | ||
| FormatVersion: "0.1", | ||
| TerraformVersion: "1.0.0", | ||
| } | ||
| output, err := json.Marshal(mockPlan) | ||
| assert.NoError(t, err, "Failed to marshal mock plan") | ||
|
|
||
| // Create mock executor | ||
| mockExecutor := mocks.NewMockCommandExecutor(ctrl) | ||
| tfbinary := "terraform" | ||
|
|
||
| // Set up expected call | ||
| mockExecutor. | ||
| EXPECT(). | ||
| CombinedOutput(tfbinary, "show", "-json", "mock-file"). | ||
| Return(output, nil) | ||
|
|
||
| // Create parser with mock executor | ||
| parser := BinaryParser{ | ||
| fileName: "mock-file", | ||
| executor: mockExecutor, | ||
| } | ||
|
|
||
| // Call Parse | ||
| parsedPlan, err := parser.Parse() | ||
|
|
||
| // assertions | ||
| assert.NoError(t, err, "Expected no error") | ||
| assert.Equal(t, mockPlan, parsedPlan) | ||
| } | ||
| func TestBinaryParser_Parse_EmptyOutput(t *testing.T) { | ||
| // Initialize GoMock controller | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| // Create mock executor | ||
| mockExecutor := mocks.NewMockCommandExecutor(ctrl) | ||
| tfbinary := "terraform" | ||
|
|
||
| // Set up expectation CombinedOutput will return empty output | ||
| mockExecutor. | ||
| EXPECT(). | ||
| CombinedOutput(tfbinary, "show", "-json", "mock-file"). | ||
| Return([]byte(""), nil) | ||
|
|
||
| // Create parser with mock executor | ||
| parser := BinaryParser{ | ||
| fileName: "mock-file", | ||
| executor: mockExecutor, | ||
| } | ||
|
|
||
| // Call Parse | ||
| _, err := parser.Parse() | ||
|
|
||
| // assertions | ||
| assert.Error(t, err, "Expected error due to empty output") | ||
| assert.Contains(t, err.Error(), "error when parsing input") | ||
| } | ||
|
|
||
| func TestBinaryParser_Parse_MissingRequiredFields(t *testing.T) { | ||
| // Initialize GoMock controller | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| // Prepare mock output with missing required fields | ||
| incompleteMockOutput := map[string]interface{}{ | ||
| // format_version and terraform_version are missing | ||
| "variables": map[string]interface{}{}, | ||
| } | ||
| output, err := json.Marshal(incompleteMockOutput) | ||
| assert.NoError(t, err, "Failed to marshal incomplete plan") | ||
|
|
||
| // Create mock executor | ||
| mockExecutor := mocks.NewMockCommandExecutor(ctrl) | ||
| tfbinary := "terraform" | ||
|
|
||
| // Set up expectation: CombinedOutput should return incomplete output | ||
| mockExecutor. | ||
| EXPECT(). | ||
| CombinedOutput(tfbinary, "show", "-json", "mock-file"). | ||
| Return(output, nil) | ||
|
|
||
| // Create parser with mock executor | ||
| parser := BinaryParser{ | ||
| fileName: "mock-file", | ||
| executor: mockExecutor, | ||
| } | ||
|
|
||
| // Call Parse | ||
| parsedPlan, err := parser.Parse() | ||
|
|
||
| // assertions | ||
| assert.Error(t, err, "Expected an error due to missing required fields") | ||
| assert.Contains(t, err.Error(), "format version is missing") | ||
| assert.Equal(t, "", parsedPlan.TerraformVersion) | ||
| } | ||
|
|
||
| func TestBinaryParser_Parse_InvalidJSONOutput(t *testing.T) { | ||
| // Initialize GoMock controller | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| // Prepare mock output with invalid json output | ||
| invalidJSON := []byte(`{"invalid-json"}`) | ||
| mockExecutor := mocks.NewMockCommandExecutor(ctrl) | ||
| tfbinary := "terraform" | ||
|
|
||
| // Set up expectation | ||
| mockExecutor. | ||
| EXPECT(). | ||
| CombinedOutput(tfbinary, "show", "-json", "mock-file"). | ||
| Return(invalidJSON, nil) | ||
|
|
||
| // Create parser with mock executor | ||
| parser := BinaryParser{ | ||
| fileName: "mock-file", | ||
| executor: mockExecutor, | ||
| } | ||
|
|
||
| // Call Parse | ||
| _, err := parser.Parse() | ||
|
|
||
| // Assertions | ||
| assert.Error(t, err, "Expected error due to invalid JSON output") | ||
| assert.Contains(t, err.Error(), "error when parsing input") | ||
| } | ||
This file contains hidden or 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,7 @@ | ||
| // command_executor.go | ||
| package parser | ||
|
|
||
| // CommandExecutor defines an interface for executing commands. | ||
| type CommandExecutor interface { | ||
| CombinedOutput(name string, args ...string) ([]byte, error) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.