Prevent services/mailer/mailer_test.go tests from deleteing data directory (#17941)

Running `make test-backend` will delete `data/` due to reloading the configuration and resetting the appdatapath.

This PR removes this unnecessary config reload but also adds extra code in to the unittest main to prevent its cleanup from deleting the wrong directory.

Signed-off-by: Andrew Thornton <art27@cantab.net>
tokarchuk/v1.17
zeripath 3 years ago committed by GitHub
parent 3ca5dc7e32
commit f550e356d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      models/migrations/migrations_test.go
  2. 12
      models/unittest/testdb.go
  3. 13
      services/mailer/mailer_test.go

@ -8,6 +8,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -57,6 +58,14 @@ func TestMain(m *testing.M) {
setting.CustomConf = giteaConf setting.CustomConf = giteaConf
} }
tmpDataPath, err := ioutil.TempDir("", "data")
if err != nil {
fmt.Printf("Unable to create temporary data path %v\n", err)
os.Exit(1)
}
setting.AppDataPath = tmpDataPath
setting.SetCustomPathAndConf("", "", "") setting.SetCustomPathAndConf("", "", "")
setting.LoadForTest() setting.LoadForTest()
git.CheckLFSVersion() git.CheckLFSVersion()
@ -68,7 +77,7 @@ func TestMain(m *testing.M) {
if err := removeAllWithRetry(setting.RepoRootPath); err != nil { if err := removeAllWithRetry(setting.RepoRootPath); err != nil {
fmt.Fprintf(os.Stderr, "os.RemoveAll: %v\n", err) fmt.Fprintf(os.Stderr, "os.RemoveAll: %v\n", err)
} }
if err := removeAllWithRetry(setting.AppDataPath); err != nil { if err := removeAllWithRetry(tmpDataPath); err != nil {
fmt.Fprintf(os.Stderr, "os.RemoveAll: %v\n", err) fmt.Fprintf(os.Stderr, "os.RemoveAll: %v\n", err)
} }
os.Exit(exitStatus) os.Exit(exitStatus)

@ -67,14 +67,16 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
setting.SSH.Port = 3000 setting.SSH.Port = 3000
setting.SSH.Domain = "try.gitea.io" setting.SSH.Domain = "try.gitea.io"
setting.Database.UseSQLite3 = true setting.Database.UseSQLite3 = true
setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos") repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos")
if err != nil { if err != nil {
fatalTestError("TempDir: %v\n", err) fatalTestError("TempDir: %v\n", err)
} }
setting.AppDataPath, err = os.MkdirTemp(os.TempDir(), "appdata") setting.RepoRootPath = repoRootPath
appDataPath, err := os.MkdirTemp(os.TempDir(), "appdata")
if err != nil { if err != nil {
fatalTestError("TempDir: %v\n", err) fatalTestError("TempDir: %v\n", err)
} }
setting.AppDataPath = appDataPath
setting.AppWorkPath = pathToGiteaRoot setting.AppWorkPath = pathToGiteaRoot
setting.StaticRootPath = pathToGiteaRoot setting.StaticRootPath = pathToGiteaRoot
setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/") setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/")
@ -95,7 +97,7 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
fatalTestError("storage.Init: %v\n", err) fatalTestError("storage.Init: %v\n", err)
} }
if err = util.RemoveAll(setting.RepoRootPath); err != nil { if err = util.RemoveAll(repoRootPath); err != nil {
fatalTestError("util.RemoveAll: %v\n", err) fatalTestError("util.RemoveAll: %v\n", err)
} }
if err = util.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil { if err = util.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
@ -103,10 +105,10 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
} }
exitStatus := m.Run() exitStatus := m.Run()
if err = util.RemoveAll(setting.RepoRootPath); err != nil { if err = util.RemoveAll(repoRootPath); err != nil {
fatalTestError("util.RemoveAll: %v\n", err) fatalTestError("util.RemoveAll: %v\n", err)
} }
if err = util.RemoveAll(setting.AppDataPath); err != nil { if err = util.RemoveAll(appDataPath); err != nil {
fatalTestError("util.RemoveAll: %v\n", err) fatalTestError("util.RemoveAll: %v\n", err)
} }
os.Exit(exitStatus) os.Exit(exitStatus)

@ -9,17 +9,16 @@ import (
"time" "time"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestGenerateMessageID(t *testing.T) { func TestGenerateMessageID(t *testing.T) {
setting.LoadForTest(` var mailService = setting.Mailer{
[mailer] From: "test@gitea.com",
ENABLED = true }
FROM = test@domain.com
`) setting.MailService = &mailService
setting.NewServices() setting.Domain = "localhost"
date := time.Date(2000, 01, 02, 03, 04, 05, 06, time.UTC) date := time.Date(2000, 01, 02, 03, 04, 05, 06, time.UTC)
m := NewMessageFrom(nil, "display-name", "from-address", "subject", "body") m := NewMessageFrom(nil, "display-name", "from-address", "subject", "body")

Loading…
Cancel
Save