Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #520 project name not available for lookup #549

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2969,3 +2969,73 @@ services:
})
assert.NilError(t, err)
}

func TestLoadProjectName(t *testing.T) {
const projectName = "testproject"

tests := []struct {
name string
env map[string]string
options func(*Options)
wantErr string
}{
{
name: "default",
options: func(o *Options) {},
wantErr: "project name must not be empty",
},
{
name: "project name from environment",
env: map[string]string{"COMPOSE_PROJECT_NAME": projectName},
options: func(o *Options) {},
wantErr: "project name must not be empty",
},
{
name: "project name from options, not imperatively set; no env",
options: withProjectName(projectName, false),
},
{
name: "project name from options, imperatively set; no env",
options: withProjectName(projectName, true),
},
{
name: "project name from options, not imperatively set; empty env",
env: map[string]string{},
options: withProjectName(projectName, false),
},
{
name: "project name from options, imperatively set; empty env",
env: map[string]string{},
options: withProjectName(projectName, true),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
project, err := LoadWithContext(context.Background(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{{
Content: []byte(`
services:
web:
image: web
container_name: ${COMPOSE_PROJECT_NAME}-web`),
}},
Environment: tt.env,
}, tt.options)
if tt.wantErr != "" {
assert.ErrorContains(t, err, tt.wantErr)
return
}
assert.NilError(t, err)
assert.Equal(t, project.Name, projectName)
assert.Equal(t, project.Environment["COMPOSE_PROJECT_NAME"], projectName)
assert.Equal(t, project.Services["web"].ContainerName, projectName+"-web")
})
}
}

func withProjectName(projectName string, imperativelySet bool) func(*Options) {
return func(opts *Options) {
opts.SetProjectName(projectName, imperativelySet)
}
}
2 changes: 1 addition & 1 deletion types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ConfigDetails struct {
}

// LookupEnv provides a lookup function for environment variables
func (cd ConfigDetails) LookupEnv(key string) (string, bool) {
func (cd *ConfigDetails) LookupEnv(key string) (string, bool) {
v, ok := cd.Environment[key]
if !isCaseInsensitiveEnvVars || ok {
return v, ok
Expand Down
Loading