1010package shadow
1111
1212import (
13+ "os"
14+ "path/filepath"
15+ "regexp"
1316 "testing"
1417
1518 "github.com/stretchr/testify/require"
1619)
1720
21+ // findRedpandaRepoRoot searches upward for the *Redpanda* repository root by
22+ // looking for the MODULE.bazel file.
23+ func findRedpandaRepoRoot (t * testing.T ) string {
24+ // Try from current working directory first. It has to be found from WD to
25+ // work with Bazel tests.
26+ dir , err := os .Getwd ()
27+ require .NoError (t , err )
28+ for {
29+ if _ , err := os .Stat (filepath .Join (dir , "MODULE.bazel" )); err == nil {
30+ return dir
31+ }
32+ parent := filepath .Dir (dir )
33+ if parent == dir {
34+ t .Fatal ("failed to find repo root (MODULE.bazel file)" )
35+ }
36+ dir = parent
37+ }
38+ }
39+
1840func TestToScreamingSnakeCase (t * testing.T ) {
1941 tests := []struct {
2042 name string
@@ -37,3 +59,41 @@ func TestToScreamingSnakeCase(t *testing.T) {
3759 })
3860 }
3961}
62+
63+ // TestBufGenYamlCoreModuleVersion ensures the buf.gen.yaml module version
64+ // matches the go.mod dependency version for buf.build/gen/go/redpandadata/core.
65+ // When updating the core proto dependency in go.mod, developers must also run
66+ // `buf generate` to regenerate the proto comments.
67+ func TestBufGenYamlCoreModuleVersion (t * testing.T ) {
68+ repoRoot := findRedpandaRepoRoot (t )
69+
70+ // Read buf.gen.yaml and extract the commit from:
71+ // module: buf.build/redpandadata/core:<commit>
72+ bufGenPath := filepath .Join (repoRoot , "buf.gen.yaml" )
73+ bufGenContent , err := os .ReadFile (bufGenPath )
74+ require .NoError (t , err , "failed to read buf.gen.yaml" )
75+
76+ bufGenRe := regexp .MustCompile (`module:\s*buf\.build/redpandadata/core:([a-f0-9]{12})` )
77+ bufGenMatch := bufGenRe .FindSubmatch (bufGenContent )
78+ require .NotNil (t , bufGenMatch , "failed to find module version in buf.gen.yaml" )
79+ bufGenCommit := string (bufGenMatch [1 ])
80+
81+ // Read go.mod and extract the commit from:
82+ // buf.build/gen/go/redpandadata/core/protocolbuffers/go v1.36.11-20251218215858-<commit>.1
83+ goModPath := filepath .Join (repoRoot , "src" , "go" , "rpk" , "go.mod" )
84+ goModContent , err := os .ReadFile (goModPath )
85+ require .NoError (t , err , "failed to read go.mod" )
86+
87+ // The version format is: v<major>.<minor>.<patch>-<timestamp>-<commit>.<build>
88+ // We need to extract the 12-char commit hash before the final .<build> suffix.
89+ goModRe := regexp .MustCompile (`buf\.build/gen/go/redpandadata/core/protocolbuffers/go\s+v[0-9]+\.[0-9]+\.[0-9]+-[0-9]{14}-([a-f0-9]{12})\.[0-9]+` )
90+ goModMatch := goModRe .FindSubmatch (goModContent )
91+ require .NotNil (t , goModMatch , "failed to find buf.build/gen/go/redpandadata/core/protocolbuffers/go version in go.mod" )
92+ goModCommit := string (goModMatch [1 ])
93+
94+ require .Equal (t , goModCommit , bufGenCommit ,
95+ "buf.gen.yaml module version (%s) does not match go.mod dependency version (%s). " +
96+ "After updating the buf.build/gen/go/redpandadata/core/protocolbuffers/go dependency in go.mod, " +
97+ "update buf.gen.yaml module commit hash and run 'buf generate' from the repo root to regenerate proto comments." ,
98+ bufGenCommit , goModCommit )
99+ }
0 commit comments