-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (77 loc) · 1.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"regexp"
"strconv"
"strings"
)
var pkgRE = regexp.MustCompile(`package (?P<package>\w+)`)
func main() {
if len(os.Args) < 2 {
log.Fatal("please pass percentage float64 as first argument")
}
pct, err := strconv.ParseFloat(os.Args[1], 64)
if err != nil {
log.Fatal(err)
}
if pct < 0.0 || pct >= 100.0 {
log.Fatal("please enter a percentage > 0 and < 100")
}
pct /= 100.0
fs, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
lines := 0
wd, err := os.Getwd()
if err != nil {
wd = "main"
}
pkg := path.Base(wd)
for _, fi := range fs {
n := fi.Name()
if !fi.IsDir() && strings.HasSuffix(n, ".go") && !strings.HasSuffix(n, "_test.go") && n != "bs.go" && n != "bs_test.go" {
f, err := ioutil.ReadFile(fi.Name())
if err != nil {
log.Fatal(err)
}
lines += bytes.Count(f, []byte{'\n'})
for _, submatches := range pkgRE.FindAllSubmatchIndex(f, -1) {
bs := pkgRE.Expand([]byte{}, []byte("$package"), f, submatches)
if len(bs) > 0 {
pkg = string(bs)
}
}
}
}
tgt := 1.0 - pct
gen := float64(lines) / tgt
f, err := os.OpenFile("bs.go", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0664)
if err != nil {
log.Fatal(err)
}
bw := bufio.NewWriter(f)
fmt.Fprintf(f, "package %s\nfunc bs() {\n\tbs := 1\n", pkg)
for i := 0; i < int(gen); i++ {
bw.Write([]byte("\tbs = 1\n"))
}
bw.Flush()
fmt.Fprintf(f, "\n_ = bs\n}\n")
f.Close()
f, err = os.OpenFile("bs_test.go", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0664)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(f, `package %s
import "testing"
func TestBS(t *testing.T) {
bs()
}
`, pkg)
}