Skip to content

Commit

Permalink
fix: using multiple features and opts with spaces (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
kconley-sq authored Nov 2, 2023
1 parent e5e1a8f commit 4a2641e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
6 changes: 3 additions & 3 deletions devcontainer/devcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ func TestCompileWithFeatures(t *testing.T) {

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

require.Equal(t, `FROM codercom/code-server:latest
Expand All @@ -105,7 +105,7 @@ RUN ./install.sh
# Go potato - Example description!
WORKDIR `+featureTwoDir+`
ENV POTATO=example
RUN VERSION=potato ./install.sh
RUN VERSION="potato" ./install.sh
USER 1000`, params.DockerfileContent)
}

Expand Down
2 changes: 1 addition & 1 deletion devcontainer/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (s *Spec) Compile(options map[string]any) (string, error) {
// delete so we can check if there are any unknown options
delete(options, key)
}
runDirective = append(runDirective, fmt.Sprintf("%s=%s", convertOptionNameToEnv(key), strValue))
runDirective = append(runDirective, fmt.Sprintf(`%s="%s"`, convertOptionNameToEnv(key), strValue))
}
if len(options) > 0 {
return "", fmt.Errorf("unknown option: %v", options)
Expand Down
2 changes: 1 addition & 1 deletion devcontainer/features/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ func TestCompile(t *testing.T) {
}
directive, err := spec.Compile(nil)
require.NoError(t, err)
require.Equal(t, "WORKDIR /\nRUN FOO=bar ./install.sh", strings.TrimSpace(directive))
require.Equal(t, "WORKDIR /\nRUN FOO=\"bar\" ./install.sh", strings.TrimSpace(directive))
})
}
4 changes: 2 additions & 2 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const (
// MagicDir is where all envbuilder related files are stored.
// This is a special directory that must not be modified
// by the user or images.
MagicDir = ".envbuilder"
MagicDir = "/.envbuilder"
)

var (
Expand Down Expand Up @@ -304,7 +304,7 @@ func Run(ctx context.Context, options Options) error {
if err != nil {
return fmt.Errorf("parse docker config: %w", err)
}
err = os.WriteFile(filepath.Join("/", MagicDir, "config.json"), decoded, 0644)
err = os.WriteFile(filepath.Join(MagicDir, "config.json"), decoded, 0644)
if err != nil {
return fmt.Errorf("write docker config: %w", err)
}
Expand Down
36 changes: 28 additions & 8 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,32 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
t.Parallel()

registry := registrytest.New(t)
ref := registrytest.WriteContainer(t, registry, "coder/test:latest", features.TarLayerMediaType, map[string]any{
feature1Ref := registrytest.WriteContainer(t, registry, "coder/test1:latest", features.TarLayerMediaType, map[string]any{
"devcontainer-feature.json": &features.Spec{
ID: "test",
Name: "test",
ID: "test1",
Name: "test1",
Version: "1.0.0",
Options: map[string]features.Option{
"bananas": {
Type: "string",
},
},
},
"install.sh": "echo $BANANAS > /test",
"install.sh": "echo $BANANAS > /test1output",
})

feature2Ref := registrytest.WriteContainer(t, registry, "coder/test2:latest", features.TarLayerMediaType, map[string]any{
"devcontainer-feature.json": &features.Spec{
ID: "test2",
Name: "test2",
Version: "1.0.0",
Options: map[string]features.Option{
"pineapple": {
Type: "string",
},
},
},
"install.sh": "echo $PINEAPPLE > /test2output",
})

// Ensures that a Git repository with a devcontainer.json is cloned and built.
Expand All @@ -105,8 +119,11 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
"dockerfile": "Dockerfile"
},
"features": {
"` + ref + `": {
"bananas": "hello"
"` + feature1Ref + `": {
"bananas": "hello from test 1!"
},
"` + feature2Ref + `": {
"pineapple": "hello from test 2!"
}
}
}`,
Expand All @@ -118,8 +135,11 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
}})
require.NoError(t, err)

output := execContainer(t, ctr, "cat /test")
require.Equal(t, "hello", strings.TrimSpace(output))
test1Output := execContainer(t, ctr, "cat /test1output")
require.Equal(t, "hello from test 1!", strings.TrimSpace(test1Output))

test2Output := execContainer(t, ctr, "cat /test2output")
require.Equal(t, "hello from test 2!", strings.TrimSpace(test2Output))
}

func TestBuildFromDockerfile(t *testing.T) {
Expand Down

0 comments on commit 4a2641e

Please sign in to comment.