Skip to content

Commit 71083e5

Browse files
committed
Simplify EnvVarCombineMergeUnique
1 parent e9d8475 commit 71083e5

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

pkg/dazzle/combiner.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,16 @@ func mergeEnv(base *ociv1.Image, others []*ociv1.Image, vars []EnvVarCombination
315315
vs = append(vs, strings.Split(envValue, ":")...)
316316
vs = append(vs, strings.Split(v, ":")...)
317317

318-
vss := []string{}
319-
flags := make(map[string]bool)
320-
for i := len(vs) - 1; i >= 0; i-- {
321-
entry := vs[i]
322-
if _, value := flags[entry]; !value {
323-
flags[entry] = true
324-
vss = append(vss, entry)
318+
var (
319+
vss []string
320+
idx = make(map[string]struct{})
321+
)
322+
for _, v := range vs {
323+
if _, exists := idx[v]; exists {
324+
continue
325325
}
326-
}
327-
for i, j := 0, len(vss)-1; i < j; i, j = i+1, j-1 {
328-
vss[i], vss[j] = vss[j], vss[i]
326+
idx[v] = struct{}{}
327+
vss = append(vss, v)
329328
}
330329
envs[k] = strings.Join(vss, ":")
331330
}

pkg/dazzle/combiner_test.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package dazzle
2323
import (
2424
"testing"
2525

26+
"github.com/google/go-cmp/cmp"
2627
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
2728
)
2829

@@ -39,29 +40,29 @@ func TestMergeEnv(t *testing.T) {
3940
base: &ociv1.Image{
4041
Config: ociv1.ImageConfig{
4142
Env: []string{
42-
"PATH=first:second:third:$PATH",
43+
"PATH=first:second:third:common-value",
4344
},
4445
},
4546
},
4647
others: []*ociv1.Image{
4748
{
4849
Config: ociv1.ImageConfig{
4950
Env: []string{
50-
"PATH=fourth:fifth:$PATH",
51+
"PATH=fourth:fifth:common-value",
5152
},
5253
},
5354
},
5455
{
5556
Config: ociv1.ImageConfig{
5657
Env: []string{
57-
"PATH=sixth:sixth:$PATH",
58+
"PATH=sixth:sixth:common-value",
5859
},
5960
},
6061
},
6162
{
6263
Config: ociv1.ImageConfig{
6364
Env: []string{
64-
"PATH=eighth:seventh:eighth:$PATH",
65+
"PATH=seventh:eighth:seventh:common-value",
6566
},
6667
},
6768
},
@@ -72,36 +73,36 @@ func TestMergeEnv(t *testing.T) {
7273
Action: EnvVarCombineMergeUnique,
7374
},
7475
},
75-
expect: []string{"PATH=first:second:third:fourth:fifth:sixth:seventh:eighth:$PATH"},
76+
expect: []string{"PATH=first:second:third:common-value:fourth:fifth:sixth:seventh:eighth"},
7677
},
7778
{
7879
name: "EnvVarCombineMerge",
7980
base: &ociv1.Image{
8081
Config: ociv1.ImageConfig{
8182
Env: []string{
82-
"PATH=first:second:third:$PATH",
83+
"PATH=first:second:third:common-value",
8384
},
8485
},
8586
},
8687
others: []*ociv1.Image{
8788
{
8889
Config: ociv1.ImageConfig{
8990
Env: []string{
90-
"PATH=fourth:fifth:$PATH",
91+
"PATH=fourth:fifth:common-value",
9192
},
9293
},
9394
},
9495
{
9596
Config: ociv1.ImageConfig{
9697
Env: []string{
97-
"PATH=sixth:sixth:$PATH",
98+
"PATH=sixth:sixth:common-value",
9899
},
99100
},
100101
},
101102
{
102103
Config: ociv1.ImageConfig{
103104
Env: []string{
104-
"PATH=eighth:seventh:eighth:$PATH",
105+
"PATH=eighth:seventh:eighth:common-value",
105106
},
106107
},
107108
},
@@ -112,22 +113,22 @@ func TestMergeEnv(t *testing.T) {
112113
Action: EnvVarCombineMerge,
113114
},
114115
},
115-
expect: []string{"PATH=first:second:third:$PATH:fourth:fifth:$PATH:sixth:sixth:$PATH:eighth:seventh:eighth:$PATH"},
116+
expect: []string{"PATH=first:second:third:common-value:fourth:fifth:common-value:sixth:sixth:common-value:eighth:seventh:eighth:common-value"},
116117
},
117118
{
118119
name: "EnvVarCombineUseLast",
119120
base: &ociv1.Image{
120121
Config: ociv1.ImageConfig{
121122
Env: []string{
122-
"PATH=first:second:third:$PATH",
123+
"PATH=first:second:third:common-value",
123124
},
124125
},
125126
},
126127
others: []*ociv1.Image{
127128
{
128129
Config: ociv1.ImageConfig{
129130
Env: []string{
130-
"PATH=fourth:fifth:$PATH",
131+
"PATH=fourth:fifth:common-value",
131132
},
132133
},
133134
},
@@ -138,22 +139,22 @@ func TestMergeEnv(t *testing.T) {
138139
Action: EnvVarCombineUseLast,
139140
},
140141
},
141-
expect: []string{"PATH=fourth:fifth:$PATH"},
142+
expect: []string{"PATH=fourth:fifth:common-value"},
142143
},
143144
{
144145
name: "EnvVarCombineUseFirst",
145146
base: &ociv1.Image{
146147
Config: ociv1.ImageConfig{
147148
Env: []string{
148-
"PATH=first:second:third:$PATH",
149+
"PATH=first:second:third:common-value",
149150
},
150151
},
151152
},
152153
others: []*ociv1.Image{
153154
{
154155
Config: ociv1.ImageConfig{
155156
Env: []string{
156-
"PATH=fourth:fifth:$PATH",
157+
"PATH=fourth:fifth:common-value",
157158
},
158159
},
159160
},
@@ -164,22 +165,18 @@ func TestMergeEnv(t *testing.T) {
164165
Action: EnvVarCombineUseFirst,
165166
},
166167
},
167-
expect: []string{"PATH=first:second:third:$PATH"},
168+
expect: []string{"PATH=first:second:third:common-value"},
168169
},
169170
}
170171
for _, test := range tests {
171-
envs, err := mergeEnv(test.base, test.others, test.vars)
172-
if err != nil {
173-
t.Fatal(err)
174-
}
175-
if len(envs) != len(test.expect) {
176-
t.Fatal("unexpected length", len(envs))
177-
}
178-
for i, env := range envs {
179-
if env != test.expect[i] {
180-
t.Fatal("unexpected env", envs)
172+
t.Run(test.name, func(t *testing.T) {
173+
envs, err := mergeEnv(test.base, test.others, test.vars)
174+
if err != nil {
175+
t.Fatal(err)
181176
}
182-
183-
}
177+
if diff := cmp.Diff(envs, test.expect); len(diff) != 0 {
178+
t.Errorf("mergeEnv() mismatch (-want +got):\n%s", diff)
179+
}
180+
})
184181
}
185182
}

0 commit comments

Comments
 (0)