github.com/solo-io/cue@v0.4.7/pkg/tool/exec/exec_test.go (about)

     1  // Copyright 2020 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package exec
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  
    23  	"github.com/solo-io/cue/cue"
    24  	"github.com/solo-io/cue/internal/task"
    25  )
    26  
    27  func TestEnv(t *testing.T) {
    28  	testCases := []struct {
    29  		desc string
    30  		val  string
    31  		env  []string
    32  	}{{
    33  		desc: "mapped",
    34  		val: `
    35  		cmd: "echo"
    36  		env: {
    37  			WHO:  "World"
    38  			WHAT: "Hello"
    39  			WHEN: "Now!"
    40  		}
    41  		`,
    42  		env: []string{"WHO=World", "WHAT=Hello", "WHEN=Now!"},
    43  	}, {
    44  		val: `
    45  		cmd: "echo"
    46  		env: [ "WHO=World", "WHAT=Hello", "WHEN=Now!" ]
    47  		`,
    48  		env: []string{"WHO=World", "WHAT=Hello", "WHEN=Now!"},
    49  	}}
    50  	for _, tc := range testCases {
    51  		t.Run("", func(t *testing.T) {
    52  			var r cue.Runtime
    53  			inst, err := r.Compile(tc.desc, tc.val)
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  
    58  			cmd, _, err := mkCommand(&task.Context{
    59  				Context: context.Background(),
    60  				Obj:     inst.Value(),
    61  			})
    62  			if err != nil {
    63  				t.Fatal(err)
    64  			}
    65  
    66  			if !cmp.Equal(cmd.Env, tc.env) {
    67  				t.Error(cmp.Diff(cmd.Env, tc.env))
    68  			}
    69  		})
    70  	}
    71  }