This is an automated email from the ASF dual-hosted git repository.

jrmccluskey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new bae6fcfcb49 Add a unit test for MakePipelineOptionsFileAndEnvVar 
(#31732)
bae6fcfcb49 is described below

commit bae6fcfcb492db64e8b7333d928fdbecdcb3e21f
Author: ljjulia <[email protected]>
AuthorDate: Mon Jul 1 06:20:33 2024 -0700

    Add a unit test for MakePipelineOptionsFileAndEnvVar (#31732)
    
    * Add unit test for MakePipelineOptionsFileAndEnvVar
    
    * Add coverage for validating options string is JSON
---
 sdks/go/container/tools/pipeline_options.go        |  5 +++
 ...ipeline_options.go => pipeline_options_test.go} | 49 +++++++++++++++-------
 2 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/sdks/go/container/tools/pipeline_options.go 
b/sdks/go/container/tools/pipeline_options.go
index 7b46d8fa8c8..026fb31b099 100644
--- a/sdks/go/container/tools/pipeline_options.go
+++ b/sdks/go/container/tools/pipeline_options.go
@@ -16,6 +16,7 @@
 package tools
 
 import (
+       "encoding/json"
        "fmt"
        "os"
 )
@@ -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)
        }
diff --git a/sdks/go/container/tools/pipeline_options.go 
b/sdks/go/container/tools/pipeline_options_test.go
similarity index 54%
copy from sdks/go/container/tools/pipeline_options.go
copy to sdks/go/container/tools/pipeline_options_test.go
index 7b46d8fa8c8..7a0d7ebd5f0 100644
--- a/sdks/go/container/tools/pipeline_options.go
+++ b/sdks/go/container/tools/pipeline_options_test.go
@@ -16,24 +16,43 @@
 package tools
 
 import (
-       "fmt"
        "os"
+       "testing"
 )
 
-// MakePipelineOptionsFileAndEnvVar writes the pipeline options to a file.
-// Assumes the options string is JSON formatted.
-//
-// Stores the file name in question in PIPELINE_OPTIONS_FILE for access by the 
SDK.
-func MakePipelineOptionsFileAndEnvVar(options string) error {
-       fn := "pipeline_options.json"
-       f, err := os.Create(fn)
-       if err != nil {
-               return fmt.Errorf("unable to create %v: %w", fn, err)
+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}",
+               },
        }
-       defer f.Close()
-       if _, err := f.WriteString(options); err != nil {
-               return fmt.Errorf("error writing %v: %w", f.Name(), err)
+
+       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.Setenv("PIPELINE_OPTIONS_FILE", f.Name())
-       return nil
+       os.Remove("pipeline_options.json")
 }

Reply via email to