-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Use unique temp dirs in unit tests #3494
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
Changes from 3 commits
8b1b38a
fe5da86
1f1b6eb
e84a857
9ba38f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ package models | |
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net/url" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
@@ -18,7 +20,6 @@ import ( | |
| "github.com/go-xorm/xorm" | ||
| "github.com/stretchr/testify/assert" | ||
| "gopkg.in/testfixtures.v2" | ||
| "net/url" | ||
| ) | ||
|
|
||
| // NonexistentID an ID that will never exist | ||
|
|
@@ -27,32 +28,48 @@ const NonexistentID = 9223372036854775807 | |
| // giteaRoot a path to the gitea root | ||
| 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 | ||
| // test database. Creates the test database, and sets necessary settings. | ||
| func MainTest(m *testing.M, pathToGiteaRoot string) { | ||
| var err error | ||
| giteaRoot = pathToGiteaRoot | ||
| fixturesDir := filepath.Join(pathToGiteaRoot, "models", "fixtures") | ||
| if err = createTestEngine(fixturesDir); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error creating test engine: %v\n", err) | ||
| os.Exit(1) | ||
| fatalTestError("Error creating test engine: %v\n", err) | ||
| } | ||
|
|
||
| setting.AppURL = "https://try.gitea.io/" | ||
| setting.RunUser = "runuser" | ||
| setting.SSH.Port = 3000 | ||
| setting.SSH.Domain = "try.gitea.io" | ||
| setting.RepoRootPath = filepath.Join(os.TempDir(), "repos") | ||
| setting.AppDataPath = filepath.Join(os.TempDir(), "appdata") | ||
| setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos") | ||
| 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.StaticRootPath = pathToGiteaRoot | ||
| setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/") | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error url.Parse: %v\n", err) | ||
| os.Exit(1) | ||
| fatalTestError("url.Parse: %v\n", err) | ||
| } | ||
|
|
||
| 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 { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be
defered. Not done at the end