Skip to content

Commit

Permalink
Add a unit test for MakePipelineOptionsFileAndEnvVar (#31732)
Browse files Browse the repository at this point in the history
* Add unit test for MakePipelineOptionsFileAndEnvVar

* Add coverage for validating options string is JSON
  • Loading branch information
mls3odp authored Jul 1, 2024
1 parent ef50604 commit bae6fcf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sdks/go/container/tools/pipeline_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package tools

import (
"encoding/json"
"fmt"
"os"
)
Expand All @@ -31,6 +32,10 @@ func MakePipelineOptionsFileAndEnvVar(options string) error {
return fmt.Errorf("unable to create %v: %w", fn, err)
}
defer f.Close()
var js map[string]interface{}
if json.Unmarshal([]byte(options), &js) != nil {
return fmt.Errorf("options string is not JSON formatted %v", options)
}
if _, err := f.WriteString(options); err != nil {
return fmt.Errorf("error writing %v: %w", f.Name(), err)
}
Expand Down
58 changes: 58 additions & 0 deletions sdks/go/container/tools/pipeline_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tools

import (
"os"
"testing"
)

func TestMakePipelineOptionsFileAndEnvVar(t *testing.T) {
tests := []struct {
name string
inputOptions string
expectedError string
}{
{
"empty options",
"{}",
"",
},
{
"valid options",
"{\"abc\": 123}",
"",
},
{
"invalid options",
"{4}",
"options string is not JSON formatted {4}",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Cleanup(os.Clearenv)
err := MakePipelineOptionsFileAndEnvVar(test.inputOptions)
if err != nil {
if got, want := err.Error(), test.expectedError; got != want {
t.Errorf("got error: %v, want error: %v", got, want)
}
}
})
}
os.Remove("pipeline_options.json")
}

0 comments on commit bae6fcf

Please sign in to comment.