Skip to content

Commit

Permalink
Merge pull request #189 from compose-spec/revert-number-restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof authored Sep 23, 2021
2 parents a7e1bc3 + 2477aa1 commit 4b512fd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ func ProjectFromOptions(options *ProjectOptions) (*types.Project, error) {
} else if nameFromEnv, ok := options.Environment[ComposeProjectName]; ok && nameFromEnv != "" {
opts.Name = nameFromEnv
} else {
opts.Name = regexp.MustCompile(`(?m)[a-z]+[-_a-z0-9]*`).FindString(strings.ToLower(filepath.Base(absWorkingDir)))
opts.Name = filepath.Base(absWorkingDir)
}
opts.Name = strings.ToLower(opts.Name)
opts.Name = regexp.MustCompile(`(?m)[a-z0-9]+[-_a-z0-9]*`).FindString(strings.ToLower(opts.Name))
}
options.loadOptions = append(options.loadOptions, nameLoadOpt)

Expand Down
49 changes: 49 additions & 0 deletions cli/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cli

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -33,6 +34,54 @@ func TestProjectName(t *testing.T) {
assert.Equal(t, p.Name, "my_project")
})

t.Run("by name start with number", func(t *testing.T) {
opts, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithName("42my_project_num"))
assert.NilError(t, err)
p, err := ProjectFromOptions(opts)
assert.NilError(t, err)
assert.Equal(t, p.Name, "42my_project_num")

opts, err = NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithEnv([]string{
fmt.Sprintf("%s=%s", ComposeProjectName, "42my_project_env"),
}))
assert.NilError(t, err)
p, err = ProjectFromOptions(opts)
assert.NilError(t, err)
assert.Equal(t, p.Name, "42my_project_env")
})

t.Run("by name start with invalid char '-'", func(t *testing.T) {
opts, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithName("-my_project"))
assert.NilError(t, err)
p, err := ProjectFromOptions(opts)
assert.NilError(t, err)
assert.Equal(t, p.Name, "my_project")

opts, err = NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithEnv([]string{
fmt.Sprintf("%s=%s", ComposeProjectName, "-my_project"),
}))
assert.NilError(t, err)
p, err = ProjectFromOptions(opts)
assert.NilError(t, err)
assert.Equal(t, p.Name, "my_project")
})

t.Run("by name start with invalid char '_'", func(t *testing.T) {
opts, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithName("_my_project"))
assert.NilError(t, err)
p, err := ProjectFromOptions(opts)
assert.NilError(t, err)
assert.Equal(t, p.Name, "my_project")

opts, err = NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithEnv([]string{
fmt.Sprintf("%s=%s", ComposeProjectName, "_my_project"),
}))
assert.NilError(t, err)
p, err = ProjectFromOptions(opts)
assert.NilError(t, err)
assert.Equal(t, p.Name, "my_project")
})

t.Run("by working dir", func(t *testing.T) {
opts, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithWorkingDirectory("."))
assert.NilError(t, err)
Expand Down

0 comments on commit 4b512fd

Please sign in to comment.