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 is contained in:
@@ -36,6 +36,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")
|
||||||
@@ -321,7 +323,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)
|
||||||
|
|
||||||
@@ -1807,6 +1809,82 @@ 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:")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCheckHelpBesideArgument runs check with --help or -h before and after
|
||||||
|
// a directory, and runs help check. Each must print what check --help prints.
|
||||||
|
func TestCheckHelpBesideArgument(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
want := testOpts([]string{testApp, cmdCheck, testFlagHelp}, afero.NewMemMapFs())
|
||||||
|
require.Equal(t, 0, runCLI(want))
|
||||||
|
require.Contains(t, testStdout(t, want), "Validate files using manifest file")
|
||||||
|
|
||||||
|
for _, args := range [][]string{
|
||||||
|
{cmdCheck, testDir, testFlagHelp},
|
||||||
|
{cmdCheck, testFlagHelp, testDir},
|
||||||
|
{cmdCheck, testDir, "-h"},
|
||||||
|
{testCmdHelp, cmdCheck},
|
||||||
|
} {
|
||||||
|
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()
|
||||||
|
|
||||||
|
|||||||
+49
-26
@@ -168,6 +168,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 {
|
||||||
@@ -191,10 +202,12 @@ func includePermissionsFlag() *cli.BoolFlag {
|
|||||||
|
|
||||||
func (mfa *CLIApp) generateCommand() *cli.Command {
|
func (mfa *CLIApp) generateCommand() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: cmdGenerate,
|
Name: cmdGenerate,
|
||||||
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()
|
||||||
@@ -253,10 +266,12 @@ func (mfa *CLIApp) generateCommand() *cli.Command {
|
|||||||
|
|
||||||
func (mfa *CLIApp) checkCommand() *cli.Command {
|
func (mfa *CLIApp) checkCommand() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: cmdCheck,
|
Name: cmdCheck,
|
||||||
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()
|
||||||
@@ -287,10 +302,12 @@ func (mfa *CLIApp) checkCommand() *cli.Command {
|
|||||||
|
|
||||||
func (mfa *CLIApp) freshenCommand() *cli.Command {
|
func (mfa *CLIApp) freshenCommand() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: cmdFreshen,
|
Name: cmdFreshen,
|
||||||
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()
|
||||||
@@ -338,10 +355,12 @@ func (mfa *CLIApp) freshenCommand() *cli.Command {
|
|||||||
|
|
||||||
func (mfa *CLIApp) exportCommand() *cli.Command {
|
func (mfa *CLIApp) exportCommand() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: cmdExport,
|
Name: cmdExport,
|
||||||
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)
|
||||||
|
|
||||||
@@ -364,12 +383,14 @@ func (mfa *CLIApp) versionCommand() *cli.Command {
|
|||||||
|
|
||||||
func (mfa *CLIApp) listCommand() *cli.Command {
|
func (mfa *CLIApp) listCommand() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: cmdList,
|
Name: cmdList,
|
||||||
Aliases: []string{"ls"},
|
Aliases: []string{"ls"},
|
||||||
Usage: "List files in manifest",
|
Usage: "List files in manifest",
|
||||||
ArgsUsage: manifestArgsUsage,
|
ArgsUsage: manifestArgsUsage,
|
||||||
ArgValidator: refuseExtraArgs,
|
ArgValidator: refuseExtraArgs,
|
||||||
Action: mfa.listManifestOperation,
|
HideHelpCommand: true,
|
||||||
|
CommandNotFound: showCommandHelp,
|
||||||
|
Action: mfa.listManifestOperation,
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "long",
|
Name: "long",
|
||||||
@@ -386,10 +407,12 @@ func (mfa *CLIApp) listCommand() *cli.Command {
|
|||||||
|
|
||||||
func (mfa *CLIApp) fetchCommand() *cli.Command {
|
func (mfa *CLIApp) fetchCommand() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: cmdFetch,
|
Name: cmdFetch,
|
||||||
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