From 8b1b38a4e3f5ab054e3a953f6aa2bc49e2dfa6ee Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Sun, 11 Feb 2018 20:57:13 -0800 Subject: [PATCH 1/3] Use unique temp dirs in unit tests --- models/unit_tests.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/models/unit_tests.go b/models/unit_tests.go index 962b1a494406d..d44d0fea3ee1a 100644 --- a/models/unit_tests.go +++ b/models/unit_tests.go @@ -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 @@ -42,8 +43,16 @@ func MainTest(m *testing.M, pathToGiteaRoot string) { 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 { + fmt.Fprintf(os.Stderr, "TempDir: %v\n", err) + os.Exit(1) + } + setting.AppDataPath, err = ioutil.TempDir(os.TempDir(), "appdata") + if err != nil { + fmt.Fprintf(os.Stderr, "TempDir: %v\n", err) + os.Exit(1) + } setting.AppWorkPath = pathToGiteaRoot setting.StaticRootPath = pathToGiteaRoot setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/") From fe5da86c0d5b71edec30ce175c596c91ec9e03b9 Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Mon, 12 Feb 2018 09:37:45 -0800 Subject: [PATCH 2/3] Remove temp dirs after tests run --- models/unit_tests.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/models/unit_tests.go b/models/unit_tests.go index d44d0fea3ee1a..f519b7abe3596 100644 --- a/models/unit_tests.go +++ b/models/unit_tests.go @@ -28,6 +28,11 @@ 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) { @@ -35,8 +40,7 @@ func MainTest(m *testing.M, pathToGiteaRoot string) { 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/" @@ -45,23 +49,27 @@ func MainTest(m *testing.M, pathToGiteaRoot string) { setting.SSH.Domain = "try.gitea.io" setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos") if err != nil { - fmt.Fprintf(os.Stderr, "TempDir: %v\n", err) - os.Exit(1) + fatalTestError("TempDir: %v\n", err) } setting.AppDataPath, err = ioutil.TempDir(os.TempDir(), "appdata") if err != nil { - fmt.Fprintf(os.Stderr, "TempDir: %v\n", err) - os.Exit(1) + 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 = os.RemoveAll(setting.RepoRootPath); err != nil { + fatalTestError("os.RemoveAll: %v\n", err) + } + if err = os.RemoveAll(setting.AppDataPath); err != nil { + fatalTestError("os.RemoveAll: %v\n", err) + } + os.Exit(exitStatus) } func createTestEngine(fixturesDir string) error { From 1f1b6eb96e4ebcf0b3d5ab78b86953602a9fe38c Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Mon, 19 Feb 2018 22:29:09 -0800 Subject: [PATCH 3/3] os.RemoveAll -> removeAllWithRetry --- models/unit_tests.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/unit_tests.go b/models/unit_tests.go index f519b7abe3596..9013fb9871677 100644 --- a/models/unit_tests.go +++ b/models/unit_tests.go @@ -63,10 +63,10 @@ func MainTest(m *testing.M, pathToGiteaRoot string) { } exitStatus := m.Run() - if err = os.RemoveAll(setting.RepoRootPath); err != nil { + if err = removeAllWithRetry(setting.RepoRootPath); err != nil { fatalTestError("os.RemoveAll: %v\n", err) } - if err = os.RemoveAll(setting.AppDataPath); err != nil { + if err = removeAllWithRetry(setting.AppDataPath); err != nil { fatalTestError("os.RemoveAll: %v\n", err) } os.Exit(exitStatus)