Commands: Refactor "show config-options" and "show config-yaml" tests

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2024-07-01 08:58:39 +02:00
parent 3005d83baf
commit d25b555dbc
6 changed files with 61 additions and 7 deletions

View File

@@ -20,8 +20,8 @@ func TestMain(m *testing.M) {
c := config.NewTestConfig("commands")
get.SetConfig(c)
defer c.CloseDb()
// Init config and connect to database.
InitConfig = func(ctx *cli.Context) (*config.Config, error) {
return c, c.Init()
}
@@ -29,6 +29,9 @@ func TestMain(m *testing.M) {
// Run unit tests.
code := m.Run()
// Close database connection.
c.CloseDb()
os.Exit(code)
}

View File

@@ -3,15 +3,19 @@ package commands
import (
"testing"
"github.com/photoprism/photoprism/pkg/capture"
"github.com/stretchr/testify/assert"
)
func TestShowConfigOptionsCommand(t *testing.T) {
var err error
ctx := NewTestContext([]string{})
ctx := NewTestContext([]string{"config-options", "--md"})
err = ShowConfigOptionsCommand.Run(ctx)
output := capture.Stdout(func() {
err = ShowConfigOptionsCommand.Run(ctx)
})
assert.NoError(t, err)
assert.Contains(t, output, "PHOTOPRISM_IMPORT_PATH")
}

View File

@@ -3,7 +3,6 @@ package commands
import (
"testing"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/pkg/capture"
"github.com/stretchr/testify/assert"
)
@@ -11,9 +10,9 @@ import (
func TestShowConfigYamlCommand(t *testing.T) {
var err error
ctx := config.CliTestContext()
ctx := NewTestContext([]string{"config-yaml", "--md"})
output := capture.Output(func() {
output := capture.Stdout(func() {
err = ShowConfigYamlCommand.Run(ctx)
})

View File

@@ -13,12 +13,13 @@ var (
// CancelAll requests to stop all activities.
func CancelAll() {
UpdatePeople.Cancel()
IndexWorker.Cancel()
SyncWorker.Cancel()
BackupWorker.Cancel()
ShareWorker.Cancel()
MetaWorker.Cancel()
FacesWorker.Cancel()
UpdatePeople.Cancel()
}
// WorkersRunning checks if a worker is currently running.

29
pkg/capture/stdout.go Normal file
View File

@@ -0,0 +1,29 @@
package capture
import (
"bytes"
"io"
"os"
)
// Stdout returns output to stdout for testing.
func Stdout(f func()) string {
r, w, err := os.Pipe()
if err != nil {
panic(err)
}
stdout := os.Stdout
os.Stdout = w
defer func() {
os.Stdout = stdout
}()
f()
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}

View File

@@ -0,0 +1,18 @@
package capture
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStdout(t *testing.T) {
result := Stdout(func() {
fmt.Fprint(os.Stdout, "foo")
fmt.Fprint(os.Stderr, "bar")
})
assert.Equal(t, "foo", result)
}