Read a path or URL named help or h as an argument (closes #198)
check / check (push) Waiting to run
check / check (push) Waiting to run
On gen, check, freshen, fetch, list and export, a first argument of `help` or `h` ran urfave/cli's built-in help command, so `check help --require-signature FPR` printed help and passed. Those commands now set urfave/cli's HideHelpCommand, which leaves out their help command; the root keeps its own, so `mfer help` and `mfer help check` still work. `check DIR --help` failed with `No help topic for 'DIR'`; their CommandNotFound now prints the command's own help, as `check --help` does. Model: opus-5-5
This commit was merged in pull request #211.
This commit is contained in:
@@ -38,6 +38,8 @@ const (
|
|||||||
testFlagNoExtra = "--no-extra-files"
|
testFlagNoExtra = "--no-extra-files"
|
||||||
testFlagVersion = "--version"
|
testFlagVersion = "--version"
|
||||||
testFlagVerbose = "--verbose"
|
testFlagVerbose = "--verbose"
|
||||||
|
testFlagHelp = "--help"
|
||||||
|
testCmdHelp = "help"
|
||||||
)
|
)
|
||||||
|
|
||||||
var errSimulatedWrite = errors.New("simulated write failure")
|
var errSimulatedWrite = errors.New("simulated write failure")
|
||||||
@@ -354,7 +356,7 @@ func TestHelpCommand(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
opts := testOpts([]string{testApp, "--help"}, fs)
|
opts := testOpts([]string{testApp, testFlagHelp}, fs)
|
||||||
|
|
||||||
exitCode := runCLI(opts)
|
exitCode := runCLI(opts)
|
||||||
|
|
||||||
@@ -1839,6 +1841,87 @@ func TestGenerateReadsPathAfterDoubleDash(t *testing.T) {
|
|||||||
assert.Equal(t, testFileTxt, files[0].GetPath())
|
assert.Equal(t, testFileTxt, files[0].GetPath())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestPathNamedHelp runs gen, then check with --require-signature, on a
|
||||||
|
// directory named help and on one named h, the two names of urfave/cli's
|
||||||
|
// help command. gen must write the manifest into the directory, and check
|
||||||
|
// must refuse it as unsigned.
|
||||||
|
//
|
||||||
|
//nolint:paralleltest // changes the process-global working directory
|
||||||
|
func TestPathNamedHelp(t *testing.T) {
|
||||||
|
for _, name := range []string{testCmdHelp, "h"} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
chdirTemp(t)
|
||||||
|
|
||||||
|
fs := afero.NewOsFs()
|
||||||
|
require.NoError(t, fs.Mkdir(name, 0o755))
|
||||||
|
writeTestFile(t, fs, filepath.Join(name, testFileTxt), "content")
|
||||||
|
|
||||||
|
opts := testOpts([]string{testApp, cmdGenerate, "-q", name}, fs)
|
||||||
|
require.Equal(t, 0, runCLI(opts), testStderr(t, opts))
|
||||||
|
|
||||||
|
files := manifestFiles(t, fs, filepath.Join(name, defaultManifestName))
|
||||||
|
require.Len(t, files, 1)
|
||||||
|
|
||||||
|
opts = testOpts([]string{
|
||||||
|
testApp, cmdCheck, "-q", name, "--" + flagRequireSignature, msgFpA,
|
||||||
|
}, fs)
|
||||||
|
assert.Equal(t, 1, runCLI(opts))
|
||||||
|
assert.Contains(t, testStderr(t, opts), "manifest is not signed")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCommandsReadHelpAsArgument runs each command that takes a path or URL
|
||||||
|
// on help and on h where nothing by that name exists. Each must fail on the
|
||||||
|
// missing path or bad URL instead of printing its help.
|
||||||
|
func TestCommandsReadHelpAsArgument(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
for _, command := range []string{
|
||||||
|
cmdGenerate, cmdCheck, cmdFreshen, cmdList, cmdExport, cmdFetch,
|
||||||
|
} {
|
||||||
|
for _, name := range []string{testCmdHelp, "h"} {
|
||||||
|
t.Run(command+" "+name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
opts := testOpts([]string{testApp, command, name}, afero.NewMemMapFs())
|
||||||
|
assert.Equal(t, 1, runCLI(opts))
|
||||||
|
assert.NotContains(t, testStdout(t, opts), "USAGE:")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHelpBesideArgument runs each command that takes a path or URL with
|
||||||
|
// --help or -h before and after a directory, and runs help on that command.
|
||||||
|
// Each must print what the command's own --help prints.
|
||||||
|
func TestHelpBesideArgument(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
for _, command := range []string{
|
||||||
|
cmdGenerate, cmdCheck, cmdFreshen, cmdList, cmdExport, cmdFetch,
|
||||||
|
} {
|
||||||
|
want := testOpts([]string{testApp, command, testFlagHelp}, afero.NewMemMapFs())
|
||||||
|
require.Equal(t, 0, runCLI(want))
|
||||||
|
require.Contains(t, testStdout(t, want), testApp+" "+command)
|
||||||
|
|
||||||
|
for _, args := range [][]string{
|
||||||
|
{command, testDir, testFlagHelp},
|
||||||
|
{command, testFlagHelp, testDir},
|
||||||
|
{command, testDir, "-h"},
|
||||||
|
{testCmdHelp, command},
|
||||||
|
} {
|
||||||
|
t.Run(strings.Join(args, " "), func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
opts := testOpts(slices.Concat([]string{testApp}, args), afero.NewMemMapFs())
|
||||||
|
assert.Equal(t, 0, runCLI(opts), testStderr(t, opts))
|
||||||
|
assert.Equal(t, testStdout(t, want), testStdout(t, opts))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCheckDetectsManifestCorruption(t *testing.T) {
|
func TestCheckDetectsManifestCorruption(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
@@ -179,6 +179,17 @@ func refuseExtraArgs(_ context.Context, cmd *cli.Command) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// showCommandHelp is the CommandNotFound of the commands that take a path or
|
||||||
|
// URL. Those commands set HideHelpCommand, so that a path or URL named help or
|
||||||
|
// h is an argument rather than urfave/cli's help command; each sets it itself,
|
||||||
|
// because on the root it would also remove `mfer help`. With --help,
|
||||||
|
// urfave/cli looks the command's first argument up as a help topic, so
|
||||||
|
// `check DIR --help` asks for a topic DIR and finds none; this prints the
|
||||||
|
// command's own help instead, as `check --help` does.
|
||||||
|
func showCommandHelp(ctx context.Context, cmd *cli.Command, _ string) {
|
||||||
|
_ = cli.ShowCommandHelp(ctx, cmd.Root(), cmd.Name)
|
||||||
|
}
|
||||||
|
|
||||||
// requireSignatureFlag returns the --require-signature flag taken by the
|
// requireSignatureFlag returns the --require-signature flag taken by the
|
||||||
// check and fetch subcommands.
|
// check and fetch subcommands.
|
||||||
func requireSignatureFlag() *cli.StringFlag {
|
func requireSignatureFlag() *cli.StringFlag {
|
||||||
@@ -206,6 +217,8 @@ func (mfa *CLIApp) generateCommand() *cli.Command {
|
|||||||
Aliases: []string{"gen"},
|
Aliases: []string{"gen"},
|
||||||
Usage: "Generate manifest file",
|
Usage: "Generate manifest file",
|
||||||
ArgsUsage: "[path ...]",
|
ArgsUsage: "[path ...]",
|
||||||
|
HideHelpCommand: true,
|
||||||
|
CommandNotFound: showCommandHelp,
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
mfa.setVerbosity(cmd)
|
mfa.setVerbosity(cmd)
|
||||||
mfa.printBanner()
|
mfa.printBanner()
|
||||||
@@ -268,6 +281,8 @@ func (mfa *CLIApp) checkCommand() *cli.Command {
|
|||||||
Usage: "Validate files using manifest file",
|
Usage: "Validate files using manifest file",
|
||||||
ArgsUsage: manifestArgsUsage,
|
ArgsUsage: manifestArgsUsage,
|
||||||
ArgValidator: refuseExtraArgs,
|
ArgValidator: refuseExtraArgs,
|
||||||
|
HideHelpCommand: true,
|
||||||
|
CommandNotFound: showCommandHelp,
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
mfa.setVerbosity(cmd)
|
mfa.setVerbosity(cmd)
|
||||||
mfa.printBanner()
|
mfa.printBanner()
|
||||||
@@ -302,6 +317,8 @@ func (mfa *CLIApp) freshenCommand() *cli.Command {
|
|||||||
Usage: "Update manifest with changed, new, and removed files",
|
Usage: "Update manifest with changed, new, and removed files",
|
||||||
ArgsUsage: manifestArgsUsage,
|
ArgsUsage: manifestArgsUsage,
|
||||||
ArgValidator: refuseExtraArgs,
|
ArgValidator: refuseExtraArgs,
|
||||||
|
HideHelpCommand: true,
|
||||||
|
CommandNotFound: showCommandHelp,
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
mfa.setVerbosity(cmd)
|
mfa.setVerbosity(cmd)
|
||||||
mfa.printBanner()
|
mfa.printBanner()
|
||||||
@@ -353,6 +370,8 @@ func (mfa *CLIApp) exportCommand() *cli.Command {
|
|||||||
Usage: "Export manifest contents as JSON",
|
Usage: "Export manifest contents as JSON",
|
||||||
ArgsUsage: "[manifest file or URL]",
|
ArgsUsage: "[manifest file or URL]",
|
||||||
ArgValidator: refuseExtraArgs,
|
ArgValidator: refuseExtraArgs,
|
||||||
|
HideHelpCommand: true,
|
||||||
|
CommandNotFound: showCommandHelp,
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
mfa.setVerbosity(cmd)
|
mfa.setVerbosity(cmd)
|
||||||
|
|
||||||
@@ -380,6 +399,8 @@ func (mfa *CLIApp) listCommand() *cli.Command {
|
|||||||
Usage: "List files in manifest",
|
Usage: "List files in manifest",
|
||||||
ArgsUsage: manifestArgsUsage,
|
ArgsUsage: manifestArgsUsage,
|
||||||
ArgValidator: refuseExtraArgs,
|
ArgValidator: refuseExtraArgs,
|
||||||
|
HideHelpCommand: true,
|
||||||
|
CommandNotFound: showCommandHelp,
|
||||||
Action: mfa.listManifestOperation,
|
Action: mfa.listManifestOperation,
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
@@ -401,6 +422,8 @@ func (mfa *CLIApp) fetchCommand() *cli.Command {
|
|||||||
Usage: "fetch manifest and referenced files",
|
Usage: "fetch manifest and referenced files",
|
||||||
ArgsUsage: "URL",
|
ArgsUsage: "URL",
|
||||||
ArgValidator: refuseExtraArgs,
|
ArgValidator: refuseExtraArgs,
|
||||||
|
HideHelpCommand: true,
|
||||||
|
CommandNotFound: showCommandHelp,
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
mfa.setVerbosity(cmd)
|
mfa.setVerbosity(cmd)
|
||||||
mfa.printBanner()
|
mfa.printBanner()
|
||||||
|
|||||||
Reference in New Issue
Block a user