2023-04-01 22:47:19 +08:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-17 21:34:59 +08:00
|
|
|
"net"
|
2023-09-29 09:15:54 +08:00
|
|
|
"os"
|
2023-04-01 22:47:19 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/server/profile"
|
|
|
|
"github.com/usememos/memos/server/version"
|
|
|
|
)
|
|
|
|
|
2023-04-17 21:34:59 +08:00
|
|
|
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()
|
2023-04-25 23:27:38 +08:00
|
|
|
mode := "dev"
|
2023-04-17 21:34:59 +08:00
|
|
|
port := getUnusedPort()
|
2023-09-29 09:15:54 +08:00
|
|
|
driver := getDriverFromEnv()
|
|
|
|
dsn := os.Getenv("DSN")
|
|
|
|
if driver == "sqlite" {
|
|
|
|
dsn = fmt.Sprintf("%s/memos_%s.db", dir, mode)
|
|
|
|
}
|
2023-04-01 22:47:19 +08:00
|
|
|
return &profile.Profile{
|
|
|
|
Mode: mode,
|
2023-04-17 21:34:59 +08:00
|
|
|
Port: port,
|
2023-04-01 22:47:19 +08:00
|
|
|
Data: dir,
|
2023-09-29 09:15:54 +08:00
|
|
|
DSN: dsn,
|
|
|
|
Driver: driver,
|
2023-04-01 22:47:19 +08:00
|
|
|
Version: version.GetCurrentVersion(mode),
|
|
|
|
}
|
|
|
|
}
|
2023-09-29 09:15:54 +08:00
|
|
|
|
|
|
|
func getDriverFromEnv() string {
|
|
|
|
driver := os.Getenv("DRIVER")
|
|
|
|
if driver == "" {
|
|
|
|
driver = "sqlite"
|
|
|
|
}
|
|
|
|
return driver
|
|
|
|
}
|