diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/fixture_access_test.go | 52 | ||||
-rw-r--r-- | models/fixture_generation.go | 45 | ||||
-rw-r--r-- | models/fixture_test.go | 34 | ||||
-rw-r--r-- | models/unit_tests.go | 12 |
4 files changed, 87 insertions, 56 deletions
diff --git a/models/fixture_access_test.go b/models/fixture_access_test.go deleted file mode 100644 index 9a50d24d7c..0000000000 --- a/models/fixture_access_test.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -// +build access_fixtures - -package models - -// This file is excluded from build and tests, and is intended for assisting -// in keeping access.yml in sync with the other .yml files. - -// To use it, do: -// cd models -// go test -tags "access_fixtures sqlite sqlite_unlock_notify" -run TestBuildAccessFixturesYaml - -import ( - "bufio" - "fmt" - "os" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestBuildAccessFixturesYaml(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) - - repos := make([]*Repository, 0, 50) - assert.NoError(t, x.Find(&repos)) - for _, repo := range repos { - repo.MustOwner() - assert.NoError(t, repo.RecalculateAccesses()) - } - - f, err := os.Create("fixtures/access.yml") - assert.NoError(t, err) - w := bufio.NewWriter(f) - - accesses := make([]*Access, 0, 200) - assert.NoError(t, x.OrderBy("user_id, repo_id").Find(&accesses)) - for i, a := range accesses { - fmt.Fprintf(w, "-\n") - fmt.Fprintf(w, " id: %d\n", i+1) - fmt.Fprintf(w, " user_id: %d\n", a.UserID) - fmt.Fprintf(w, " repo_id: %d\n", a.RepoID) - fmt.Fprintf(w, " mode: %d\n", a.Mode) - fmt.Fprintf(w, "\n") - } - - w.Flush() - f.Close() -} diff --git a/models/fixture_generation.go b/models/fixture_generation.go new file mode 100644 index 0000000000..e3be7bce30 --- /dev/null +++ b/models/fixture_generation.go @@ -0,0 +1,45 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "fmt" + "strings" +) + +// GetYamlFixturesAccess returns a string containing the contents +// for the access table, as recalculated using repo.RecalculateAccesses() +func GetYamlFixturesAccess() (string, error) { + + repos := make([]*Repository, 0, 50) + if err := x.Find(&repos); err != nil { + return "", err + } + + for _, repo := range repos { + repo.MustOwner() + if err := repo.RecalculateAccesses(); err != nil { + return "", err + } + } + + var b strings.Builder + + accesses := make([]*Access, 0, 200) + if err := x.OrderBy("user_id, repo_id").Find(&accesses); err != nil { + return "", err + } + + for i, a := range accesses { + fmt.Fprintf(&b, "-\n") + fmt.Fprintf(&b, " id: %d\n", i+1) + fmt.Fprintf(&b, " user_id: %d\n", a.UserID) + fmt.Fprintf(&b, " repo_id: %d\n", a.RepoID) + fmt.Fprintf(&b, " mode: %d\n", a.Mode) + fmt.Fprintf(&b, "\n") + } + + return b.String(), nil +} diff --git a/models/fixture_test.go b/models/fixture_test.go new file mode 100644 index 0000000000..450e374f86 --- /dev/null +++ b/models/fixture_test.go @@ -0,0 +1,34 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "io/ioutil" + "path/filepath" + "testing" + + "code.gitea.io/gitea/modules/util" + + "github.com/stretchr/testify/assert" +) + +func TestFixtureGeneration(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + test := func(gen func() (string, error), name string) { + expected, err := gen() + if !assert.NoError(t, err) { + return + } + bytes, err := ioutil.ReadFile(filepath.Join(fixturesDir, name+".yml")) + if !assert.NoError(t, err) { + return + } + data := string(util.NormalizeEOL(bytes)) + assert.True(t, data == expected, "Differences detected for %s.yml", name) + } + + test(GetYamlFixturesAccess, "access") +} diff --git a/models/unit_tests.go b/models/unit_tests.go index eb4da37fe5..b9f894af7c 100644 --- a/models/unit_tests.go +++ b/models/unit_tests.go @@ -28,7 +28,10 @@ import ( const NonexistentID = int64(math.MaxInt64) // giteaRoot a path to the gitea root -var giteaRoot string +var ( + giteaRoot string + fixturesDir string +) func fatalTestError(fmtStr string, args ...interface{}) { fmt.Fprintf(os.Stderr, fmtStr, args...) @@ -40,8 +43,8 @@ func fatalTestError(fmtStr string, args ...interface{}) { func MainTest(m *testing.M, pathToGiteaRoot string) { var err error giteaRoot = pathToGiteaRoot - fixturesDir := filepath.Join(pathToGiteaRoot, "models", "fixtures") - if err = createTestEngine(fixturesDir); err != nil { + fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures") + if err = CreateTestEngine(fixturesDir); err != nil { fatalTestError("Error creating test engine: %v\n", err) } @@ -82,7 +85,8 @@ func MainTest(m *testing.M, pathToGiteaRoot string) { os.Exit(exitStatus) } -func createTestEngine(fixturesDir string) error { +// CreateTestEngine creates a memory database and loads the fixture data from fixturesDir +func CreateTestEngine(fixturesDir string) error { var err error x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared") if err != nil { |