mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Commands: Refactor "show config-options" and "show config-yaml" tests
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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"})
|
||||
|
||||
output := capture.Stdout(func() {
|
||||
err = ShowConfigOptionsCommand.Run(ctx)
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, output, "PHOTOPRISM_IMPORT_PATH")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
|
||||
@@ -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
29
pkg/capture/stdout.go
Normal 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()
|
||||
}
|
||||
18
pkg/capture/stdout_test.go
Normal file
18
pkg/capture/stdout_test.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user