CLI: Add RunWithTestContext function for command tests #3168

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2024-12-06 09:08:38 +01:00
parent 374e0de8cc
commit cd4e6f1b57
61 changed files with 231 additions and 329 deletions

View File

@@ -1,27 +1,15 @@
package commands
import (
"fmt"
"runtime/debug"
"testing"
"github.com/stretchr/testify/assert"
"github.com/photoprism/photoprism/pkg/capture"
)
func TestClientsListCommand(t *testing.T) {
t.Run("All", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
args := []string{"ls"}
ctx := NewTestContext(args)
// Run command with test context.
output := capture.Output(func() {
err = ClientsListCommand.Run(ctx, args...)
})
output, err := RunWithTestContext(ClientsListCommand, []string{"ls"})
// Check command output for plausibility.
// t.Logf(output)
@@ -32,16 +20,8 @@ func TestClientsListCommand(t *testing.T) {
assert.NotContains(t, output, "visitor")
})
t.Run("Monitoring", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
args := []string{"ls", "monitoring"}
ctx := NewTestContext(args)
// Run command with test context.
output := capture.Output(func() {
err = ClientsListCommand.Run(ctx, args...)
})
output, err := RunWithTestContext(ClientsListCommand, []string{"ls", "monitoring"})
// Check command output for plausibility.
// t.Logf(output)
@@ -53,16 +33,8 @@ func TestClientsListCommand(t *testing.T) {
assert.NotContains(t, output, "visitor")
})
t.Run("CSV", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
args := []string{"ls", "--csv", "monitoring"}
ctx := NewTestContext(args)
// Run command with test context.
output := capture.Output(func() {
err = ClientsListCommand.Run(ctx, args...)
})
output, err := RunWithTestContext(ClientsListCommand, []string{"ls", "--csv", "monitoring"})
// Check command output for plausibility.
// t.Logf(output)
@@ -74,16 +46,8 @@ func TestClientsListCommand(t *testing.T) {
assert.NotContains(t, output, "alice")
})
t.Run("NoResult", func(t *testing.T) {
var err error
// Create test context with flags and arguments.
args := []string{"ls", "notexisting"}
ctx := NewTestContext(args)
// Run command with test context.
output := capture.Output(func() {
err = ClientsListCommand.Run(ctx, args...)
})
output, err := RunWithTestContext(ClientsListCommand, []string{"ls", "notexisting"})
// Check command output for plausibility.
// t.Logf(output)
@@ -91,27 +55,12 @@ func TestClientsListCommand(t *testing.T) {
assert.Empty(t, output)
})
t.Run("Error", func(t *testing.T) {
var err error
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("error: %s \nstack: %s", r, debug.Stack())
assert.Error(t, err)
}
}()
// Create test context with flags and arguments.
args := []string{"ls", "--xyz", "monitoring"}
ctx := NewTestContext(args)
// Run command with test context.
output := capture.Output(func() {
err = ClientsListCommand.Run(ctx, args...)
})
output, err := RunWithTestContext(ClientsListCommand, []string{"ls", "--xyz", "monitoring"})
// Check command output for plausibility.
// t.Logf(output)
assert.Error(t, err)
assert.Empty(t, output)
assert.Error(t, err)
})
}