test.go 1006 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package test
  2. import (
  3. "fmt"
  4. "net"
  5. "os"
  6. "testing"
  7. "github.com/usememos/memos/server/profile"
  8. "github.com/usememos/memos/server/version"
  9. )
  10. func getUnusedPort() int {
  11. // Get a random unused port
  12. listener, err := net.Listen("tcp", "localhost:0")
  13. if err != nil {
  14. panic(err)
  15. }
  16. defer listener.Close()
  17. // Get the port number
  18. port := listener.Addr().(*net.TCPAddr).Port
  19. return port
  20. }
  21. func GetTestingProfile(t *testing.T) *profile.Profile {
  22. // Get a temporary directory for the test data.
  23. dir := t.TempDir()
  24. mode := "dev"
  25. port := getUnusedPort()
  26. driver := getDriverFromEnv()
  27. dsn := os.Getenv("DSN")
  28. if driver == "sqlite" {
  29. dsn = fmt.Sprintf("%s/memos_%s.db", dir, mode)
  30. }
  31. println("dsn", dsn, driver)
  32. return &profile.Profile{
  33. Mode: mode,
  34. Port: port,
  35. Data: dir,
  36. DSN: dsn,
  37. Driver: driver,
  38. Version: version.GetCurrentVersion(mode),
  39. }
  40. }
  41. func getDriverFromEnv() string {
  42. driver := os.Getenv("DRIVER")
  43. if driver == "" {
  44. driver = "sqlite"
  45. }
  46. return driver
  47. }