From 7e920703f94c4f2782860a72cc6beca5671736f5 Mon Sep 17 00:00:00 2001 From: guillep2k <18600385+guillep2k@users.noreply.github.com> Date: Sat, 15 Feb 2020 05:59:43 -0300 Subject: [PATCH] Move fixture generation to contrib and add test (#10277) * Add fixture gen tool and fix "access" test * Close file before exiting * Add missing repo_unit for repo id: 5 * Fix count on TestAPIOrgRepos * Generate access fixture from contrib and add test * Remove old access fixture generation * Fix lint Co-authored-by: Lunny Xiao --- contrib/fixtures/fixture_generation.go | 76 ++++++++++++++++++++++++++ models/fixture_access_test.go | 52 ------------------ models/fixture_generation.go | 45 +++++++++++++++ models/fixture_test.go | 34 ++++++++++++ models/unit_tests.go | 12 ++-- 5 files changed, 163 insertions(+), 56 deletions(-) create mode 100644 contrib/fixtures/fixture_generation.go delete mode 100644 models/fixture_access_test.go create mode 100644 models/fixture_generation.go create mode 100644 models/fixture_test.go diff --git a/contrib/fixtures/fixture_generation.go b/contrib/fixtures/fixture_generation.go new file mode 100644 index 000000000..802a2ca30 --- /dev/null +++ b/contrib/fixtures/fixture_generation.go @@ -0,0 +1,76 @@ +// 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 main + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + + "code.gitea.io/gitea/models" +) + +// To generate derivative fixtures, execute the following from Gitea's repository base dir: +// go run -tags 'sqlite sqlite_unlock_notify' contrib/fixtures/fixture_generation.go [fixture...] + +var ( + generators = []struct { + gen func() (string, error) + name string + }{ + { + models.GetYamlFixturesAccess, "access", + }, + } + fixturesDir string +) + +func main() { + pathToGiteaRoot := "." + fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures") + if err := models.CreateTestEngine(fixturesDir); err != nil { + fmt.Printf("CreateTestEngine: %+v", err) + os.Exit(1) + } + if err := models.PrepareTestDatabase(); err != nil { + fmt.Printf("PrepareTestDatabase: %+v\n", err) + os.Exit(1) + } + if len(os.Args) == 0 { + for _, r := range os.Args { + if err := generate(r); err != nil { + fmt.Printf("generate '%s': %+v\n", r, err) + os.Exit(1) + } + } + } else { + for _, g := range generators { + if err := generate(g.name); err != nil { + fmt.Printf("generate '%s': %+v\n", g.name, err) + os.Exit(1) + } + } + } +} + +func generate(name string) error { + for _, g := range generators { + if g.name == name { + data, err := g.gen() + if err != nil { + return err + } + path := filepath.Join(fixturesDir, name+".yml") + if err := ioutil.WriteFile(path, []byte(data), 0644); err != nil { + return fmt.Errorf("%s: %+v", path, err) + } + fmt.Printf("%s created.\n", path) + return nil + } + } + + return fmt.Errorf("generator not found") +} diff --git a/models/fixture_access_test.go b/models/fixture_access_test.go deleted file mode 100644 index 9a50d24d7..000000000 --- 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 000000000..e3be7bce3 --- /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 000000000..450e374f8 --- /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 eb4da37fe..b9f894af7 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 {