Skip to content

Commit

Permalink
Add TestGetProjectsMissingApp
Browse files Browse the repository at this point in the history
  • Loading branch information
rfay committed Mar 28, 2024
1 parent 3113e8a commit d4e8fe9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
32 changes: 30 additions & 2 deletions pkg/ddevapp/ddevapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4033,16 +4033,44 @@ func TestPortSpecifications(t *testing.T) {
require.NotEmpty(t, globalconfig.DdevProjectList[conflictApp.Name].UsedHostPorts)
}

// TestDdevGetProjects exercises GetProjects()
// TestGetProjects exercises GetProjects()
// It's only here for profiling at this point
func TestDdevGetProjects(t *testing.T) {
func TestGetProjects(t *testing.T) {
assert := asrt.New(t)
defer util.TimeTrackC(t.Name())()

apps, err := ddevapp.GetProjects(false)
assert.NoError(err)
_ = apps
}

// TestGetProjectsMissingApp exercises GetProjects() with a missing project from filesystem
func TestGetProjectsMissingApp(t *testing.T) {
tmpDir := testcommon.CreateTmpDir(t.Name())
badApp, err := ddevapp.NewApp(tmpDir, false)
require.NoError(t, err)

t.Cleanup(func() {
_ = badApp.Stop(true, false)
_ = os.RemoveAll(tmpDir)
})

err = badApp.Start()
require.NoError(t, err)
// Make sure the new badApp is listed
l, err := ddevapp.GetProjects(false)
require.NoError(t, err)
m := ddevapp.AppSliceToMap(l)
require.NotEmptyf(t, m[badApp.Name], "app not found when it should be")
// Remove the badApp from the filesystem and verify
// that it is no longer listed
err = badApp.Stop(false, false)
require.NoError(t, err)
_ = os.RemoveAll(badApp.AppRoot)
l, err = ddevapp.GetProjects(false)
require.NoError(t, err)
m = ddevapp.AppSliceToMap(l)
require.Emptyf(t, m[badApp.Name], "map should no longer have badapp")
}

// TestCustomCerts makes sure that added custom certificates are respected and used
Expand Down
9 changes: 9 additions & 0 deletions pkg/ddevapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,12 @@ func (app *DdevApp) CanUseHTTPOnly() bool {
// Default case is OK to use https
return false
}

// Turn a slice of *DdevApp into a map keyed by name
func AppSliceToMap(appList []*DdevApp) map[string]*DdevApp {
nameMap := make(map[string]*DdevApp)
for _, app := range appList {
nameMap[app.Name] = app
}
return nameMap
}

0 comments on commit d4e8fe9

Please sign in to comment.