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

planner: improve the generation for record the test cases #35369

Merged
merged 5 commits into from
Jun 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions testkit/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,14 @@ func loadTestSuiteData(dir, suiteName string) (res TestData, err error) {
if err != nil {
return res, err
}
if record {
res.output = make([]testCases, len(res.input))
for i := range res.input {
res.output[i].Name = res.input[i].Name
}
} else {
res.output, err = loadTestSuiteCases(fmt.Sprintf("%s_out.json", res.filePathPrefix))
if err != nil {
return res, err
}
if len(res.input) != len(res.output) {
return res, errors.New(fmt.Sprintf("Number of test input cases %d does not match test output cases %d", len(res.input), len(res.output)))
}

// Load all test cases result in order to keep the unrelated test results.
res.output, err = loadTestSuiteCases(fmt.Sprintf("%s_out.json", res.filePathPrefix))
if err != nil {
return res, err
}
if len(res.input) != len(res.output) {
return res, errors.New(fmt.Sprintf("Number of test input cases %d does not match test output cases %d", len(res.input), len(res.output)))
}
res.funcMap = make(map[string]int, len(res.input))
for i, test := range res.input {
Expand Down Expand Up @@ -188,16 +183,25 @@ func (td *TestData) generateOutputIfNeeded() error {
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
isRecord4ThisSuite := false
for i, test := range td.output {
err := enc.Encode(test.decodedOut)
if err != nil {
return err
if test.decodedOut != nil {
// Only update the results for the related test cases.
isRecord4ThisSuite = true
err := enc.Encode(test.decodedOut)
if err != nil {
return err
}
res := make([]byte, len(buf.Bytes()))
copy(res, buf.Bytes())
buf.Reset()
rm := json.RawMessage(res)
td.output[i].Cases = &rm
}
res := make([]byte, len(buf.Bytes()))
copy(res, buf.Bytes())
buf.Reset()
rm := json.RawMessage(res)
td.output[i].Cases = &rm
}
// Skip the record for the unrelated test files.
if !isRecord4ThisSuite {
return nil
}
err := enc.Encode(td.output)
if err != nil {
Expand Down