Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion models/migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -57,6 +58,14 @@ func TestMain(m *testing.M) {
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.LoadForTest()
git.CheckLFSVersion()
Expand All @@ -68,7 +77,7 @@ func TestMain(m *testing.M) {
if err := removeAllWithRetry(setting.RepoRootPath); err != nil {
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)
}
os.Exit(exitStatus)
Expand Down
12 changes: 7 additions & 5 deletions models/unittest/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
setting.SSH.Port = 3000
setting.SSH.Domain = "try.gitea.io"
setting.Database.UseSQLite3 = true
setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos")
repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos")
if err != nil {
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 {
fatalTestError("TempDir: %v\n", err)
}
setting.AppDataPath = appDataPath
setting.AppWorkPath = pathToGiteaRoot
setting.StaticRootPath = pathToGiteaRoot
setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/")
Expand All @@ -95,18 +97,18 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
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)
}
if err = util.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
fatalTestError("util.CopyDir: %v\n", err)
}

exitStatus := m.Run()
if err = util.RemoveAll(setting.RepoRootPath); err != nil {
if err = util.RemoveAll(repoRootPath); err != nil {
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)
}
os.Exit(exitStatus)
Expand Down
13 changes: 6 additions & 7 deletions services/mailer/mailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import (
"time"

"code.gitea.io/gitea/modules/setting"

"github.com/stretchr/testify/assert"
)

func TestGenerateMessageID(t *testing.T) {
setting.LoadForTest(`
[mailer]
ENABLED = true
FROM = test@domain.com
`)
setting.NewServices()
var mailService = setting.Mailer{
From: "test@gitea.com",
}

setting.MailService = &mailService
setting.Domain = "localhost"

date := time.Date(2000, 01, 02, 03, 04, 05, 06, time.UTC)
m := NewMessageFrom(nil, "display-name", "from-address", "subject", "body")
Expand Down