Ported per the library's v2-to-v3 migration guide: the app is a root cli.Command, actions take a context and the command, and flag environment variables become value sources. -v, -q and the version flag are marked local so that, as before, only the commands that define them accept them. ErrWriter is stdout so usage errors print with their help, as before. go mod tidy raises testify to v1.12.1, which v3 requires. The urfave_cli_no_docs build tag is dropped; v3 has no such tag. v3 no longer refuses a flag given under two of its names, so -v --verbose now gives debug output, and the test that pinned the refusal becomes a TestVerboseCount case. Help text follows v3's templates. Model: opus-5-5
This commit is contained in:
+31
-30
@@ -4,7 +4,6 @@ package cli
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"flag"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -15,7 +14,7 @@ import (
|
||||
"github.com/spf13/afero"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
urfcli "github.com/urfave/cli/v2"
|
||||
urfcli "github.com/urfave/cli/v3"
|
||||
"sneak.berlin/go/mfer/mfer"
|
||||
)
|
||||
|
||||
@@ -195,13 +194,17 @@ func signedChecker(t *testing.T, manifest []byte) *mfer.Checker {
|
||||
func TestPathDoesNotExistMessage(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
set := flag.NewFlagSet("gen", flag.ContinueOnError)
|
||||
require.NoError(t, set.Parse([]string{"nope"}))
|
||||
|
||||
mfa := &CLIApp{Fs: afero.NewMemMapFs()}
|
||||
ctx := urfcli.NewContext(nil, set, nil)
|
||||
cmd := &urfcli.Command{
|
||||
Name: "gen",
|
||||
Action: func(_ context.Context, c *urfcli.Command) error {
|
||||
_, err := mfa.collectInputPaths(c.Args())
|
||||
|
||||
_, err := mfa.collectInputPaths(ctx.Args())
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
err := cmd.Run(context.Background(), []string{"gen", "nope"})
|
||||
require.ErrorIs(t, err, errPathNotExist)
|
||||
assert.EqualError(t, err, "path does not exist: nope")
|
||||
}
|
||||
@@ -214,18 +217,21 @@ func TestOutputFileExistsMessage(t *testing.T) {
|
||||
require.NoError(t, afero.WriteFile(fs, "/d/f.txt", []byte("hi"), 0o644))
|
||||
require.NoError(t, afero.WriteFile(fs, "/out.mf", []byte("old"), 0o644))
|
||||
|
||||
set := flag.NewFlagSet("gen", flag.ContinueOnError)
|
||||
set.String("output", "", "")
|
||||
set.Bool("force", false, "")
|
||||
require.NoError(t, set.Parse([]string{"/d"}))
|
||||
require.NoError(t, set.Set("output", "/out.mf"))
|
||||
|
||||
mfa := &CLIApp{Fs: fs}
|
||||
ctx := urfcli.NewContext(nil, set, nil)
|
||||
cmd := &urfcli.Command{
|
||||
Name: "gen",
|
||||
Flags: []urfcli.Flag{
|
||||
&urfcli.StringFlag{Name: "output"},
|
||||
&urfcli.BoolFlag{Name: "force"},
|
||||
},
|
||||
Action: mfa.generateManifestOperation,
|
||||
}
|
||||
|
||||
// generateManifestOperation writes to the process-global logger during
|
||||
// enumeration, so serialize with the other CLI runs.
|
||||
err := runLocked(func() error { return mfa.generateManifestOperation(ctx) })
|
||||
err := runLocked(func() error {
|
||||
return cmd.Run(context.Background(), []string{"gen", "--output", "/out.mf", "/d"})
|
||||
})
|
||||
require.ErrorIs(t, err, errOutputExists)
|
||||
assert.EqualError(t, err,
|
||||
"output file /out.mf already exists (use --force to overwrite)")
|
||||
@@ -250,7 +256,7 @@ func TestUnknownCommandMessage(t *testing.T) {
|
||||
err := runLocked(func() error {
|
||||
mfa.run([]string{testApp})
|
||||
|
||||
return mfa.app.Run([]string{testApp, "bogus"})
|
||||
return mfa.app.Run(context.Background(), []string{testApp, "bogus"})
|
||||
})
|
||||
require.ErrorIs(t, err, errUnknownCommand)
|
||||
assert.EqualError(t, err, `unknown command "bogus"`)
|
||||
@@ -284,17 +290,13 @@ func TestFetchManifestHTTPStatusMessage(t *testing.T) {
|
||||
|
||||
mfa := &CLIApp{Fs: afero.NewMemMapFs()}
|
||||
|
||||
set := flag.NewFlagSet(cmdFetch, flag.ContinueOnError)
|
||||
for _, f := range mfa.fetchCommand().Flags {
|
||||
require.NoError(t, f.Apply(set))
|
||||
}
|
||||
|
||||
require.NoError(t, set.Parse([]string{server.URL}))
|
||||
|
||||
ctx := urfcli.NewContext(nil, set, nil)
|
||||
cmd := mfa.fetchCommand()
|
||||
cmd.Action = mfa.fetchManifestOperation
|
||||
|
||||
// fetchManifestOperation logs to the process-global logger.
|
||||
err := runLocked(func() error { return mfa.fetchManifestOperation(ctx) })
|
||||
err := runLocked(func() error {
|
||||
return cmd.Run(context.Background(), []string{cmdFetch, server.URL})
|
||||
})
|
||||
require.ErrorIs(t, err, errHTTPStatus)
|
||||
assert.EqualError(t, err, "failed to fetch manifest: HTTP 404")
|
||||
}
|
||||
@@ -320,14 +322,13 @@ func TestFetchFileHTTPStatusMessage(t *testing.T) {
|
||||
func TestURLRequiredMessage(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
set := flag.NewFlagSet("fetch", flag.ContinueOnError)
|
||||
require.NoError(t, set.Parse([]string{}))
|
||||
|
||||
mfa := &CLIApp{Fs: afero.NewMemMapFs()}
|
||||
ctx := urfcli.NewContext(nil, set, nil)
|
||||
cmd := &urfcli.Command{Name: "fetch", Action: mfa.fetchManifestOperation}
|
||||
|
||||
// fetchManifestOperation logs to the process-global logger.
|
||||
err := runLocked(func() error { return mfa.fetchManifestOperation(ctx) })
|
||||
err := runLocked(func() error {
|
||||
return cmd.Run(context.Background(), []string{"fetch"})
|
||||
})
|
||||
require.ErrorIs(t, err, errURLRequired)
|
||||
assert.EqualError(t, err, "URL argument required")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user