Skip to content

Commit

Permalink
New unit tests for multiple option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Dariquest committed Jan 9, 2025
1 parent 95cb4be commit bbe4c63
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 27 deletions.
59 changes: 32 additions & 27 deletions cf/commands/route/update_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func (cmd *UpdateRoute) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["hostname"] = &flags.StringFlag{Name: "hostname", ShortName: "n", Usage: T("Hostname for the HTTP route (required for shared domains)")}
fs["path"] = &flags.StringFlag{Name: "path", Usage: T("Path for the HTTP route")}
fs["option"] = &flags.StringFlag{Name: "option", ShortName: "o", Usage: T("Set the value of a per-route option")}
fs["remove-option"] = &flags.StringFlag{Name: "remove-option", ShortName: "r", Usage: T("Remove an option with the given name")}
fs["option"] = &flags.StringSliceFlag{Name: "option", ShortName: "o", Usage: T("Set the value of a per-route option")}
fs["remove-option"] = &flags.StringSliceFlag{Name: "remove-option", ShortName: "r", Usage: T("Remove an option with the given name")}

return commandregistry.CommandMetadata{
Name: "update-route",
Expand Down Expand Up @@ -140,38 +140,43 @@ func (cmd *UpdateRoute) validateAPIVersionForPerRouteOptions() error {

func (cmd *UpdateRoute) setPerRouteOptions(c flags.FlagContext, route models.Route, url string) {
if c.IsSet("o") {
option := c.String("o")
key, value, found := strings.Cut(option, "=")
if found {
cmd.ui.Say(T("Setting route option {{.Option}} for {{.URL}} to {{.Value}}...", map[string]interface{}{
"Option": terminal.EntityNameColor(key),
"Value": terminal.EntityNameColor(value),
"URL": terminal.EntityNameColor(url)}))
if route.Options == nil {
route.Options = make(map[string]string)
options := c.StringSlice("o")
for _, option := range options {
key, value, found := strings.Cut(option, "=")
if found {
cmd.ui.Say(T("Setting route option {{.Option}} for {{.URL}} to {{.Value}}...", map[string]interface{}{
"Option": terminal.EntityNameColor(key),
"Value": terminal.EntityNameColor(value),
"URL": terminal.EntityNameColor(url)}))
if route.Options == nil {
route.Options = make(map[string]string)
}
route.Options[key] = value
} else {
cmd.ui.Say(T("Wrong route option format {{.Option}} for {{.URL}}", map[string]interface{}{
"Option": terminal.FailureColor(option),
"URL": terminal.EntityNameColor(url)}))
}
route.Options[key] = value
} else {
cmd.ui.Say(T("Wrong route option format {{.Option}} for {{.URL}}", map[string]interface{}{
"Option": terminal.FailureColor(option),
"URL": terminal.EntityNameColor(url)}))
}

}
}

func (cmd *UpdateRoute) removePerRouteOptions(c flags.FlagContext, route models.Route, url string) {
if c.IsSet("r") {
removeOption := c.String("r")
if _, ok := route.Options[removeOption]; ok {
cmd.ui.Say(T("Removing route option {{.Option}} for {{.URL}}...", map[string]interface{}{
"Option": terminal.EntityNameColor(removeOption),
"URL": terminal.EntityNameColor(url)}))

delete(route.Options, removeOption)
} else {
cmd.ui.Say(T("No route option {{.Option}} for {{.URL}}", map[string]interface{}{
"Option": terminal.EntityNameColor(removeOption),
"URL": terminal.EntityNameColor(url)}))
removeOptions := c.StringSlice("r")
for _, removeOption := range removeOptions {
if _, ok := route.Options[removeOption]; ok {
cmd.ui.Say(T("Removing route option {{.Option}} for {{.URL}}...", map[string]interface{}{
"Option": terminal.EntityNameColor(removeOption),
"URL": terminal.EntityNameColor(url)}))

delete(route.Options, removeOption)
} else {
cmd.ui.Say(T("No route option {{.Option}} for {{.URL}}", map[string]interface{}{
"Option": terminal.EntityNameColor(removeOption),
"URL": terminal.EntityNameColor(url)}))
}
}
}
}
40 changes: 40 additions & 0 deletions cf/commands/route/update_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,46 @@ var _ = Describe("UpdateRoute", func() {
})
})

Context("when the route can be found and multiple options are passed", func() {
BeforeEach(func() {
routeRepo.FindReturns(
models.Route{GUID: "route-guid",
Options: map[string]string{"a": "b", "c": "d"}},
nil,
)
err := flagContext.Parse("domain-name", "--option", "a=b", "--option", "c=d")
Expect(err).NotTo(HaveOccurred())
cmd.Requirements(factory, flagContext)
})

It("tries to set the given route options", func() {
Expect(ui.Outputs()).To(ContainSubstrings(
[]string{"Setting route option a for domain-name to b"},
[]string{"Setting route option c for domain-name to d"},
))
})
})

Context("when the route can be found and multiple remove options are passed", func() {
BeforeEach(func() {
routeRepo.FindReturns(
models.Route{GUID: "route-guid",
Options: map[string]string{"a": "b", "c": "d"}},
nil,
)
err := flagContext.Parse("domain-name", "--remove-option", "a", "--remove-option", "c")
Expect(err).NotTo(HaveOccurred())
cmd.Requirements(factory, flagContext)
})

It("tries to set the given route options", func() {
Expect(ui.Outputs()).To(ContainSubstrings(
[]string{"Removing route option a"},
[]string{"Removing route option c"},
))
})
})

Context("when the route cannot be found", func() {
BeforeEach(func() {
err := flagContext.Parse("domain-name")
Expand Down

0 comments on commit bbe4c63

Please sign in to comment.