Tests: add tests to support new cli migrations transfer

This commit is contained in:
Keith Martin
2025-05-10 23:20:54 +10:00
parent a264280d5d
commit 1980a5093a
2 changed files with 336 additions and 0 deletions

View File

@@ -105,3 +105,57 @@ func RunWithTestContext(cmd *cli.Command, args []string) (output string, err err
return output, err
}
// NewTestContextWithParse creates a new CLI test context with the flags and arguments provided.
func NewTestContextWithParse(appArgs []string, cmdArgs []string) *cli.Context {
// Create new command-line test app.
app := cli.NewApp()
app.Name = "photoprism"
app.Usage = "PhotoPrism®"
app.Description = ""
app.Version = "test"
app.Copyright = "(c) 2018-2025 PhotoPrism UG. All rights reserved."
app.Flags = config.Flags.Cli()
app.Commands = PhotoPrism
app.HelpName = app.Name
app.CustomAppHelpTemplate = ""
app.HideHelp = true
app.HideHelpCommand = true
app.Action = func(*cli.Context) error { return nil }
app.EnableBashCompletion = false
app.Metadata = map[string]interface{}{
"Name": "PhotoPrism",
"About": "PhotoPrism®",
"Edition": "ce",
"Version": "test",
}
// Parse photoprism command arguments.
photoprismFlagSet := flag.NewFlagSet("photoprism", flag.ContinueOnError)
for _, f := range app.Flags {
f.Apply(photoprismFlagSet)
}
LogErr(photoprismFlagSet.Parse(appArgs[1:]))
// Parse command test arguments.
flagSet := flag.NewFlagSet("test", flag.ContinueOnError)
LogErr(flagSet.Parse(cmdArgs))
// Create and return new test context.
return cli.NewContext(app, flagSet, cli.NewContext(app, photoprismFlagSet, nil))
}
func RunWithProvidedTestContext(ctx *cli.Context, cmd *cli.Command, args []string) (output string, err error) {
// Redirect the output from cli to buffer for transfer to output for testing
var catureOutput bytes.Buffer
oldWriter := ctx.App.Writer
ctx.App.Writer = &catureOutput
// Run command with test context.
output = capture.Output(func() {
err = cmd.Run(ctx, args...)
})
ctx.App.Writer = oldWriter
output += catureOutput.String()
return output, err
}