Skip to content

Commit

Permalink
feat: Replace underscores with hyphens in project-name, fixes ddev#6206
Browse files Browse the repository at this point in the history
… (ddev#6210)

Co-authored-by: Randy Fay <[email protected]>
Co-authored-by: Stanislav Zhuk <[email protected]>
  • Loading branch information
3 people authored Jul 18, 2024
1 parent 5424179 commit 4877361
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/ddev/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func handleMainConfigArgs(cmd *cobra.Command, _ []string, app *ddevapp.DdevApp)
} else { // No siteNameArg passed, c.Name not set: use c.Name from the directory
pwd, err := os.Getwd()
util.CheckErr(err)
app.Name = filepath.Base(pwd)
app.Name = ddevapp.NormalizeProjectName(filepath.Base(pwd))
}

err = app.CheckExistingAppInApproot()
Expand Down
7 changes: 3 additions & 4 deletions pkg/ddevapp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewApp(appRoot string, includeOverrides bool) (*DdevApp, error) {
app.FailOnHookFailGlobal = globalconfig.DdevGlobalConfig.FailOnHookFailGlobal

// Provide a default app name based on directory name
app.Name = filepath.Base(app.AppRoot)
app.Name = NormalizeProjectName(filepath.Base(app.AppRoot))

// Gather containers to omit, adding ddev-router for gitpod/codespaces
app.OmitContainersGlobal = globalconfig.DdevGlobalConfig.OmitContainersGlobal
Expand Down Expand Up @@ -1283,9 +1283,8 @@ func WriteImageDockerfile(fullpath string, contents []byte) error {
func (app *DdevApp) promptForName() error {
if app.Name == "" {
dir, err := os.Getwd()
// If working directory name is invalid for hostnames, we shouldn't suggest it
if err == nil && hostRegex.MatchString(filepath.Base(dir)) {
app.Name = filepath.Base(dir)
if err == nil && hostRegex.MatchString(NormalizeProjectName(filepath.Base(dir))) {
app.Name = NormalizeProjectName(filepath.Base(dir))
}
}

Expand Down
43 changes: 42 additions & 1 deletion pkg/ddevapp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestConfigCommand(t *testing.T) {
testMatrix := map[string][]string{
"magentophpversion": {nodeps.AppTypeMagento, nodeps.PHPDefault},
"drupal7phpversion": {nodeps.AppTypeDrupal7, nodeps.PHP82},
"drupalphpversion": {nodeps.AppTypeDrupal, nodeps.PHPDefault},
"Drupalphpversion": {nodeps.AppTypeDrupal, nodeps.PHPDefault},
}

for testName, testValues := range testMatrix {
Expand Down Expand Up @@ -298,6 +298,47 @@ func TestConfigCommand(t *testing.T) {
}
}

// TestConfigCommandProjectNormalization tests behavior when normalizing project names.
// For example, a directory like "some_dir" should result in project name "some-dir"
func TestConfigCommandProjectNormalization(t *testing.T) {
assert := asrt.New(t)
origDir, _ := os.Getwd()

testMatrix := []struct {
name string
expectation string
}{
{"normalname", "normalname"},
{"with-hyphen", "with-hyphen"},
{"with_underscore", "with-underscore"},
{"with.dot", "with.dot"},
}

for _, tc := range testMatrix {
t.Run(tc.name, func(t *testing.T) {

baseTestDir := t.TempDir()
testDir := filepath.Join(baseTestDir, tc.name)
err := os.Mkdir(testDir, 0755)
require.NoError(t, err)

// Create the app we'll use for testing.
app, err := ddevapp.NewApp(testDir, false)
require.NoError(t, err)

require.Equal(t, tc.expectation, app.Name)

t.Cleanup(func() {
_ = app.Stop(true, false)
_ = os.Chdir(origDir)
})

err = ddevapp.PrepDdevDirectory(app)
assert.NoError(err)
})
}
}

// TestConfigCommandDocrootDetection asserts the default docroot is detected.
func TestConfigCommandDocrootDetection(t *testing.T) {
// Set up tests and give ourselves a working directory.
Expand Down
5 changes: 5 additions & 0 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2870,6 +2870,11 @@ func GetActiveApp(siteName string) (*DdevApp, error) {
return app, nil
}

// NormalizeProjectName replaces underscores in the site name with hyphens.
func NormalizeProjectName(siteName string) string {
return strings.ReplaceAll(siteName, "_", "-")
}

// restoreApp recreates an AppConfig's Name and returns an error
// if it cannot restore them.
func restoreApp(app *DdevApp, siteName string) error {
Expand Down
4 changes: 1 addition & 3 deletions pkg/ddevapp/ddevapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3219,9 +3219,7 @@ func TestRouterPortsCheck(t *testing.T) {
}

app, err = ddevapp.GetActiveApp(site.Name)
if err != nil {
t.Fatalf("Failed to GetActiveApp(%s), err:%v", site.Name, err)
}
require.NoError(t, err, "Failed to GetActiveApp(%s), err:%v", site.Name, err)
startErr = app.StartAndWait(5)
//nolint: errcheck
defer app.Stop(true, false)
Expand Down

0 comments on commit 4877361

Please sign in to comment.