mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
CLI: Add RunWithTestContext function for command tests #3168
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user