You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.7 KiB

  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "code.gitea.io/gitea/models"
  11. )
  12. // To generate derivative fixtures, execute the following from Gitea's repository base dir:
  13. // go run -tags 'sqlite sqlite_unlock_notify' contrib/fixtures/fixture_generation.go [fixture...]
  14. var (
  15. generators = []struct {
  16. gen func() (string, error)
  17. name string
  18. }{
  19. {
  20. models.GetYamlFixturesAccess, "access",
  21. },
  22. }
  23. fixturesDir string
  24. )
  25. func main() {
  26. pathToGiteaRoot := "."
  27. fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
  28. if err := models.CreateTestEngine(fixturesDir); err != nil {
  29. fmt.Printf("CreateTestEngine: %+v", err)
  30. os.Exit(1)
  31. }
  32. if err := models.PrepareTestDatabase(); err != nil {
  33. fmt.Printf("PrepareTestDatabase: %+v\n", err)
  34. os.Exit(1)
  35. }
  36. if len(os.Args) == 0 {
  37. for _, r := range os.Args {
  38. if err := generate(r); err != nil {
  39. fmt.Printf("generate '%s': %+v\n", r, err)
  40. os.Exit(1)
  41. }
  42. }
  43. } else {
  44. for _, g := range generators {
  45. if err := generate(g.name); err != nil {
  46. fmt.Printf("generate '%s': %+v\n", g.name, err)
  47. os.Exit(1)
  48. }
  49. }
  50. }
  51. }
  52. func generate(name string) error {
  53. for _, g := range generators {
  54. if g.name == name {
  55. data, err := g.gen()
  56. if err != nil {
  57. return err
  58. }
  59. path := filepath.Join(fixturesDir, name+".yml")
  60. if err := ioutil.WriteFile(path, []byte(data), 0644); err != nil {
  61. return fmt.Errorf("%s: %+v", path, err)
  62. }
  63. fmt.Printf("%s created.\n", path)
  64. return nil
  65. }
  66. }
  67. return fmt.Errorf("generator not found")
  68. }