Skip to content
Draft
Show file tree
Hide file tree
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
25 changes: 24 additions & 1 deletion cmd/generate-stable-protos/corpus_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
package main

import "testing"
import (
"testing"

"github.com/temporalio/api/cmd/generate-stable-protos/internal/oracle"
)

// TestRepoAPINext generates the stable tree from the repository's own api_next tree and
// checks it against the oracle, so real-world declarations, options and comments are
// covered beyond the hand-written fixtures.
func TestRepoAPINext(t *testing.T) {
t.Parallel()
const root = "../.."
out := t.TempDir()
if err := testConfig.stableProtosGenerator(t.Context(), root, out); err != nil {
t.Fatal(err)
}
result, err := oracle.Check(root, out)
if err != nil {
t.Fatal(err)
}
for _, problem := range result.AllProblems() {
t.Error(problem)
}
}

// TestRepoAPINextIdempotent checks that the stable tree generated from the repository's
// api_next tree is a fixed point of the generator.
Expand Down
58 changes: 52 additions & 6 deletions cmd/generate-stable-protos/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ import (
"slices"
"strings"
"testing"

"github.com/temporalio/api/cmd/generate-stable-protos/internal/oracle"
)

// TestFixtures runs the generator on every fixture under testdata. A fixture is a
// directory with an input/ api_next tree, and optionally an expected.json and an
// expected/ tree holding the raw generator output without its generated header.
// TestFixtures runs every fixture under testdata. A fixture is a directory with an
// input/ api_next tree, and optionally an expected.json and an expected/ tree holding the
// raw generator output without its generated header. A fixture with an output/ tree
// tests the oracle instead of the generator: output/ stands in for the generator's
// output, and the oracle must report exactly the expected problems.
func TestFixtures(t *testing.T) {
t.Parallel()
for _, fixture := range findFixtures(t) {
Expand All @@ -37,6 +41,11 @@ type fixtureExpectation struct {
Annotations draftAnnotations `json:"annotations"`
// ValidationOnly limits the fixture to source validation until projection supports it.
ValidationOnly bool `json:"validation_only"`
// Problems are substrings of the problems the oracle must report, one each; any
// other problem fails the fixture.
Problems []string `json:"problems"`
// DeclarationChanges lists the exact descriptor additions or removals the oracle must report.
DeclarationChanges []oracle.DeclarationChange `json:"declaration_changes"`
}

// findFixtures returns the fixture directories under testdata, relative to it.
Expand Down Expand Up @@ -75,11 +84,47 @@ func runFixture(t *testing.T, dir string) {
t.Fatal(err)
}
root := filepath.Join(dir, "input")
out := t.TempDir()
out := filepath.Join(dir, "output")
if _, err := os.Stat(out); errors.Is(err, fs.ErrNotExist) {
out = t.TempDir()
if !generateFixture(t, dir, root, out, expected) {
return
}
} else if err != nil {
t.Fatal(err)
}

result, err := oracle.Check(root, out)
if err != nil {
t.Fatal(err)
}
problems := result.AllProblems()
if len(expected.DeclarationChanges) > 0 {
if !slices.Equal(result.DeclarationChanges, expected.DeclarationChanges) || result.DescriptorDiff == "" {
t.Errorf("want declaration changes %v, got %v\n%s", expected.DeclarationChanges, result.DeclarationChanges, result.DescriptorDiff)
}
problems = result.Problems
}
for _, want := range expected.Problems {
i := slices.IndexFunc(problems, func(problem string) bool { return strings.Contains(problem, want) })
if i < 0 {
t.Errorf("want an oracle problem containing %q", want)
continue
}
problems = slices.Delete(problems, i, i+1)
}
for _, problem := range problems {
t.Errorf("unexpected oracle problem: %s", problem)
}
}

// generateFixture runs the generator on root into out and checks the result against
// the fixture's expectations. It reports whether out holds output for the oracle.
func generateFixture(t *testing.T, dir, root, out string, expected fixtureExpectation) bool {
t.Helper()
if expected.ValidationOnly {
checkFixtureAnnotations(t, root, expected.Annotations)
return
return false
}
if expected.Error != "" {
before := readTree(t, out)
Expand All @@ -93,7 +138,7 @@ func runFixture(t *testing.T, dir string) {
if !maps.Equal(before, readTree(t, out)) {
t.Fatal("generation failure modified output")
}
return
return false
}

if err := testConfig.stableProtosGenerator(t.Context(), root, out); err != nil {
Expand Down Expand Up @@ -127,6 +172,7 @@ func runFixture(t *testing.T, dir string) {
}
}
}
return true
}

func checkFixtureAnnotations(t *testing.T, root string, expected draftAnnotations) {
Expand Down
1 change: 1 addition & 0 deletions cmd/generate-stable-protos/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.26.7

require (
github.com/bufbuild/protocompile v0.14.2-0.20260825174057-3dfa26e2df9b
github.com/google/go-cmp v0.7.0
google.golang.org/protobuf v1.36.12
pgregory.net/rapid v1.3.0
)
Expand Down
Loading
Loading