Use unique temp dirs in unit tests (#3494)
* Use unique temp dirs in unit tests * Remove temp dirs after tests run * os.RemoveAll -> removeAllWithRetry
This commit is contained in:
parent
2f5c1ba1db
commit
d27d720f05
|
@ -6,6 +6,8 @@ package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -18,7 +20,6 @@ import (
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"gopkg.in/testfixtures.v2"
|
"gopkg.in/testfixtures.v2"
|
||||||
"net/url"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NonexistentID an ID that will never exist
|
// NonexistentID an ID that will never exist
|
||||||
|
@ -27,6 +28,11 @@ const NonexistentID = 9223372036854775807
|
||||||
// giteaRoot a path to the gitea root
|
// giteaRoot a path to the gitea root
|
||||||
var giteaRoot string
|
var giteaRoot string
|
||||||
|
|
||||||
|
func fatalTestError(fmtStr string, args ...interface{}) {
|
||||||
|
fmt.Fprintf(os.Stderr, fmtStr, args...)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// MainTest a reusable TestMain(..) function for unit tests that need to use a
|
// MainTest a reusable TestMain(..) function for unit tests that need to use a
|
||||||
// test database. Creates the test database, and sets necessary settings.
|
// test database. Creates the test database, and sets necessary settings.
|
||||||
func MainTest(m *testing.M, pathToGiteaRoot string) {
|
func MainTest(m *testing.M, pathToGiteaRoot string) {
|
||||||
|
@ -34,25 +40,36 @@ func MainTest(m *testing.M, pathToGiteaRoot string) {
|
||||||
giteaRoot = pathToGiteaRoot
|
giteaRoot = pathToGiteaRoot
|
||||||
fixturesDir := filepath.Join(pathToGiteaRoot, "models", "fixtures")
|
fixturesDir := filepath.Join(pathToGiteaRoot, "models", "fixtures")
|
||||||
if err = createTestEngine(fixturesDir); err != nil {
|
if err = createTestEngine(fixturesDir); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error creating test engine: %v\n", err)
|
fatalTestError("Error creating test engine: %v\n", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setting.AppURL = "https://try.gitea.io/"
|
setting.AppURL = "https://try.gitea.io/"
|
||||||
setting.RunUser = "runuser"
|
setting.RunUser = "runuser"
|
||||||
setting.SSH.Port = 3000
|
setting.SSH.Port = 3000
|
||||||
setting.SSH.Domain = "try.gitea.io"
|
setting.SSH.Domain = "try.gitea.io"
|
||||||
setting.RepoRootPath = filepath.Join(os.TempDir(), "repos")
|
setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos")
|
||||||
setting.AppDataPath = filepath.Join(os.TempDir(), "appdata")
|
if err != nil {
|
||||||
|
fatalTestError("TempDir: %v\n", err)
|
||||||
|
}
|
||||||
|
setting.AppDataPath, err = ioutil.TempDir(os.TempDir(), "appdata")
|
||||||
|
if err != nil {
|
||||||
|
fatalTestError("TempDir: %v\n", err)
|
||||||
|
}
|
||||||
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/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error url.Parse: %v\n", err)
|
fatalTestError("url.Parse: %v\n", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(m.Run())
|
exitStatus := m.Run()
|
||||||
|
if err = removeAllWithRetry(setting.RepoRootPath); err != nil {
|
||||||
|
fatalTestError("os.RemoveAll: %v\n", err)
|
||||||
|
}
|
||||||
|
if err = removeAllWithRetry(setting.AppDataPath); err != nil {
|
||||||
|
fatalTestError("os.RemoveAll: %v\n", err)
|
||||||
|
}
|
||||||
|
os.Exit(exitStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTestEngine(fixturesDir string) error {
|
func createTestEngine(fixturesDir string) error {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user