mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-10 04:05:42 +01:00
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 <xiaolunwen@gmail.com>
This commit is contained in:
parent
62a1322cf9
commit
7e920703f9
5 changed files with 163 additions and 56 deletions
76
contrib/fixtures/fixture_generation.go
Normal file
76
contrib/fixtures/fixture_generation.go
Normal file
|
@ -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")
|
||||
}
|
|
@ -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()
|
||||
}
|
45
models/fixture_generation.go
Normal file
45
models/fixture_generation.go
Normal file
|
@ -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
|
||||
}
|
34
models/fixture_test.go
Normal file
34
models/fixture_test.go
Normal file
|
@ -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")
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue