Skip to content

Commit

Permalink
Split Test_NewResources into smaller tests
Browse files Browse the repository at this point in the history
We used to have a function called NewResources that we later split into
smaller functions -- ResolveParams and ResolveResources. However, in our
unit tests, we kept the Test_NewResources function leading to tests that
were more verbose/needed more setup than necessary. This commit splits
the tests into smaller more targeted tests.

Fixes tektoncd#252

Signed-off-by: Dibyo Mukherjee <[email protected]>
  • Loading branch information
dibyom committed Jan 7, 2020
1 parent 9ace5c1 commit 7e38ba4
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 357 deletions.
16 changes: 9 additions & 7 deletions pkg/template/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ func ResolveParams(bindings []*triggersv1.TriggerBinding, body []byte, header ht
if err != nil {
return nil, fmt.Errorf("error merging trigger params: %w", err)
}
event, err := newEvent(body, header)
if err != nil {
return nil, fmt.Errorf("failed to create Event: %w", err)
}
out, err = applyEventValuesToParams(out, event)

out, err = applyEventValuesToParams(out, body, header)
if err != nil {
return nil, fmt.Errorf("failed to ApplyEventValuesToParams: %w", err)
}
Expand Down Expand Up @@ -82,13 +79,18 @@ func newEvent(body []byte, headers http.Header) (*event, error) {

// applyEventValuesToParams returns a slice of Params with the JSONPath variables replaced
// with values from the event body and headers.
func applyEventValuesToParams(params []pipelinev1.Param, ec *event) ([]pipelinev1.Param, error) {
func applyEventValuesToParams(params []pipelinev1.Param, body []byte, header http.Header) ([]pipelinev1.Param, error) {
event, err := newEvent(body, header)
if err != nil {
return nil, fmt.Errorf("failed to marshal event: %w", err)
}

for idx, p := range params {
pValue := p.Value.StringVal
// Find all expressions wrapped in $() from the value
expressions := tektonVar.FindAllString(pValue, -1)
for _, expr := range expressions {
val, err := ParseJSONPath(ec, expr)
val, err := ParseJSONPath(event, expr)
if err != nil {
return nil, fmt.Errorf("failed to replace JSONPath value for param %s: %s: %w", p.Name, p.Value, err)
}
Expand Down
Loading

0 comments on commit 7e38ba4

Please sign in to comment.