MCPcopy
hub / github.com/urfave/cli / TestCommand_AfterFunc

Function TestCommand_AfterFunc

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

Source from the content-addressed store, hash-verified

1744}
1745
1746func TestCommand_AfterFunc(t *testing.T) {
1747 counts := &opCounts{}
1748 afterError := fmt.Errorf("fail")
1749 var err error
1750
1751 cmd := &Command{
1752 After: func(_ context.Context, cmd *Command) error {
1753 counts.Total++
1754 counts.After = counts.Total
1755 s := cmd.String("opt")
1756 if s == "fail" {
1757 return afterError
1758 }
1759
1760 return nil
1761 },
1762 Commands: []*Command{
1763 {
1764 Name: "sub",
1765 Action: func(context.Context, *Command) error {
1766 counts.Total++
1767 counts.SubCommand = counts.Total
1768 return nil
1769 },
1770 },
1771 },
1772 Flags: []Flag{
1773 &StringFlag{Name: "opt"},
1774 },
1775 }
1776
1777 // run with the After() func succeeding
1778 err = cmd.Run(buildTestContext(t), []string{"command", "--opt", "succeed", "sub"})
1779 require.NoError(t, err)
1780 assert.Equal(t, 2, counts.After, "After() not executed when expected")
1781 assert.Equal(t, 1, counts.SubCommand, "Subcommand not executed when expected")
1782
1783 // reset
1784 counts = &opCounts{}
1785
1786 // run with the Before() func failing
1787 err = cmd.Run(buildTestContext(t), []string{"command", "--opt", "fail", "sub"})
1788
1789 // should be the same error produced by the Before func
1790 assert.ErrorIs(t, err, afterError, "Run error expected, but not received")
1791 assert.Equal(t, 2, counts.After, "After() not executed when expected")
1792 assert.Equal(t, 1, counts.SubCommand, "Subcommand not executed when expected")
1793
1794 /*
1795 reset
1796 */
1797 counts = &opCounts{}
1798 // reset the flags since they are set previously
1799 cmd.Flags = []Flag{
1800 &StringFlag{Name: "opt"},
1801 }
1802
1803 // run with none args

Callers

nothing calls this directly

Calls 3

StringMethod · 0.95
RunMethod · 0.95
buildTestContextFunction · 0.85

Tested by

no test coverage detected