fix: copy-on-wrap commands and typed errors
Clone commands before wrapping Before hooks, export ErrLifecycleNotInContext and ErrNoDependencies, add Run helper, split command.go and deps.go. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package appcli_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitea.auvem.com/go-toolkit/app"
|
||||
"gitea.auvem.com/go-toolkit/appcli"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func TestNewCommandRequiresLifecycle(t *testing.T) {
|
||||
cmd := appcli.NewCommand(&cli.Command{Name: "test"}, appcli.DepList())
|
||||
err := cmd.Run(context.Background(), []string{"test"})
|
||||
assert.ErrorIs(t, err, appcli.ErrLifecycleNotInContext)
|
||||
}
|
||||
|
||||
func TestNewCommandLoadsDeps(t *testing.T) {
|
||||
var loaded bool
|
||||
mod := app.NewModule("dep", app.ModuleOpts{
|
||||
Setup: func(m *app.Module) error { loaded = true; return nil },
|
||||
})
|
||||
|
||||
cmd := appcli.NewCommand(&cli.Command{
|
||||
Name: "test",
|
||||
Action: func(ctx context.Context, c *cli.Command) error {
|
||||
return nil
|
||||
},
|
||||
}, appcli.DepList(mod))
|
||||
|
||||
lc := app.NewLifecycle()
|
||||
err := cmd.Run(lc.Context(context.Background()), []string{"test"})
|
||||
require.NoError(t, err)
|
||||
assert.True(t, loaded)
|
||||
}
|
||||
|
||||
func TestNewCommandDoesNotMutateInput(t *testing.T) {
|
||||
original := &cli.Command{Name: "orig"}
|
||||
wrapped := appcli.NewCommand(original, appcli.DepList())
|
||||
assert.NotSame(t, original, wrapped)
|
||||
assert.Nil(t, original.Before)
|
||||
}
|
||||
|
||||
func TestDepListEmpty(t *testing.T) {
|
||||
fn := appcli.DepList()
|
||||
_, err := fn(nil, nil)
|
||||
assert.ErrorIs(t, err, appcli.ErrNoDependencies)
|
||||
}
|
||||
|
||||
func TestVerboseFromCommand(t *testing.T) {
|
||||
root := appcli.NewRootCommand(&cli.Command{Name: "root"})
|
||||
assert.False(t, appcli.VerboseFromCommand(root))
|
||||
}
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
lc := app.NewLifecycle()
|
||||
cmd := &cli.Command{
|
||||
Name: "ok",
|
||||
Action: func(ctx context.Context, c *cli.Command) error {
|
||||
assert.NotNil(t, app.LifecycleFromContext(ctx))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
require.NoError(t, appcli.Run(context.Background(), lc, cmd, []string{"ok"}))
|
||||
}
|
||||
Reference in New Issue
Block a user