2020-02-15 11:59:43 +03:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-02-15 11:59:43 +03:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-11-12 17:36:47 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2020-02-15 11:59:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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")
|
2021-11-12 17:36:47 +03:00
|
|
|
if err := unittest.CreateTestEngine(unittest.FixturesOptions{
|
2021-09-24 14:32:56 +03:00
|
|
|
Dir: fixturesDir,
|
|
|
|
}); err != nil {
|
2020-02-15 11:59:43 +03:00
|
|
|
fmt.Printf("CreateTestEngine: %+v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-11-12 17:36:47 +03:00
|
|
|
if err := unittest.PrepareTestDatabase(); err != nil {
|
2020-02-15 11:59:43 +03:00
|
|
|
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")
|
2022-01-20 20:46:10 +03:00
|
|
|
if err := os.WriteFile(path, []byte(data), 0o644); err != nil {
|
2020-02-15 11:59:43 +03:00
|
|
|
return fmt.Errorf("%s: %+v", path, err)
|
|
|
|
}
|
|
|
|
fmt.Printf("%s created.\n", path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("generator not found")
|
|
|
|
}
|