Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4a2641e

Browse files
authored
fix: using multiple features and opts with spaces (#65)
1 parent e5e1a8f commit 4a2641e

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

devcontainer/devcontainer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ func TestCompileWithFeatures(t *testing.T) {
9191

9292
// We have to SHA because we get a different MD5 every time!
9393
featureOneMD5 := md5.Sum([]byte(featureOne))
94-
featureOneDir := fmt.Sprintf(".envbuilder/features/one-%x", featureOneMD5[:4])
94+
featureOneDir := fmt.Sprintf("/.envbuilder/features/one-%x", featureOneMD5[:4])
9595
featureTwoMD5 := md5.Sum([]byte(featureTwo))
96-
featureTwoDir := fmt.Sprintf(".envbuilder/features/two-%x", featureTwoMD5[:4])
96+
featureTwoDir := fmt.Sprintf("/.envbuilder/features/two-%x", featureTwoMD5[:4])
9797

9898
require.Equal(t, `FROM codercom/code-server:latest
9999
@@ -105,7 +105,7 @@ RUN ./install.sh
105105
# Go potato - Example description!
106106
WORKDIR `+featureTwoDir+`
107107
ENV POTATO=example
108-
RUN VERSION=potato ./install.sh
108+
RUN VERSION="potato" ./install.sh
109109
USER 1000`, params.DockerfileContent)
110110
}
111111

devcontainer/features/features.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (s *Spec) Compile(options map[string]any) (string, error) {
179179
// delete so we can check if there are any unknown options
180180
delete(options, key)
181181
}
182-
runDirective = append(runDirective, fmt.Sprintf("%s=%s", convertOptionNameToEnv(key), strValue))
182+
runDirective = append(runDirective, fmt.Sprintf(`%s="%s"`, convertOptionNameToEnv(key), strValue))
183183
}
184184
if len(options) > 0 {
185185
return "", fmt.Errorf("unknown option: %v", options)

devcontainer/features/features_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@ func TestCompile(t *testing.T) {
111111
}
112112
directive, err := spec.Compile(nil)
113113
require.NoError(t, err)
114-
require.Equal(t, "WORKDIR /\nRUN FOO=bar ./install.sh", strings.TrimSpace(directive))
114+
require.Equal(t, "WORKDIR /\nRUN FOO=\"bar\" ./install.sh", strings.TrimSpace(directive))
115115
})
116116
}

envbuilder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const (
6161
// MagicDir is where all envbuilder related files are stored.
6262
// This is a special directory that must not be modified
6363
// by the user or images.
64-
MagicDir = ".envbuilder"
64+
MagicDir = "/.envbuilder"
6565
)
6666

6767
var (
@@ -304,7 +304,7 @@ func Run(ctx context.Context, options Options) error {
304304
if err != nil {
305305
return fmt.Errorf("parse docker config: %w", err)
306306
}
307-
err = os.WriteFile(filepath.Join("/", MagicDir, "config.json"), decoded, 0644)
307+
err = os.WriteFile(filepath.Join(MagicDir, "config.json"), decoded, 0644)
308308
if err != nil {
309309
return fmt.Errorf("write docker config: %w", err)
310310
}

integration/integration_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,32 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
8282
t.Parallel()
8383

8484
registry := registrytest.New(t)
85-
ref := registrytest.WriteContainer(t, registry, "coder/test:latest", features.TarLayerMediaType, map[string]any{
85+
feature1Ref := registrytest.WriteContainer(t, registry, "coder/test1:latest", features.TarLayerMediaType, map[string]any{
8686
"devcontainer-feature.json": &features.Spec{
87-
ID: "test",
88-
Name: "test",
87+
ID: "test1",
88+
Name: "test1",
8989
Version: "1.0.0",
9090
Options: map[string]features.Option{
9191
"bananas": {
9292
Type: "string",
9393
},
9494
},
9595
},
96-
"install.sh": "echo $BANANAS > /test",
96+
"install.sh": "echo $BANANAS > /test1output",
97+
})
98+
99+
feature2Ref := registrytest.WriteContainer(t, registry, "coder/test2:latest", features.TarLayerMediaType, map[string]any{
100+
"devcontainer-feature.json": &features.Spec{
101+
ID: "test2",
102+
Name: "test2",
103+
Version: "1.0.0",
104+
Options: map[string]features.Option{
105+
"pineapple": {
106+
Type: "string",
107+
},
108+
},
109+
},
110+
"install.sh": "echo $PINEAPPLE > /test2output",
97111
})
98112

99113
// Ensures that a Git repository with a devcontainer.json is cloned and built.
@@ -105,8 +119,11 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
105119
"dockerfile": "Dockerfile"
106120
},
107121
"features": {
108-
"` + ref + `": {
109-
"bananas": "hello"
122+
"` + feature1Ref + `": {
123+
"bananas": "hello from test 1!"
124+
},
125+
"` + feature2Ref + `": {
126+
"pineapple": "hello from test 2!"
110127
}
111128
}
112129
}`,
@@ -118,8 +135,11 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
118135
}})
119136
require.NoError(t, err)
120137

121-
output := execContainer(t, ctr, "cat /test")
122-
require.Equal(t, "hello", strings.TrimSpace(output))
138+
test1Output := execContainer(t, ctr, "cat /test1output")
139+
require.Equal(t, "hello from test 1!", strings.TrimSpace(test1Output))
140+
141+
test2Output := execContainer(t, ctr, "cat /test2output")
142+
require.Equal(t, "hello from test 2!", strings.TrimSpace(test2Output))
123143
}
124144

125145
func TestBuildFromDockerfile(t *testing.T) {

0 commit comments

Comments
 (0)