random_bugs: make it seedable and reusable

This commit is contained in:
Michael Muré 2018-08-19 21:26:35 +02:00
parent 08127d8d1c
commit 285e839486
No known key found for this signature in database
GPG Key ID: A4457C029293126F
2 changed files with 57 additions and 30 deletions

View File

@ -1,10 +1,7 @@
// +build ignore package random_bugs
package main
import ( import (
"math/rand" "math/rand"
"os"
"strings" "strings"
"time" "time"
@ -14,17 +11,31 @@ import (
"github.com/icrowley/fake" "github.com/icrowley/fake"
) )
const bugNumber = 15
const personNumber = 5
const minOp = 3
const maxOp = 20
type opsGenerator func(*bug.Bug, bug.Person) type opsGenerator func(*bug.Bug, bug.Person)
// This program will randomly generate a collection of bugs in the repository type Options struct {
// of the current path BugNumber int
func main() { PersonNumber int
rand.Seed(time.Now().UnixNano()) MinOp int
MaxOp int
}
func DefaultOptions() Options {
return Options{
BugNumber: 15,
PersonNumber: 5,
MinOp: 3,
MaxOp: 20,
}
}
func GenerateRandomBugs(repo repository.Repo, opts Options) {
GenerateRandomBugsWithSeed(repo, opts, time.Now().UnixNano())
}
func GenerateRandomBugsWithSeed(repo repository.Repo, opts Options, seed int64) {
rand.Seed(seed)
fake.Seed(seed)
opsGenerators := []opsGenerator{ opsGenerators := []opsGenerator{
comment, comment,
@ -35,31 +46,19 @@ func main() {
operations.Close, operations.Close,
} }
dir, err := os.Getwd() for i := 0; i < opts.BugNumber; i++ {
if err != nil {
panic(err)
}
repo, err := repository.NewGitRepo(dir, func(repo *repository.GitRepo) error {
return nil
})
if err != nil {
panic(err)
}
for i := 0; i < bugNumber; i++ {
addedLabels = []string{} addedLabels = []string{}
b, err := operations.Create(randomPerson(), fake.Sentence(), paragraphs()) b, err := operations.Create(randomPerson(opts.PersonNumber), fake.Sentence(), paragraphs())
if err != nil { if err != nil {
panic(err) panic(err)
} }
nOps := minOp + rand.Intn(maxOp-minOp) nOps := opts.MinOp + rand.Intn(opts.MaxOp-opts.MinOp)
for j := 0; j < nOps; j++ { for j := 0; j < nOps; j++ {
index := rand.Intn(len(opsGenerators)) index := rand.Intn(len(opsGenerators))
opsGenerators[index](b, randomPerson()) opsGenerators[index](b, randomPerson(opts.PersonNumber))
} }
err = b.Commit(repo) err = b.Commit(repo)
@ -78,7 +77,7 @@ func person() bug.Person {
var persons []bug.Person var persons []bug.Person
func randomPerson() bug.Person { func randomPerson(personNumber int) bug.Person {
if len(persons) == 0 { if len(persons) == 0 {
persons = make([]bug.Person, personNumber) persons = make([]bug.Person, personNumber)
for i := range persons { for i := range persons {

28
misc/random_bugs/main.go Normal file
View File

@ -0,0 +1,28 @@
// +build ignore
package main
import (
"os"
rb "github.com/MichaelMure/git-bug/misc/random_bugs"
"github.com/MichaelMure/git-bug/repository"
)
// This program will randomly generate a collection of bugs in the repository
// of the current path
func main() {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
repo, err := repository.NewGitRepo(dir, func(repo *repository.GitRepo) error {
return nil
})
if err != nil {
panic(err)
}
rb.GenerateRandomBugs(repo, rb.DefaultOptions())
}