File: parse_test.go

package info (click to toggle)
docker.io 20.10.24%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 60,824 kB
  • sloc: sh: 5,621; makefile: 593; ansic: 179; python: 162; asm: 7
file content (72 lines) | stat: -rw-r--r-- 1,823 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package opts

import (
	"testing"

	"gotest.tools/v3/assert"
	"gotest.tools/v3/env"
	"gotest.tools/v3/fs"
)

func TestReadKVEnvStrings(t *testing.T) {
	emptyEnvFile := fs.NewFile(t, t.Name())
	defer emptyEnvFile.Remove()

	// EMPTY_VAR should be set, but with an empty string as value
	// FROM_ENV value should be substituted with FROM_ENV in the current environment
	// NO_SUCH_ENV does not exist in the current environment, so should be omitted
	envFile1 := fs.NewFile(t, t.Name(), fs.WithContent(`Z1=z
EMPTY_VAR=
FROM_ENV
NO_SUCH_ENV
`))
	defer envFile1.Remove()
	envFile2 := fs.NewFile(t, t.Name(), fs.WithContent("Z2=z\nA2=a"))
	defer envFile2.Remove()
	defer env.Patch(t, "FROM_ENV", "from-env")()

	tests := []struct {
		name      string
		files     []string
		overrides []string
		expected  []string
	}{
		{
			name: "empty",
		},
		{
			name:  "empty file",
			files: []string{emptyEnvFile.Path()},
		},
		{
			name:     "single file",
			files:    []string{envFile1.Path()},
			expected: []string{"Z1=z", "EMPTY_VAR=", "FROM_ENV=from-env"},
		},
		{
			name:     "two files",
			files:    []string{envFile1.Path(), envFile2.Path()},
			expected: []string{"Z1=z", "EMPTY_VAR=", "FROM_ENV=from-env", "Z2=z", "A2=a"},
		},
		{
			name:      "single file and override",
			files:     []string{envFile1.Path()},
			overrides: []string{"Z1=override", "EXTRA=extra"},
			expected:  []string{"Z1=z", "EMPTY_VAR=", "FROM_ENV=from-env", "Z1=override", "EXTRA=extra"},
		},
		{
			name:      "overrides only",
			overrides: []string{"Z1=z", "EMPTY_VAR="},
			expected:  []string{"Z1=z", "EMPTY_VAR="},
		},
	}

	for _, tc := range tests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			envs, err := ReadKVEnvStrings(tc.files, tc.overrides)
			assert.NilError(t, err)
			assert.DeepEqual(t, tc.expected, envs)
		})
	}
}