memos/test/test.go

38 lines
733 B
Go
Raw Normal View History

2023-04-01 22:47:19 +08:00
package test
import (
"fmt"
"net"
2023-04-01 22:47:19 +08:00
"testing"
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/server/version"
)
func getUnusedPort() int {
// Get a random unused port
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
panic(err)
}
defer listener.Close()
// Get the port number
port := listener.Addr().(*net.TCPAddr).Port
return port
}
2023-04-01 22:47:19 +08:00
func GetTestingProfile(t *testing.T) *profile.Profile {
// Get a temporary directory for the test data.
dir := t.TempDir()
mode := "dev"
port := getUnusedPort()
2023-04-01 22:47:19 +08:00
return &profile.Profile{
Mode: mode,
Port: port,
2023-04-01 22:47:19 +08:00
Data: dir,
DSN: fmt.Sprintf("%s/memos_%s.db", dir, mode),
Version: version.GetCurrentVersion(mode),
}
}