Skip to content
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

Commit small test json files for each test instead of the large ones #509

Merged
merged 15 commits into from
Feb 13, 2025
  •  
  •  
  •  
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@
.bin

# CI
bin
bin

# large test files
tests.json
59 changes: 48 additions & 11 deletions qbft/spectest/generate/main.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,52 @@ import (

func main() {
clearStateComparisonFolder()
clearTestsFolder()

all := map[string]tests.SpecTest{}
for _, testF := range spectest.AllTests {
test := testF()

// write json test
n := reflect.TypeOf(test).String() + "_" + test.TestName()
if all[n] != nil {
panic(fmt.Sprintf("duplicate test: %s\n", n))
}
all[n] = test
}

byts, err := json.Marshal(all)
if err != nil {
panic(err.Error())
log.Printf("found %d tests\n", len(all))
if len(all) != len(spectest.AllTests) {
log.Fatalf("did not generate all tests\n")
}

if len(all) != len(spectest.AllTests) {
panic("did not generate all tests\n")
// write small json files for each test
// try to create directory if it doesn't exist
_, basedir, _, ok := runtime.Caller(0)
if !ok {
log.Fatalf("no caller info")
}
testsDir := filepath.Join(strings.TrimSuffix(basedir, "main.go"), "tests")
if err := os.MkdirAll(testsDir, 0700); err != nil && !os.IsExist(err) {
panic(err.Error())
}
for name, test := range all {
byts, err := json.MarshalIndent(test, "", " ")
if err != nil {
panic(err.Error())
}
name = strings.ReplaceAll(name, " ", "_")
name = strings.ReplaceAll(name, "*", "")
name = "tests/" + name
writeJson(name, byts)
}

log.Printf("found %d tests\n", len(all))
writeJson(byts)
// write large tests.json file
byts, err := json.MarshalIndent(all, "", " ")
if err != nil {
panic(err.Error())
}
writeJson("tests", byts)

// write state comparison json files
for _, testF := range spectest.AllTests {
test := testF()
// generate post state comparison
Expand Down Expand Up @@ -73,6 +94,22 @@ func clearStateComparisonFolder() {
}
}

func clearTestsFolder() {
_, basedir, _, ok := runtime.Caller(0)
if !ok {
panic("no caller info")
}
dir := filepath.Join(strings.TrimSuffix(basedir, "main.go"), "tests")

if err := os.RemoveAll(dir); err != nil {
panic(err.Error())
}

if err := os.Mkdir(dir, 0700); err != nil {
panic(err.Error())
}
}

func writeJsonStateComparison(name, testType string, post interface{}) {
if post == nil { // If nil, test not supporting post state comparison yet
log.Printf("skipping state comparison json, not supported: %s\n", name)
Expand Down Expand Up @@ -108,7 +145,7 @@ func scDir(testType string) string {
return scDir
}

func writeJson(data []byte) {
func writeJson(name string, data []byte) {
_, basedir, _, ok := runtime.Caller(0)
if !ok {
panic("no caller info")
Expand All @@ -118,7 +155,7 @@ func writeJson(data []byte) {
// try to create directory if it doesn't exist
_ = os.Mkdir(basedir, os.ModeDir)

file := filepath.Join(basedir, "tests.json")
file := filepath.Join(basedir, name+".json")
log.Printf("writing spec tests json to: %s\n", file)
if err := os.WriteFile(file, data, 0400); err != nil {
panic(err.Error())
Expand Down
1 change: 0 additions & 1 deletion qbft/spectest/generate/tests.json

This file was deleted.

Loading
Loading