Skip to content

Move fixture generation to contrib and add test #10277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions contrib/fixtures/fixture_generation.go
Original file line number Diff line number Diff line change
@@ -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")
}
52 changes: 0 additions & 52 deletions models/fixture_access_test.go

This file was deleted.

45 changes: 45 additions & 0 deletions models/fixture_generation.go
Original file line number Diff line number Diff line change
@@ -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 changes: 34 additions & 0 deletions models/fixture_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
12 changes: 8 additions & 4 deletions models/unit_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down