MCPcopy
hub / github.com/spf13/cobra / TestReplaceCommandWithRemove

Function TestReplaceCommandWithRemove

command_test.go:1597–1626  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1595}
1596
1597func TestReplaceCommandWithRemove(t *testing.T) {
1598 childUsed := 0
1599 rootCmd := &Command{Use: "root", Run: emptyRun}
1600 child1Cmd := &Command{
1601 Use: "child",
1602 Run: func(*Command, []string) { childUsed = 1 },
1603 }
1604 child2Cmd := &Command{
1605 Use: "child",
1606 Run: func(*Command, []string) { childUsed = 2 },
1607 }
1608 rootCmd.AddCommand(child1Cmd)
1609 rootCmd.RemoveCommand(child1Cmd)
1610 rootCmd.AddCommand(child2Cmd)
1611
1612 output, err := executeCommand(rootCmd, "child")
1613 if output != "" {
1614 t.Errorf("Unexpected output: %v", output)
1615 }
1616 if err != nil {
1617 t.Errorf("Unexpected error: %v", err)
1618 }
1619
1620 if childUsed == 1 {
1621 t.Error("Removed command shouldn't be called")
1622 }
1623 if childUsed != 2 {
1624 t.Error("Replacing command should have been called but didn't")
1625 }
1626}
1627
1628func TestDeprecatedCommand(t *testing.T) {
1629 rootCmd := &Command{Use: "root", Run: emptyRun}

Callers

nothing calls this directly

Calls 4

AddCommandMethod · 0.95
RemoveCommandMethod · 0.95
executeCommandFunction · 0.85
ErrorMethod · 0.80

Tested by

no test coverage detected