...

Source file src/cuelang.org/go/pkg/tool/exec/exec_test.go

Documentation: cuelang.org/go/pkg/tool/exec

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

View as plain text