diff --git a/contrib/fixtures/fixture_generation.go b/contrib/fixtures/fixture_generation.go new file mode 100644 index 00000000000..802a2ca30c9 --- /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 9a50d24d7c6..00000000000 --- 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 00000000000..e3be7bce304 --- /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 00000000000..450e374f86d --- /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 eb4da37fe51..b9f894af7c0 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 {