mirror of
https://github.com/photoprism/photoprism.git
synced 2025-10-05 16:57:17 +08:00
Tests: add tests to support new cli migrations transfer
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user