This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudformation): add an option to override parameters (#36)
* feat(cloudformation): add an option to override parameters * chore: update comments * refactor: improve error handling * refactor: use strict mode when parsing a format like CF
- Loading branch information
Showing
7 changed files
with
497 additions
and
14 deletions.
There are no files selected for viewing
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,61 @@ | ||
package parser | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFileContext_OverrideParameters(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ctx FileContext | ||
arg map[string]any | ||
expected map[string]*Parameter | ||
}{ | ||
{ | ||
name: "happy", | ||
ctx: FileContext{ | ||
Parameters: map[string]*Parameter{ | ||
"BucketName": { | ||
inner: parameterInner{ | ||
Type: "String", | ||
Default: "test", | ||
}, | ||
}, | ||
"QueueName": { | ||
inner: parameterInner{ | ||
Type: "String", | ||
}, | ||
}, | ||
}, | ||
}, | ||
arg: map[string]any{ | ||
"BucketName": "test2", | ||
"QueueName": "test", | ||
"SomeKey": "some_value", | ||
}, | ||
expected: map[string]*Parameter{ | ||
"BucketName": { | ||
inner: parameterInner{ | ||
Type: "String", | ||
Default: "test2", | ||
}, | ||
}, | ||
"QueueName": { | ||
inner: parameterInner{ | ||
Type: "String", | ||
Default: "test", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.ctx.OverrideParameters(tt.arg) | ||
assert.Equal(t, tt.expected, tt.ctx.Parameters) | ||
}) | ||
} | ||
} |
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,89 @@ | ||
package parser | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParameters_UnmarshalJSON(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
source string | ||
expected Parameters | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "original format", | ||
source: `[ | ||
"Key1=Value1", | ||
"Key2=Value2" | ||
]`, | ||
expected: map[string]any{ | ||
"Key1": "Value1", | ||
"Key2": "Value2", | ||
}, | ||
}, | ||
{ | ||
name: "CloudFormation like format", | ||
source: `[ | ||
{ | ||
"ParameterKey": "Key1", | ||
"ParameterValue": "Value1" | ||
}, | ||
{ | ||
"ParameterKey": "Key2", | ||
"ParameterValue": "Value2" | ||
} | ||
]`, | ||
expected: map[string]any{ | ||
"Key1": "Value1", | ||
"Key2": "Value2", | ||
}, | ||
}, | ||
{ | ||
name: "CloudFormation like format, with unknown fields", | ||
source: `[ | ||
{ | ||
"ParameterKey": "Key1", | ||
"ParameterValue": "Value1" | ||
}, | ||
{ | ||
"ParameterKey": "Key2", | ||
"ParameterValue": "Value2", | ||
"UsePreviousValue": true | ||
} | ||
]`, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "CodePipeline like format", | ||
source: `{ | ||
"Parameters": { | ||
"Key1": "Value1", | ||
"Key2": "Value2" | ||
} | ||
}`, | ||
expected: map[string]any{ | ||
"Key1": "Value1", | ||
"Key2": "Value2", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var params Parameters | ||
|
||
err := json.Unmarshal([]byte(tt.source), ¶ms) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.expected, params) | ||
}) | ||
} | ||
} |
Oops, something went wrong.