github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/stack/loader/loader_test.go (about)

     1  package loader
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  	"gotest.tools/v3/fs"
    13  )
    14  
    15  func TestGetConfigDetails(t *testing.T) {
    16  	content := `
    17  version: "3.0"
    18  services:
    19    foo:
    20      image: alpine:3.5
    21  `
    22  	file := fs.NewFile(t, "test-get-config-details", fs.WithContent(content))
    23  	defer file.Remove()
    24  
    25  	details, err := GetConfigDetails([]string{file.Path()}, nil)
    26  	assert.NilError(t, err)
    27  	assert.Check(t, is.Equal(filepath.Dir(file.Path()), details.WorkingDir))
    28  	assert.Assert(t, is.Len(details.ConfigFiles, 1))
    29  	assert.Check(t, is.Equal("3.0", details.ConfigFiles[0].Config["version"]))
    30  	assert.Check(t, is.Len(details.Environment, len(os.Environ())))
    31  }
    32  
    33  func TestGetConfigDetailsStdin(t *testing.T) {
    34  	content := `
    35  version: "3.0"
    36  services:
    37    foo:
    38      image: alpine:3.5
    39  `
    40  	details, err := GetConfigDetails([]string{"-"}, strings.NewReader(content))
    41  	assert.NilError(t, err)
    42  	cwd, err := os.Getwd()
    43  	assert.NilError(t, err)
    44  	assert.Check(t, is.Equal(cwd, details.WorkingDir))
    45  	assert.Assert(t, is.Len(details.ConfigFiles, 1))
    46  	assert.Check(t, is.Equal("3.0", details.ConfigFiles[0].Config["version"]))
    47  	assert.Check(t, is.Len(details.Environment, len(os.Environ())))
    48  }
    49  
    50  func TestBuildEnvironment(t *testing.T) {
    51  	inputEnv := []string{
    52  		"LEGIT_VAR=LEGIT_VALUE",
    53  		"EMPTY_VARIABLE=",
    54  	}
    55  
    56  	if runtime.GOOS == "windows" {
    57  		inputEnv = []string{
    58  			"LEGIT_VAR=LEGIT_VALUE",
    59  
    60  			// cmd.exe has some special environment variables which start with "=".
    61  			// These should be ignored as they're only there for MS-DOS compatibility.
    62  			"=ExitCode=00000041",
    63  			"=ExitCodeAscii=A",
    64  			`=C:=C:\some\dir`,
    65  			`=D:=D:\some\different\dir`,
    66  			`=X:=X:\`,
    67  			`=::=::\`,
    68  
    69  			"EMPTY_VARIABLE=",
    70  		}
    71  	}
    72  
    73  	env, err := buildEnvironment(inputEnv)
    74  	assert.NilError(t, err)
    75  
    76  	assert.Check(t, is.Len(env, 2))
    77  	assert.Check(t, is.Equal("LEGIT_VALUE", env["LEGIT_VAR"]))
    78  	assert.Check(t, is.Equal("", env["EMPTY_VARIABLE"]))
    79  }