cuelang.org/go@v0.10.1/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  	"cuelang.org/go/cue"
    24  	"cuelang.org/go/internal/task"
    25  	"cuelang.org/go/pkg/internal"
    26  )
    27  
    28  func TestEnv(t *testing.T) {
    29  	testCases := []struct {
    30  		desc string
    31  		val  string
    32  		env  []string
    33  	}{
    34  		{
    35  			desc: "mapped",
    36  			val: `
    37  		cmd: "echo"
    38  		env: {
    39  			WHO:  "World"
    40  			WHAT: "Hello"
    41  			WHEN: "Now!"
    42  		}
    43  		`,
    44  			env: []string{"WHO=World", "WHAT=Hello", "WHEN=Now!"},
    45  		},
    46  		{
    47  			desc: "list",
    48  			val: `
    49  		cmd: "echo"
    50  		env: ["WHO=World", "WHAT=Hello", "WHEN=Now!"]
    51  		`,
    52  			env: []string{"WHO=World", "WHAT=Hello", "WHEN=Now!"},
    53  		},
    54  		{
    55  			desc: "struct handles default values",
    56  			val: `
    57  		cmd: "echo"
    58  		env: {
    59  			WHEN: *"Now!" | string
    60  			HOW: *WHEN | string
    61  		}
    62  		`,
    63  			env: []string{"WHEN=Now!", "HOW=Now!"},
    64  		},
    65  		{
    66  			desc: "list handles default values",
    67  			val: `
    68  		cmd: "echo"
    69  		env: ["WHO=World", "WHAT=Hello", *"COMMAND=\(cmd)" | string]
    70  		`,
    71  			env: []string{"WHO=World", "WHAT=Hello", "COMMAND=echo"},
    72  		},
    73  	}
    74  	for _, tc := range testCases {
    75  		t.Run(tc.desc, func(t *testing.T) {
    76  			ctx := internal.NewContext()
    77  			v := ctx.CompileString(tc.val, cue.Filename(tc.desc))
    78  			if err := v.Err(); err != nil {
    79  				t.Fatal(err)
    80  			}
    81  
    82  			cmd, _, err := mkCommand(&task.Context{
    83  				Context: context.Background(),
    84  				Obj:     v,
    85  			})
    86  			if err != nil {
    87  				t.Fatalf("mkCommand error = %v", err)
    88  			}
    89  
    90  			if diff := cmp.Diff(cmd.Env, tc.env); diff != "" {
    91  				t.Error(diff)
    92  			}
    93  		})
    94  	}
    95  }